|
| 1 | +import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
| 2 | +import { RemoteQueryFunction } from "@medusajs/types" |
| 3 | +import { ContainerRegistrationKeys, defaultCurrencies } from "@medusajs/utils" |
| 4 | +import { setTimeout } from "timers/promises" |
| 5 | +import { |
| 6 | + adminHeaders, |
| 7 | + createAdminUser, |
| 8 | +} from "../../../helpers/create-admin-user" |
| 9 | + |
| 10 | +jest.setTimeout(120000) |
| 11 | + |
| 12 | +process.env.ENABLE_INDEX_MODULE = "true" |
| 13 | + |
| 14 | +medusaIntegrationTestRunner({ |
| 15 | + testSuite: ({ getContainer, dbConnection, api, dbConfig }) => { |
| 16 | + let appContainer |
| 17 | + |
| 18 | + beforeAll(() => { |
| 19 | + appContainer = getContainer() |
| 20 | + }) |
| 21 | + |
| 22 | + afterAll(() => { |
| 23 | + process.env.ENABLE_INDEX_MODULE = "false" |
| 24 | + }) |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + await createAdminUser(dbConnection, adminHeaders, appContainer) |
| 28 | + }) |
| 29 | + |
| 30 | + describe("Index engine - Query.index", () => { |
| 31 | + it("should use query.index to query the index module and hydrate the data", async () => { |
| 32 | + const shippingProfile = ( |
| 33 | + await api.post( |
| 34 | + `/admin/shipping-profiles`, |
| 35 | + { name: "Test", type: "default" }, |
| 36 | + adminHeaders |
| 37 | + ) |
| 38 | + ).data.shipping_profile |
| 39 | + |
| 40 | + const payload = [ |
| 41 | + { |
| 42 | + title: "Test Product", |
| 43 | + description: "test-product-description", |
| 44 | + shipping_profile_id: shippingProfile.id, |
| 45 | + options: [{ title: "Denominations", values: ["100"] }], |
| 46 | + variants: [ |
| 47 | + { |
| 48 | + title: `Test variant 1`, |
| 49 | + sku: `test-variant-1`, |
| 50 | + prices: [ |
| 51 | + { |
| 52 | + currency_code: Object.values(defaultCurrencies)[0].code, |
| 53 | + amount: 30, |
| 54 | + }, |
| 55 | + { |
| 56 | + currency_code: Object.values(defaultCurrencies)[2].code, |
| 57 | + amount: 50, |
| 58 | + }, |
| 59 | + ], |
| 60 | + options: { |
| 61 | + Denominations: "100", |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + { |
| 67 | + title: "Extra product", |
| 68 | + description: "extra description", |
| 69 | + shipping_profile_id: shippingProfile.id, |
| 70 | + options: [{ title: "Colors", values: ["Red"] }], |
| 71 | + variants: new Array(2).fill(0).map((_, i) => ({ |
| 72 | + title: `extra variant ${i}`, |
| 73 | + sku: `extra-variant-${i}`, |
| 74 | + prices: [ |
| 75 | + { |
| 76 | + currency_code: Object.values(defaultCurrencies)[1].code, |
| 77 | + amount: 20, |
| 78 | + }, |
| 79 | + { |
| 80 | + currency_code: Object.values(defaultCurrencies)[0].code, |
| 81 | + amount: 80, |
| 82 | + }, |
| 83 | + ], |
| 84 | + options: { |
| 85 | + Colors: "Red", |
| 86 | + }, |
| 87 | + })), |
| 88 | + }, |
| 89 | + ] |
| 90 | + |
| 91 | + for (const data of payload) { |
| 92 | + await api.post("/admin/products", data, adminHeaders).catch((err) => { |
| 93 | + console.log(err) |
| 94 | + }) |
| 95 | + } |
| 96 | + await setTimeout(5000) |
| 97 | + |
| 98 | + const query = appContainer.resolve( |
| 99 | + ContainerRegistrationKeys.QUERY |
| 100 | + ) as RemoteQueryFunction |
| 101 | + |
| 102 | + const resultset = await query.index({ |
| 103 | + entity: "product", |
| 104 | + fields: [ |
| 105 | + "id", |
| 106 | + "description", |
| 107 | + "status", |
| 108 | + |
| 109 | + "variants.sku", |
| 110 | + "variants.barcode", |
| 111 | + "variants.material", |
| 112 | + "variants.options.value", |
| 113 | + "variants.prices.amount", |
| 114 | + "variants.prices.currency_code", |
| 115 | + "variants.inventory_items.inventory.sku", |
| 116 | + "variants.inventory_items.inventory.description", |
| 117 | + ], |
| 118 | + filters: { |
| 119 | + "variants.sku": { $like: "%-1" }, |
| 120 | + "variants.prices.amount": { $gt: 30 }, |
| 121 | + }, |
| 122 | + pagination: { |
| 123 | + order: { |
| 124 | + "variants.prices.amount": "DESC", |
| 125 | + }, |
| 126 | + }, |
| 127 | + }) |
| 128 | + |
| 129 | + expect(resultset.data).toEqual([ |
| 130 | + { |
| 131 | + id: expect.any(String), |
| 132 | + description: "extra description", |
| 133 | + status: "draft", |
| 134 | + variants: [ |
| 135 | + { |
| 136 | + sku: "extra-variant-0", |
| 137 | + barcode: null, |
| 138 | + material: null, |
| 139 | + id: expect.any(String), |
| 140 | + options: [ |
| 141 | + { |
| 142 | + value: "Red", |
| 143 | + }, |
| 144 | + ], |
| 145 | + inventory_items: [ |
| 146 | + { |
| 147 | + variant_id: expect.any(String), |
| 148 | + inventory_item_id: expect.any(String), |
| 149 | + inventory: { |
| 150 | + sku: "extra-variant-0", |
| 151 | + description: "extra variant 0", |
| 152 | + id: expect.any(String), |
| 153 | + }, |
| 154 | + }, |
| 155 | + ], |
| 156 | + prices: expect.arrayContaining([]), |
| 157 | + }, |
| 158 | + { |
| 159 | + sku: "extra-variant-1", |
| 160 | + barcode: null, |
| 161 | + material: null, |
| 162 | + id: expect.any(String), |
| 163 | + options: [ |
| 164 | + { |
| 165 | + value: "Red", |
| 166 | + }, |
| 167 | + ], |
| 168 | + prices: expect.arrayContaining([ |
| 169 | + { |
| 170 | + amount: 20, |
| 171 | + currency_code: "CAD", |
| 172 | + id: expect.any(String), |
| 173 | + }, |
| 174 | + { |
| 175 | + amount: 80, |
| 176 | + currency_code: "USD", |
| 177 | + id: expect.any(String), |
| 178 | + }, |
| 179 | + ]), |
| 180 | + inventory_items: [ |
| 181 | + { |
| 182 | + variant_id: expect.any(String), |
| 183 | + inventory_item_id: expect.any(String), |
| 184 | + inventory: { |
| 185 | + sku: "extra-variant-1", |
| 186 | + description: "extra variant 1", |
| 187 | + id: expect.any(String), |
| 188 | + }, |
| 189 | + }, |
| 190 | + ], |
| 191 | + }, |
| 192 | + ], |
| 193 | + }, |
| 194 | + { |
| 195 | + id: expect.any(String), |
| 196 | + description: "test-product-description", |
| 197 | + status: "draft", |
| 198 | + variants: [ |
| 199 | + { |
| 200 | + sku: "test-variant-1", |
| 201 | + barcode: null, |
| 202 | + material: null, |
| 203 | + id: expect.any(String), |
| 204 | + options: [ |
| 205 | + { |
| 206 | + value: "100", |
| 207 | + }, |
| 208 | + ], |
| 209 | + prices: expect.arrayContaining([ |
| 210 | + { |
| 211 | + amount: 30, |
| 212 | + currency_code: "USD", |
| 213 | + id: expect.any(String), |
| 214 | + }, |
| 215 | + { |
| 216 | + amount: 50, |
| 217 | + currency_code: "EUR", |
| 218 | + id: expect.any(String), |
| 219 | + }, |
| 220 | + ]), |
| 221 | + inventory_items: [ |
| 222 | + { |
| 223 | + variant_id: expect.any(String), |
| 224 | + inventory_item_id: expect.any(String), |
| 225 | + inventory: { |
| 226 | + sku: "test-variant-1", |
| 227 | + description: "Test variant 1", |
| 228 | + id: expect.any(String), |
| 229 | + }, |
| 230 | + }, |
| 231 | + ], |
| 232 | + }, |
| 233 | + ], |
| 234 | + }, |
| 235 | + ]) |
| 236 | + }) |
| 237 | + }) |
| 238 | + }, |
| 239 | +}) |
0 commit comments