1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 generatePublishableKey,
6 generateStoreHeaders,
7 } from "../../../helpers/create-admin-user"
8
9 jest.setTimeout(30000)
10
11 medusaIntegrationTestRunner({
12 env: {},
13 testSuite: ({ dbConnection, getContainer, api }) => {
14 let option1
15 let option2
16
17 let storeHeaders
18
19 beforeEach(async () => {
20 const container = getContainer()
21 await createAdminUser(dbConnection, adminHeaders, container)
22
23 const publishableKey = await generatePublishableKey(container)
24 storeHeaders = generateStoreHeaders({ publishableKey })
25
26 option1 = (
27 await api.post(
28 "/admin/product-options",
29 {
30 title: "option1",
31 values: ["A", "B", "C"],
32 },
33 adminHeaders
34 )
35 ).data.product_option
36
37 option2 = (
38 await api.post(
39 "/admin/product-options",
40 {
41 title: "option2",
42 values: ["D", "E"],
43 is_exclusive: true,
44 },
45 adminHeaders
46 )
47 ).data.product_option
48 })
49
50 describe("GET /admin/product-options", () => {
51 it("should return a list of product options", async () => {
52 const res = await api.get("/admin/product-options", adminHeaders)
53
54 expect(res.status).toEqual(200)
55 expect(res.data.product_options).toEqual(
56 expect.arrayContaining([
57 expect.objectContaining({
58 title: "option1",
59 is_exclusive: false,
60 values: expect.arrayContaining([
61 expect.objectContaining({ value: "A" }),
62 expect.objectContaining({ value: "B" }),
63 expect.objectContaining({ value: "C" }),
64 ]),
65 }),
66 expect.objectContaining({
67 title: "option2",
68 is_exclusive: true,
69 values: expect.arrayContaining([
70 expect.objectContaining({ value: "D" }),
71 expect.objectContaining({ value: "E" }),
72 ]),
73 }),
74 ])
75 )
76 })
77
78 it("should return a list of product options matching free text search param", async () => {
79 const res = await api.get("/admin/product-options?q=1", adminHeaders)
80
81 expect(res.status).toEqual(200)
82 expect(res.data.product_options.length).toEqual(1)
83 expect(res.data.product_options).toEqual(
84 expect.arrayContaining([
85 expect.objectContaining({ title: "option1" }),
86 ])
87 )
88 })
89
90 it("should return a list of exclusive product options", async () => {
91 const res = await api.get(
92 "/admin/product-options?is_exclusive=false",
93 adminHeaders
94 )
95
96 expect(res.status).toEqual(200)
97 expect(res.data.product_options.length).toEqual(1)
98 expect(res.data.product_options).toEqual(
99 expect.arrayContaining([
100 expect.objectContaining({ title: "option1" }),
101 ])
102 )
103 })
104 })
105
106 describe("POST /admin/product-options", () => {
107 it("should create a product option with value ranks", async () => {
108 const option = (
109 await api.post(
110 `/admin/product-options`,
111 {
112 title: "option3",
113 values: ["D", "E"],
114 ranks: {
115 E: 1,
116 D: 2,
117 },
118 },
119 adminHeaders
120 )
121 ).data.product_option
122
123 expect(option).toEqual(
124 expect.objectContaining({
125 title: "option3",
126 is_exclusive: false,
127 values: expect.arrayContaining([
128 expect.objectContaining({
129 value: "D",
130 rank: 2,
131 }),
132 expect.objectContaining({
133 value: "E",
134 rank: 1,
135 }),
136 ]),
137 })
138 )
139 })
140
141 it("should throw if a rank is specified for invalid value", async () => {
142 const error = await api
143 .post(
144 `/admin/product-options`,
145 {
146 title: "option3",
147 values: ["D", "E"],
148 ranks: {
149 E: 1,
150 invalid: 2,
151 },
152 },
153 adminHeaders
154 )
155 .catch((err) => err)
156
157 expect(error.response.status).toEqual(400)
158 expect(error.response.data.message).toEqual(
159 'Value "invalid" is assigned a rank but is not defined in the list of values.'
160 )
161 })
162
163 it("should fail to create a second global option with an existing title", async () => {
164 // option1 is a global (non-exclusive) option titled "option1". Global
165 // option titles are unique (partial unique index on product_option),
166 // so a second global with the same title is rejected.
167 const error = await api
168 .post(
169 `/admin/product-options`,
170 {
171 title: "option1",
172 values: ["X"],
173 is_exclusive: false,
174 },
175 adminHeaders
176 )
177 .catch((err) => err)
178
179 expect(error.response.status).toEqual(400)
180 expect(error.response.data.message).toContain("already exists")
181 })
182
183 it("should allow an exclusive option to reuse a global option's title", async () => {
184 // The uniqueness constraint only applies to global options. Exclusive
185 // options are scoped to a single product and may repeat titles.
186 const option = (
187 await api.post(
188 `/admin/product-options`,
189 {
190 title: "option1",
191 values: ["X"],
192 is_exclusive: true,
193 },
194 adminHeaders
195 )
196 ).data.product_option
197
198 expect(option).toEqual(
199 expect.objectContaining({
200 title: "option1",
201 is_exclusive: true,
202 })
203 )
204 })
205 })
206
207 describe("GET /admin/product-options/[id]", () => {
208 it("should return a product option", async () => {
209 const res = await api.get(
210 `/admin/product-options/${option1.id}`,
211 adminHeaders
212 )
213
214 expect(res.status).toEqual(200)
215 expect(res.data.product_option.values.length).toEqual(3)
216 expect(res.data.product_option).toEqual(
217 expect.objectContaining({
218 title: "option1",
219 is_exclusive: false,
220 values: expect.arrayContaining([
221 expect.objectContaining({ value: "A" }),
222 expect.objectContaining({ value: "B" }),
223 expect.objectContaining({ value: "C" }),
224 ]),
225 })
226 )
227 })
228 })
229
230 describe("POST /admin/product-options/[id]", () => {
231 it("should update a product option", async () => {
232 const option = (
233 await api.post(
234 `/admin/product-options/${option2.id}`,
235 {
236 is_exclusive: false,
237 },
238 adminHeaders
239 )
240 ).data.product_option
241
242 expect(option.values.length).toEqual(2)
243 expect(option).toEqual(
244 expect.objectContaining({
245 title: "option2",
246 is_exclusive: false,
247 values: expect.arrayContaining([
248 expect.objectContaining({ value: "D" }),
249 expect.objectContaining({ value: "E" }),
250 ]),
251 })
252 )
253
254 const res = await api.get(
255 "/admin/product-options?is_exclusive=true",
256 adminHeaders
257 )
258
259 expect(res.status).toEqual(200)
260 expect(res.data.product_options.length).toEqual(0)
261 })
262
263 it("should fail to update a product option from global to exclusive", async () => {
264 const error = await api
265 .post(
266 `/admin/product-options/${option1.id}`,
267 {
268 is_exclusive: true,
269 },
270 adminHeaders
271 )
272 .catch((err) => err)
273
274 expect(error.response.status).toEqual(400)
275 expect(error.response.data.message).toEqual(
276 `Cannot change product option: ${option1.id} from global to exclusive.`
277 )
278 })
279
280 it("should update a product value ranks", async () => {
281 const option = (
282 await api.post(
283 `/admin/product-options/${option2.id}`,
284 {
285 ranks: {
286 D: 2,
287 E: 1,
288 },
289 },
290 adminHeaders
291 )
292 ).data.product_option
293
294 expect(option.values.length).toEqual(2)
295 expect(option).toEqual(
296 expect.objectContaining({
297 title: "option2",
298 is_exclusive: true,
299 values: expect.arrayContaining([
300 expect.objectContaining({
301 value: "D",
302 rank: 2,
303 }),
304 expect.objectContaining({
305 value: "E",
306 rank: 1,
307 }),
308 ]),
309 })
310 )
311 })
312
313 it("should throw when trying to update an option that does not exist", async () => {
314 const error = await api
315 .post(
316 `/admin/product-options/iDontExist`,
317 {
318 is_exclusive: false,
319 },
320 adminHeaders
321 )
322 .catch((e) => e)
323
324 expect(error.response.status).toEqual(404)
325 expect(error.response.data).toEqual({
326 message: 'Product option with id "iDontExist" not found',
327 type: "not_found",
328 })
329 })
330 })
331
332 describe("DELETE /admin/product-options/[id]", () => {
333 it("should delete a product option", async () => {
334 await api.delete(`/admin/product-options/${option2.id}`, adminHeaders)
335
336 const res = await api.get("/admin/product-options", adminHeaders)
337
338 expect(res.status).toEqual(200)
339 expect(res.data.product_options.length).toEqual(1)
340 })
341 })
342
343 describe("GET /store/product-options", () => {
344 it("should list product options", async () => {
345 const res = await api.get("/store/product-options", storeHeaders)
346
347 expect(res.status).toEqual(200)
348 expect(res.data.product_options.length).toEqual(2)
349 expect(res.data.product_options).toEqual(
350 expect.arrayContaining([
351 expect.objectContaining({
352 id: option1.id,
353 title: "option1",
354 }),
355 expect.objectContaining({
356 id: option2.id,
357 title: "option2",
358 }),
359 ])
360 )
361 })
362
363 it("should filter product options by is_exclusive", async () => {
364 const res = await api.get(
365 "/store/product-options?is_exclusive=true",
366 storeHeaders
367 )
368
369 expect(res.status).toEqual(200)
370 expect(res.data.product_options).toEqual([
371 expect.objectContaining({
372 id: option2.id,
373 is_exclusive: true,
374 }),
375 ])
376 })
377
378 it("should filter product options by free text search", async () => {
379 const res = await api.get("/store/product-options?q=1", storeHeaders)
380
381 expect(res.status).toEqual(200)
382 expect(res.data.product_options).toEqual([
383 expect.objectContaining({
384 id: option1.id,
385 title: "option1",
386 }),
387 ])
388 })
389 })
390
391 describe("GET /store/product-options/[id]", () => {
392 it("should retrieve a product option", async () => {
393 const res = await api.get(
394 `/store/product-options/${option1.id}`,
395 storeHeaders
396 )
397
398 expect(res.status).toEqual(200)
399 expect(res.data.product_option).toEqual(
400 expect.objectContaining({
401 id: option1.id,
402 title: "option1",
403 })
404 )
405 })
406
407 it("should return 404 when the product option does not exist", async () => {
408 const error = await api
409 .get(`/store/product-options/not-found`, storeHeaders)
410 .catch((e) => e)
411
412 expect(error.response.status).toEqual(404)
413 expect(error.response.data.message).toEqual(
414 "Product option with id: not-found was not found"
415 )
416 })
417 })
418 },
419 })