1 import { IStoreModuleService } from "@medusajs/framework/types"
2 import { Module, Modules } from "@medusajs/framework/utils"
3 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
4 import { StoreModuleService } from "@services"
5 import { createStoreFixture } from "../__fixtures__"
6
7 jest.setTimeout(100000)
8
9 moduleIntegrationTestRunner<IStoreModuleService>({
10 moduleName: Modules.STORE,
11 testSuite: ({ service }) => {
12 describe("Store Module Service", () => {
13 it(`should export the appropriate linkable configuration`, () => {
14 const linkable = Module(Modules.STORE, {
15 service: StoreModuleService,
16 }).linkable
17
18 expect(Object.keys(linkable)).toEqual([
19 "store",
20 "storeCurrency",
21 "storeLocale",
22 ])
23
24 Object.keys(linkable).forEach((key) => {
25 delete linkable[key].toJSON
26 })
27
28 expect(linkable).toEqual({
29 store: {
30 id: {
31 linkable: "store_id",
32 entity: "Store",
33 primaryKey: "id",
34 serviceName: "store",
35 field: "store",
36 },
37 },
38 storeCurrency: {
39 id: {
40 linkable: "store_currency_id",
41 entity: "StoreCurrency",
42 primaryKey: "id",
43 serviceName: "store",
44 field: "storeCurrency",
45 },
46 },
47 storeLocale: {
48 id: {
49 linkable: "store_locale_id",
50 entity: "StoreLocale",
51 primaryKey: "id",
52 serviceName: "store",
53 field: "storeLocale",
54 },
55 },
56 })
57 })
58
59 describe("creating a store", () => {
60 it("should get created successfully", async function () {
61 const store = await service.createStores(createStoreFixture)
62
63 expect(store).toEqual(
64 expect.objectContaining({
65 name: "Test store",
66 supported_currencies: expect.arrayContaining([
67 expect.objectContaining({ currency_code: "eur" }),
68 expect.objectContaining({ currency_code: "usd" }),
69 ]),
70 supported_locales: expect.arrayContaining([
71 expect.objectContaining({ locale_code: "fr-FR" }),
72 expect.objectContaining({ locale_code: "en-US" }),
73 ]),
74 default_sales_channel_id: "test-sales-channel",
75 default_region_id: "test-region",
76 metadata: {
77 test: "test",
78 },
79 })
80 )
81 })
82
83 it("should fail to get created if there is no default currency", async function () {
84 const err = await service
85 .createStores({
86 ...createStoreFixture,
87 supported_currencies: [{ currency_code: "usd" }],
88 })
89 .catch((err) => err.message)
90
91 expect(err).toEqual(
92 "There should be a default currency set for the store"
93 )
94 })
95 })
96
97 describe("upserting a store", () => {
98 it("should get created if it does not exist", async function () {
99 const store = await service.upsertStores(createStoreFixture)
100
101 expect(store).toEqual(
102 expect.objectContaining({
103 name: "Test store",
104 default_sales_channel_id: "test-sales-channel",
105 default_region_id: "test-region",
106 metadata: {
107 test: "test",
108 },
109 })
110 )
111 })
112
113 it("should get created if it does not exist", async function () {
114 const createdStore = await service.upsertStores(createStoreFixture)
115 const upsertedStore = await service.upsertStores({
116 name: "Upserted store",
117 })
118
119 expect(upsertedStore).toEqual(
120 expect.objectContaining({
121 name: "Upserted store",
122 })
123 )
124 expect(upsertedStore.id).not.toEqual(createdStore.id)
125 })
126 })
127
128 describe("updating a store", () => {
129 it("should update the name successfully", async function () {
130 const createdStore = await service.createStores(createStoreFixture)
131 const updatedStore = await service.updateStores(createdStore.id, {
132 name: "Updated store",
133 })
134 expect(updatedStore.name).toEqual("Updated store")
135 })
136
137 it("should fail updating currencies without a default one", async function () {
138 const createdStore = await service.createStores(createStoreFixture)
139 const updateErr = await service
140 .updateStores(createdStore.id, {
141 supported_currencies: [{ currency_code: "usd" }],
142 })
143 .catch((err) => err.message)
144
145 expect(updateErr).toEqual(
146 "There should be a default currency set for the store"
147 )
148 })
149
150 it("should fail updating currencies where a duplicate currency code exists", async function () {
151 const createdStore = await service.createStores(createStoreFixture)
152 const updateErr = await service
153 .updateStores(createdStore.id, {
154 supported_currencies: [
155 { currency_code: "usd" },
156 { currency_code: "usd" },
157 ],
158 })
159 .catch((err) => err.message)
160
161 expect(updateErr).toEqual("Duplicate currency codes: usd")
162 })
163
164 it("should fail updating locales where a duplicate locale code exists", async function () {
165 const createdStore = await service.createStores(createStoreFixture)
166 const updateErr = await service
167 .updateStores(createdStore.id, {
168 supported_locales: [
169 { locale_code: "en-US" },
170 { locale_code: "en-US" },
171 ],
172 })
173 .catch((err) => err.message)
174
175 expect(updateErr).toEqual("Duplicate locale codes: en-US")
176 })
177
178 it("should fail updating currencies where there is more than 1 default currency", async function () {
179 const createdStore = await service.createStores(createStoreFixture)
180 const updateErr = await service
181 .updateStores(createdStore.id, {
182 supported_currencies: [
183 { currency_code: "usd", is_default: true },
184 { currency_code: "eur", is_default: true },
185 ],
186 })
187 .catch((err) => err.message)
188
189 expect(updateErr).toEqual("Only one default currency is allowed")
190 })
191 })
192
193 describe("deleting a store", () => {
194 it("should successfully delete existing stores", async function () {
195 const createdStore = await service.createStores([
196 createStoreFixture,
197 createStoreFixture,
198 ])
199
200 await service.deleteStores([createdStore[0].id, createdStore[1].id])
201
202 const storeInDatabase = await service.listStores()
203 expect(storeInDatabase).toHaveLength(0)
204 })
205 })
206
207 describe("retrieving a store", () => {
208 it("should successfully return all existing stores", async function () {
209 await service.createStores([
210 createStoreFixture,
211 { ...createStoreFixture, name: "Another store" },
212 ])
213
214 const storesInDatabase = await service.listStores()
215 expect(storesInDatabase).toHaveLength(2)
216 expect(storesInDatabase.map((s) => s.name)).toEqual(
217 expect.arrayContaining(["Test store", "Another store"])
218 )
219 })
220 })
221 })
222 },
223 })