|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
adminHeaders, |
|
4
|
createAdminUser, |
|
5
|
} from "../../../../helpers/create-admin-user" |
|
6
|
|
|
7
|
jest.setTimeout(50000) |
|
8
|
|
|
9
|
medusaIntegrationTestRunner({ |
|
10
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
11
|
describe("GET /admin/products - SKU Search", () => { |
|
12
|
beforeAll(async () => { |
|
13
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
14
|
|
|
15
|
await dbUtils.snapshot() |
|
16
|
}) |
|
17
|
|
|
18
|
describe("searching products by variant SKU using q parameter", () => { |
|
19
|
it("should return products when searching by exact variant SKU", async () => { |
|
20
|
// Create a product with a unique SKU |
|
21
|
let product |
|
22
|
product = await api.post( |
|
23
|
"/admin/products", |
|
24
|
{ |
|
25
|
title: "Test Product for SKU Search", |
|
26
|
status: "published", |
|
27
|
options: [ |
|
28
|
{ |
|
29
|
title: "Default", |
|
30
|
values: ["Default"], |
|
31
|
}, |
|
32
|
], |
|
33
|
variants: [ |
|
34
|
{ |
|
35
|
title: "Test Variant", |
|
36
|
sku: "UNIQUE-SKU-12345", |
|
37
|
options: { |
|
38
|
Default: "Default", |
|
39
|
}, |
|
40
|
prices: [ |
|
41
|
{ |
|
42
|
currency_code: "usd", |
|
43
|
amount: 1000, |
|
44
|
}, |
|
45
|
], |
|
46
|
}, |
|
47
|
], |
|
48
|
}, |
|
49
|
adminHeaders |
|
50
|
) |
|
51
|
|
|
52
|
expect(product.status).toEqual(200) |
|
53
|
expect(product.data.product).toBeDefined() |
|
54
|
|
|
55
|
// Search for the product using the SKU |
|
56
|
const response = await api.get( |
|
57
|
"/admin/products?q=UNIQUE-SKU-12345", |
|
58
|
adminHeaders |
|
59
|
) |
|
60
|
|
|
61
|
expect(response.status).toEqual(200) |
|
62
|
expect(response.data.products).toHaveLength(1) |
|
63
|
expect(response.data.products[0].id).toEqual(product.data.product.id) |
|
64
|
expect(response.data.products[0].variants).toEqual( |
|
65
|
expect.arrayContaining([ |
|
66
|
expect.objectContaining({ |
|
67
|
sku: "UNIQUE-SKU-12345", |
|
68
|
}), |
|
69
|
]) |
|
70
|
) |
|
71
|
}) |
|
72
|
|
|
73
|
it("should return products when searching by partial variant SKU", async () => { |
|
74
|
// Create a product with a SKU |
|
75
|
const product = await api.post( |
|
76
|
"/admin/products", |
|
77
|
{ |
|
78
|
title: "Another Test Product", |
|
79
|
status: "published", |
|
80
|
options: [ |
|
81
|
{ |
|
82
|
title: "Default", |
|
83
|
values: ["Default"], |
|
84
|
}, |
|
85
|
], |
|
86
|
variants: [ |
|
87
|
{ |
|
88
|
title: "Another Variant", |
|
89
|
sku: "PARTIAL-TEST-SKU-999", |
|
90
|
options: { |
|
91
|
Default: "Default", |
|
92
|
}, |
|
93
|
prices: [ |
|
94
|
{ |
|
95
|
currency_code: "usd", |
|
96
|
amount: 2000, |
|
97
|
}, |
|
98
|
], |
|
99
|
}, |
|
100
|
], |
|
101
|
}, |
|
102
|
adminHeaders |
|
103
|
) |
|
104
|
|
|
105
|
expect(product.status).toEqual(200) |
|
106
|
|
|
107
|
// Search using partial SKU |
|
108
|
const response = await api.get( |
|
109
|
"/admin/products?q=TEST-SKU", |
|
110
|
adminHeaders |
|
111
|
) |
|
112
|
|
|
113
|
expect(response.status).toEqual(200) |
|
114
|
expect(response.data.products.length).toBeGreaterThanOrEqual(1) |
|
115
|
|
|
116
|
const foundProduct = response.data.products.find( |
|
117
|
(p) => p.id === product.data.product.id |
|
118
|
) |
|
119
|
expect(foundProduct).toBeDefined() |
|
120
|
expect(foundProduct.variants).toEqual( |
|
121
|
expect.arrayContaining([ |
|
122
|
expect.objectContaining({ |
|
123
|
sku: "PARTIAL-TEST-SKU-999", |
|
124
|
}), |
|
125
|
]) |
|
126
|
) |
|
127
|
}) |
|
128
|
|
|
129
|
it("should return products with multiple variants when searching by SKU of one variant", async () => { |
|
130
|
// Create a product with multiple variants |
|
131
|
const product = await api.post( |
|
132
|
"/admin/products", |
|
133
|
{ |
|
134
|
title: "Multi-Variant Product", |
|
135
|
status: "published", |
|
136
|
options: [ |
|
137
|
{ |
|
138
|
title: "Size", |
|
139
|
values: ["Small", "Large"], |
|
140
|
}, |
|
141
|
], |
|
142
|
variants: [ |
|
143
|
{ |
|
144
|
title: "Small Variant", |
|
145
|
sku: "MULTI-VAR-SMALL", |
|
146
|
options: { |
|
147
|
Size: "Small", |
|
148
|
}, |
|
149
|
prices: [ |
|
150
|
{ |
|
151
|
currency_code: "usd", |
|
152
|
amount: 1500, |
|
153
|
}, |
|
154
|
], |
|
155
|
}, |
|
156
|
{ |
|
157
|
title: "Large Variant", |
|
158
|
sku: "MULTI-VAR-LARGE", |
|
159
|
options: { |
|
160
|
Size: "Large", |
|
161
|
}, |
|
162
|
prices: [ |
|
163
|
{ |
|
164
|
currency_code: "usd", |
|
165
|
amount: 2500, |
|
166
|
}, |
|
167
|
], |
|
168
|
}, |
|
169
|
], |
|
170
|
}, |
|
171
|
adminHeaders |
|
172
|
) |
|
173
|
|
|
174
|
expect(product.status).toEqual(200) |
|
175
|
|
|
176
|
// Search using one variant's SKU |
|
177
|
const response = await api.get( |
|
178
|
"/admin/products?q=MULTI-VAR-SMALL", |
|
179
|
adminHeaders |
|
180
|
) |
|
181
|
|
|
182
|
expect(response.status).toEqual(200) |
|
183
|
expect(response.data.products.length).toBeGreaterThanOrEqual(1) |
|
184
|
|
|
185
|
const foundProduct = response.data.products.find( |
|
186
|
(p) => p.id === product.data.product.id |
|
187
|
) |
|
188
|
expect(foundProduct).toBeDefined() |
|
189
|
// Should return the product with all its variants |
|
190
|
expect(foundProduct.variants.length).toEqual(2) |
|
191
|
}) |
|
192
|
|
|
193
|
it("should search across product title AND variant SKU", async () => { |
|
194
|
// Create products to test combined search |
|
195
|
const productWithTitleMatch = await api.post( |
|
196
|
"/admin/products", |
|
197
|
{ |
|
198
|
title: "SearchTerm Product", |
|
199
|
status: "published", |
|
200
|
options: [ |
|
201
|
{ |
|
202
|
title: "Default", |
|
203
|
values: ["Default"], |
|
204
|
}, |
|
205
|
], |
|
206
|
variants: [ |
|
207
|
{ |
|
208
|
title: "Regular Variant", |
|
209
|
sku: "REGULAR-SKU-001", |
|
210
|
options: { |
|
211
|
Default: "Default", |
|
212
|
}, |
|
213
|
prices: [ |
|
214
|
{ |
|
215
|
currency_code: "usd", |
|
216
|
amount: 1000, |
|
217
|
}, |
|
218
|
], |
|
219
|
}, |
|
220
|
], |
|
221
|
}, |
|
222
|
adminHeaders |
|
223
|
) |
|
224
|
|
|
225
|
const productWithSKUMatch = await api.post( |
|
226
|
"/admin/products", |
|
227
|
{ |
|
228
|
title: "Different Product", |
|
229
|
status: "published", |
|
230
|
options: [ |
|
231
|
{ |
|
232
|
title: "Default", |
|
233
|
values: ["Default"], |
|
234
|
}, |
|
235
|
], |
|
236
|
variants: [ |
|
237
|
{ |
|
238
|
title: "Variant with SearchTerm in SKU", |
|
239
|
sku: "SearchTerm-SKU-002", |
|
240
|
options: { |
|
241
|
Default: "Default", |
|
242
|
}, |
|
243
|
prices: [ |
|
244
|
{ |
|
245
|
currency_code: "usd", |
|
246
|
amount: 1500, |
|
247
|
}, |
|
248
|
], |
|
249
|
}, |
|
250
|
], |
|
251
|
}, |
|
252
|
adminHeaders |
|
253
|
) |
|
254
|
|
|
255
|
expect(productWithTitleMatch.status).toEqual(200) |
|
256
|
expect(productWithSKUMatch.status).toEqual(200) |
|
257
|
|
|
258
|
// Search using the term - should find both products |
|
259
|
const response = await api.get( |
|
260
|
"/admin/products?q=SearchTerm", |
|
261
|
adminHeaders |
|
262
|
) |
|
263
|
|
|
264
|
expect(response.status).toEqual(200) |
|
265
|
expect(response.data.products.length).toBeGreaterThanOrEqual(2) |
|
266
|
|
|
267
|
const productIds = response.data.products.map((p) => p.id) |
|
268
|
expect(productIds).toContain(productWithTitleMatch.data.product.id) |
|
269
|
expect(productIds).toContain(productWithSKUMatch.data.product.id) |
|
270
|
}) |
|
271
|
|
|
272
|
it("should not return products when searching for non-existent SKU", async () => { |
|
273
|
// Create a product |
|
274
|
await api.post( |
|
275
|
"/admin/products", |
|
276
|
{ |
|
277
|
title: "Product without matching SKU", |
|
278
|
status: "published", |
|
279
|
options: [ |
|
280
|
{ |
|
281
|
title: "Default", |
|
282
|
values: ["Default"], |
|
283
|
}, |
|
284
|
], |
|
285
|
variants: [ |
|
286
|
{ |
|
287
|
title: "Normal Variant", |
|
288
|
sku: "NORMAL-SKU-123", |
|
289
|
options: { |
|
290
|
Default: "Default", |
|
291
|
}, |
|
292
|
prices: [ |
|
293
|
{ |
|
294
|
currency_code: "usd", |
|
295
|
amount: 1000, |
|
296
|
}, |
|
297
|
], |
|
298
|
}, |
|
299
|
], |
|
300
|
}, |
|
301
|
adminHeaders |
|
302
|
) |
|
303
|
|
|
304
|
// Search for a non-existent SKU |
|
305
|
const response = await api.get( |
|
306
|
"/admin/products?q=NONEXISTENT-SKU-99999", |
|
307
|
adminHeaders |
|
308
|
) |
|
309
|
|
|
310
|
expect(response.status).toEqual(200) |
|
311
|
expect(response.data.products).toHaveLength(0) |
|
312
|
}) |
|
313
|
|
|
314
|
it("should be case-insensitive when searching by SKU", async () => { |
|
315
|
// Create a product with uppercase SKU |
|
316
|
const product = await api.post( |
|
317
|
"/admin/products", |
|
318
|
{ |
|
319
|
title: "Case Test Product", |
|
320
|
status: "published", |
|
321
|
options: [ |
|
322
|
{ |
|
323
|
title: "Default", |
|
324
|
values: ["Default"], |
|
325
|
}, |
|
326
|
], |
|
327
|
variants: [ |
|
328
|
{ |
|
329
|
title: "Case Test Variant", |
|
330
|
sku: "UPPERCASE-SKU-ABC", |
|
331
|
options: { |
|
332
|
Default: "Default", |
|
333
|
}, |
|
334
|
prices: [ |
|
335
|
{ |
|
336
|
currency_code: "usd", |
|
337
|
amount: 1000, |
|
338
|
}, |
|
339
|
], |
|
340
|
}, |
|
341
|
], |
|
342
|
}, |
|
343
|
adminHeaders |
|
344
|
) |
|
345
|
|
|
346
|
expect(product.status).toEqual(200) |
|
347
|
|
|
348
|
// Search using lowercase |
|
349
|
const response = await api.get( |
|
350
|
"/admin/products?q=uppercase-sku-abc", |
|
351
|
adminHeaders |
|
352
|
) |
|
353
|
|
|
354
|
expect(response.status).toEqual(200) |
|
355
|
expect(response.data.products.length).toBeGreaterThanOrEqual(1) |
|
356
|
|
|
357
|
const foundProduct = response.data.products.find( |
|
358
|
(p) => p.id === product.data.product.id |
|
359
|
) |
|
360
|
expect(foundProduct).toBeDefined() |
|
361
|
}) |
|
362
|
}) |
|
363
|
}) |
|
364
|
}, |
|
365
|
}) |