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, dbUtils }) => {
14 let baseCollection
15 let baseCollection1
16 let baseCollection2
17 let storeHeaders
18
19 beforeAll(async () => {
20 const container = getContainer()
21 const publishableKey = await generatePublishableKey(container)
22 storeHeaders = generateStoreHeaders({ publishableKey })
23 await createAdminUser(dbConnection, adminHeaders, container)
24
25 baseCollection = (
26 await api.post(
27 "/admin/collections",
28 { title: "test-collection", external_id: "ext-collection-01" },
29 adminHeaders
30 )
31 ).data.collection
32
33 baseCollection1 = (
34 await api.post(
35 "/admin/collections",
36 { title: "test-collection1" },
37 adminHeaders
38 )
39 ).data.collection
40
41 baseCollection2 = (
42 await api.post(
43 "/admin/collections",
44 { title: "test-collection2" },
45 adminHeaders
46 )
47 ).data.collection
48
49 await dbUtils.snapshot()
50 })
51
52 describe("/store/collections", () => {
53 describe("/store/collections/:id", () => {
54 it("gets collection", async () => {
55 const response = await api.get(
56 `/store/collections/${baseCollection.id}`,
57 storeHeaders
58 )
59
60 expect(response.data.collection).toEqual(
61 expect.objectContaining({
62 id: baseCollection.id,
63 external_id: "ext-collection-01",
64 created_at: expect.any(String),
65 updated_at: expect.any(String),
66 })
67 )
68 })
69 })
70
71 describe("/store/collections", () => {
72 it("lists collections matching external_id search param", async () => {
73 const response = await api.get(
74 "/store/collections?external_id=ext-collection-01",
75 storeHeaders
76 )
77
78 expect(response.data).toEqual({
79 collections: [
80 expect.objectContaining({
81 id: baseCollection.id,
82 external_id: "ext-collection-01",
83 created_at: expect.any(String),
84 updated_at: expect.any(String),
85 }),
86 ],
87 count: 1,
88 limit: 10,
89 offset: 0,
90 })
91 })
92
93 it("lists collections", async () => {
94 const response = await api.get("/store/collections", storeHeaders)
95
96 expect(response.data).toEqual({
97 collections: [
98 expect.objectContaining({
99 id: baseCollection2.id,
100 created_at: expect.any(String),
101 updated_at: expect.any(String),
102 }),
103 expect.objectContaining({
104 id: baseCollection1.id,
105 created_at: expect.any(String),
106 updated_at: expect.any(String),
107 }),
108 expect.objectContaining({
109 id: baseCollection.id,
110 external_id: "ext-collection-01",
111 created_at: expect.any(String),
112 updated_at: expect.any(String),
113 }),
114 ],
115 count: 3,
116 limit: 10,
117 offset: 0,
118 })
119 })
120 })
121 })
122
123 describe("published products", () => {
124 const seed = async (collectionId) => {
125 const create = async (title, status) =>
126 (
127 await api.post(
128 "/admin/products",
129 {
130 title,
131 status,
132 options: [{ title: "size", values: ["one"] }],
133 variants: [
134 {
135 title: "one",
136 options: { size: "one" },
137 prices: [{ currency_code: "usd", amount: 10 }],
138 },
139 ],
140 },
141 adminHeaders
142 )
143 ).data.product
144
145 const draft = await create("draft product", "draft")
146 const published = await create("published product", "published")
147
148 await api.post(
149 `/admin/collections/${collectionId}/products`,
150 { add: [draft.id, published.id] },
151 adminHeaders
152 )
153 }
154
155 const titlesOf = (collection) =>
156 (collection?.products ?? []).map((product) => product.title).sort()
157
158 it("does not expand unpublished products on a collection", async () => {
159 await seed(baseCollection.id)
160
161 const response = await api.get(
162 `/store/collections/${baseCollection.id}?fields=id,products.title`,
163 storeHeaders
164 )
165
166 expect(response.status).toEqual(200)
167 expect(titlesOf(response.data.collection)).toEqual([
168 "published product",
169 ])
170 })
171
172 it("does not expand unpublished products when listing collections", async () => {
173 await seed(baseCollection.id)
174
175 const response = await api.get(
176 "/store/collections?fields=id,title,products.title",
177 storeHeaders
178 )
179
180 const collection = response.data.collections.find(
181 (c) => c.id === baseCollection.id
182 )
183 expect(titlesOf(collection)).toEqual(["published product"])
184 })
185 })
186 },
187 })