1 import { ICurrencyModuleService } from "@medusajs/framework/types"
2 import { Module, Modules } from "@medusajs/framework/utils"
3 import { CurrencyModuleService } from "@services"
4 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
5
6 jest.setTimeout(100000)
7
8 moduleIntegrationTestRunner<ICurrencyModuleService>({
9 moduleName: Modules.CURRENCY,
10 testSuite: ({ service }) => {
11 describe("Currency Module Service", () => {
12 it(`should export the appropriate linkable configuration`, () => {
13 const linkable = Module(Modules.CURRENCY, {
14 service: CurrencyModuleService,
15 }).linkable
16
17 expect(Object.keys(linkable)).toEqual(["currency"])
18
19 Object.keys(linkable).forEach((key) => {
20 delete linkable[key].toJSON
21 })
22
23 expect(linkable).toEqual({
24 currency: {
25 code: {
26 linkable: "currency_code",
27 entity: "Currency",
28 primaryKey: "code",
29 serviceName: "currency",
30 field: "currency",
31 },
32 },
33 })
34 })
35
36 describe("list", () => {
37 it("list currencies", async () => {
38 const currenciesResult = await service.listCurrencies({}, {})
39 expect(currenciesResult).toEqual(
40 expect.arrayContaining([
41 expect.objectContaining({
42 code: "cad",
43 name: "Canadian Dollar",
44 decimal_digits: 2,
45 }),
46 expect.objectContaining({
47 code: "usd",
48 name: "US Dollar",
49 decimal_digits: 2,
50 }),
51 ])
52 )
53 })
54
55 it("list currencies by code", async () => {
56 const currenciesResult = await service.listCurrencies(
57 { code: ["usd"] },
58 {}
59 )
60
61 expect(currenciesResult).toEqual([
62 expect.objectContaining({
63 code: "usd",
64 name: "US Dollar",
65 }),
66 ])
67 })
68
69 it("list currencies by code regardless of case-sensitivity", async () => {
70 const currenciesResult = await service.listCurrencies(
71 { code: ["Usd"] },
72 {}
73 )
74
75 expect(currenciesResult).toEqual([
76 expect.objectContaining({
77 code: "usd",
78 name: "US Dollar",
79 }),
80 ])
81 })
82 })
83
84 describe("listAndCountCurrenciesCurrencies", () => {
85 it("should return currencies and count", async () => {
86 const [currenciesResult, count] =
87 await service.listAndCountCurrencies({}, {})
88
89 expect(count).toEqual(126)
90 expect(currenciesResult).toEqual(
91 expect.arrayContaining([
92 expect.objectContaining({
93 code: "cad",
94 name: "Canadian Dollar",
95 }),
96 expect.objectContaining({
97 code: "gmd",
98 name: "Gambian Dalasi",
99 decimal_digits: 2,
100 }),
101 expect.objectContaining({
102 code: "usd",
103 name: "US Dollar",
104 }),
105 ])
106 )
107 })
108
109 it("should return currencies and count when filtered", async () => {
110 const [currenciesResult, count] =
111 await service.listAndCountCurrencies(
112 {
113 code: ["usd"],
114 },
115 {}
116 )
117
118 expect(count).toEqual(1)
119 expect(currenciesResult).toEqual([
120 expect.objectContaining({
121 code: "usd",
122 name: "US Dollar",
123 }),
124 ])
125 })
126
127 it("should return currencies and count when using skip and take", async () => {
128 const [currenciesResult, count] =
129 await service.listAndCountCurrencies({}, { skip: 5, take: 1 })
130
131 expect(count).toEqual(126)
132 expect(currenciesResult).toEqual([
133 expect.objectContaining({
134 code: "ars",
135 name: "Argentine Peso",
136 }),
137 ])
138 })
139
140 it("should return requested fields", async () => {
141 const [currenciesResult, count] =
142 await service.listAndCountCurrencies(
143 {},
144 {
145 take: 1,
146 select: ["code", "rounding"],
147 }
148 )
149
150 const serialized = JSON.parse(JSON.stringify(currenciesResult))
151
152 expect(count).toEqual(126)
153 expect(serialized).toEqual([
154 {
155 code: "aed",
156 rounding: 0,
157 raw_rounding: expect.any(Object),
158 },
159 ])
160 })
161 })
162
163 describe("retrieve", () => {
164 const code = "usd"
165 const name = "US Dollar"
166
167 it("should return currency for the given code", async () => {
168 const currency = await service.retrieveCurrency(code)
169
170 expect(currency).toEqual(
171 expect.objectContaining({
172 code,
173 })
174 )
175 })
176
177 it("should return currency for the given code in a case-insensitive manner", async () => {
178 const currency = await service.retrieveCurrency(code.toUpperCase())
179
180 expect(currency).toEqual(
181 expect.objectContaining({
182 code,
183 })
184 )
185 })
186
187 it("should throw an error when currency with code does not exist", async () => {
188 let error
189
190 try {
191 await service.retrieveCurrency("does-not-exist")
192 } catch (e) {
193 error = e
194 }
195
196 expect(error.message).toEqual(
197 "Currency with code: does-not-exist was not found"
198 )
199 })
200
201 it("should throw an error when a code is not provided", async () => {
202 let error
203
204 try {
205 await service.retrieveCurrency(undefined as unknown as string)
206 } catch (e) {
207 error = e
208 }
209
210 expect(error.message).toEqual("currency - code must be defined")
211 })
212
213 it("should return currency based on config select param", async () => {
214 const currency = await service.retrieveCurrency(code, {
215 select: ["code", "name"],
216 })
217
218 const serialized = JSON.parse(JSON.stringify(currency))
219
220 expect(serialized).toEqual({
221 code,
222 name,
223 })
224 })
225 })
226 })
227 },
228 })