1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { IStoreModuleService } from "@medusajs/types"
3 import { Modules } from "@medusajs/utils"
4 import {
5 adminHeaders,
6 createAdminUser,
7 } from "../../../../helpers/create-admin-user"
8 import {
9 createStoresWorkflow,
10 updateStoresWorkflow,
11 } from "@medusajs/core-flows"
12
13 jest.setTimeout(90000)
14
15 medusaIntegrationTestRunner({
16 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
17 describe("/admin/stores", () => {
18 let store
19 let container
20
21 beforeAll(async () => {
22 container = getContainer()
23 const storeModule: IStoreModuleService = container.resolve(
24 Modules.STORE
25 )
26 await createAdminUser(dbConnection, adminHeaders, container)
27
28 // A default store is created when the app is started, so we want to delete that one and create one specifically for our tests.
29 const defaultId = (await api.get("/admin/stores", adminHeaders)).data
30 .stores?.[0]?.id
31 if (defaultId) {
32 await storeModule.deleteStores(defaultId)
33 }
34
35 store = await storeModule.createStores({
36 name: "New store",
37 supported_currencies: [
38 { currency_code: "usd", is_default: true },
39 { currency_code: "dkk" },
40 ],
41 default_sales_channel_id: "sc_12345",
42 })
43
44 await dbUtils.snapshot()
45 })
46
47 // BREAKING: The URL changed from `GET /admin/store` to `GET /admin/stores`
48 describe("Store creation", () => {
49 it("has created store with default currency", async () => {
50 const resStore = (
51 await api.get("/admin/stores", adminHeaders)
52 ).data.stores.find((s) => s.id === store.id)
53
54 // BREAKING: The store response contained currencies, modules, and feature flags, which are not present anymore
55 expect(resStore).toEqual(
56 expect.objectContaining({
57 id: expect.any(String),
58 name: "New store",
59 default_sales_channel_id: expect.any(String),
60 supported_currencies: [
61 expect.objectContaining({
62 currency_code: "usd",
63 is_default: true,
64 }),
65 expect.objectContaining({ currency_code: "dkk" }),
66 ],
67 created_at: expect.any(String),
68 updated_at: expect.any(String),
69 })
70 )
71 })
72 })
73
74 describe("workflows", () => {
75 describe("createStoresWorkflow", () => {
76 it("should not call listPricePreferences when supported_currencies is empty", async () => {
77 const priceService = container.resolve(Modules.PRICING)
78 const listPricePreferencesSpy = jest.spyOn(
79 priceService,
80 "listPricePreferences"
81 )
82
83 const { result } = await createStoresWorkflow(container).run({
84 input: {
85 stores: [
86 {
87 name: "New store",
88 supported_currencies: [],
89 },
90 ],
91 },
92 })
93
94 expect(result).toEqual([
95 expect.objectContaining({
96 name: "New store",
97 supported_currencies: [],
98 }),
99 ])
100
101 expect(listPricePreferencesSpy).not.toHaveBeenCalled()
102 listPricePreferencesSpy.mockRestore()
103 })
104 })
105 })
106
107 describe("POST /admin/stores/:id", () => {
108 it("should update store", async () => {
109 const response = await api
110 .post(
111 `/admin/stores/${store.id}`,
112 {
113 name: "Updated store",
114
115 supported_currencies: [
116 { currency_code: "eur", is_default: true },
117 { currency_code: "usd" },
118 ],
119 },
120 adminHeaders
121 )
122 .catch((e) => e)
123
124 expect(response.status).toEqual(200)
125 expect(response.data.store).toEqual(
126 expect.objectContaining({
127 name: "Updated store",
128 supported_currencies: [
129 expect.objectContaining({ currency_code: "eur" }),
130 expect.objectContaining({ currency_code: "usd" }),
131 ],
132 })
133 )
134 })
135
136 it("fails to update default currencies if there is no default one", async () => {
137 const err = await api
138 .post(
139 `/admin/stores/${store.id}`,
140 {
141 supported_currencies: [{ currency_code: "eur" }],
142 },
143 adminHeaders
144 )
145 .catch((e) => e)
146
147 expect(err.response.status).toBe(400)
148 expect(err.response.data).toEqual(
149 expect.objectContaining({
150 type: "invalid_data",
151 message: "There should be a default currency set for the store",
152 })
153 )
154 })
155
156 // BREAKING: `currencies` was renamed to `supported_currencies`
157 it("fails to remove default currency from currencies without replacing it", async () => {
158 const err = await api
159 .post(
160 `/admin/stores/${store.id}`,
161 {
162 supported_currencies: [{ currency_code: "dkk" }],
163 },
164 adminHeaders
165 )
166 .catch((e) => e)
167
168 expect(err.response.status).toBe(400)
169 expect(err.response.data).toEqual(
170 expect.objectContaining({
171 type: "invalid_data",
172 message: "There should be a default currency set for the store",
173 })
174 )
175 })
176
177 it("successfully updates currencies and default currency", async () => {
178 const response = await api.post(
179 `/admin/stores/${store.id}`,
180 {
181 supported_currencies: [
182 { currency_code: "usd" },
183 { currency_code: "dkk", is_default: true },
184 ],
185 },
186 adminHeaders
187 )
188
189 expect(response.status).toEqual(200)
190 expect(response.data.store).toEqual(
191 expect.objectContaining({
192 id: expect.any(String),
193 name: "New store",
194 supported_currencies: [
195 expect.objectContaining({
196 currency_code: "usd",
197 }),
198 expect.objectContaining({
199 currency_code: "dkk",
200 is_default: true,
201 }),
202 ],
203 created_at: expect.any(String),
204 updated_at: expect.any(String),
205 })
206 )
207 })
208
209 it("successfully updates and store currencies", async () => {
210 const response = await api.post(
211 `/admin/stores/${store.id}`,
212 {
213 supported_currencies: [
214 { currency_code: "jpy", is_default: true },
215 { currency_code: "usd" },
216 ],
217 },
218 adminHeaders
219 )
220
221 expect(response.status).toEqual(200)
222 expect(response.data.store).toEqual(
223 expect.objectContaining({
224 id: expect.any(String),
225 default_sales_channel_id: expect.any(String),
226 name: "New store",
227 supported_currencies: [
228 expect.objectContaining({
229 currency_code: "jpy",
230 is_default: true,
231 }),
232 expect.objectContaining({
233 currency_code: "usd",
234 }),
235 ],
236 created_at: expect.any(String),
237 updated_at: expect.any(String),
238 })
239 )
240 })
241 })
242
243 describe("GET /admin/stores", () => {
244 it("supports searching of stores", async () => {
245 const response = await api.get(
246 "/admin/stores?q=nonexistent",
247 adminHeaders
248 )
249 expect(response.status).toEqual(200)
250 expect(response.data.stores).toHaveLength(0)
251
252 const response2 = await api.get("/admin/stores?q=store", adminHeaders)
253 expect(response.status).toEqual(200)
254 expect(response2.data.stores).toEqual([
255 expect.objectContaining({
256 id: store.id,
257 name: "New store",
258 }),
259 ])
260 })
261 })
262 })
263 },
264 })