1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 createAdminUser,
4 adminHeaders,
5 } from "../../../../helpers/create-admin-user"
6
7 jest.setTimeout(30000)
8
9 medusaIntegrationTestRunner({
10 env: {},
11 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
12 let pricePreference1
13 let pricePreference2
14
15 beforeAll(async () => {
16 const container = getContainer()
17 await createAdminUser(dbConnection, adminHeaders, container)
18
19 pricePreference1 = (
20 await api.post(
21 "/admin/price-preferences",
22 {
23 attribute: "region_id",
24 value: "region-1",
25 is_tax_inclusive: true,
26 },
27 adminHeaders
28 )
29 ).data.price_preference
30
31 pricePreference2 = (
32 await api.post(
33 "/admin/price-preferences",
34 {
35 attribute: "currency_code",
36 value: "EUR",
37 is_tax_inclusive: true,
38 },
39 adminHeaders
40 )
41 ).data.price_preference
42
43 await dbUtils.snapshot()
44 })
45
46 describe("/admin/price-preferences", () => {
47 describe("POST /admin/price-preferences", () => {
48 it("creates a price preference", async () => {
49 const newPricePreference = (
50 await api.post(
51 "/admin/price-preferences",
52 {
53 attribute: "region_id",
54 value: "region-2",
55 is_tax_inclusive: true,
56 },
57 adminHeaders
58 )
59 ).data.price_preference
60
61 expect(newPricePreference).toEqual(
62 expect.objectContaining({
63 attribute: "region_id",
64 value: "region-2",
65 is_tax_inclusive: true,
66 })
67 )
68 })
69
70 it("creates a price preference with false tax inclusivity by default", async () => {
71 const newPricePreference = (
72 await api.post(
73 "/admin/price-preferences",
74 {
75 attribute: "region_id",
76 value: "region-2",
77 },
78 adminHeaders
79 )
80 ).data.price_preference
81
82 expect(newPricePreference).toEqual(
83 expect.objectContaining({
84 attribute: "region_id",
85 value: "region-2",
86 is_tax_inclusive: false,
87 })
88 )
89 })
90 })
91
92 describe("GET /admin/price-preferences", () => {
93 it("returns a list of price preferences", async () => {
94 const response = (
95 await api.get("/admin/price-preferences", adminHeaders)
96 ).data.price_preferences
97
98 expect(response).toEqual(
99 expect.arrayContaining([
100 expect.objectContaining({
101 attribute: "region_id",
102 value: "region-1",
103 is_tax_inclusive: true,
104 }),
105 expect.objectContaining({
106 attribute: "currency_code",
107 value: "EUR",
108 is_tax_inclusive: true,
109 }),
110 ])
111 )
112 })
113 it("filters price preferences by attribute", async () => {
114 const response = (
115 await api.get(
116 "/admin/price-preferences?attribute=region_id",
117 adminHeaders
118 )
119 ).data.price_preferences
120
121 expect(response).toEqual([
122 expect.objectContaining({
123 attribute: "region_id",
124 value: "region-1",
125 is_tax_inclusive: true,
126 }),
127 ])
128 })
129 })
130
131 describe("GET /admin/price-preferences/:id", () => {
132 it("returns a price preference by :id", async () => {
133 const response = (
134 await api.get(
135 `/admin/price-preferences/${pricePreference1.id}`,
136 adminHeaders
137 )
138 ).data.price_preference
139
140 expect(response).toEqual(
141 expect.objectContaining({
142 attribute: "region_id",
143 value: "region-1",
144 is_tax_inclusive: true,
145 })
146 )
147 })
148 })
149
150 describe("POST /admin/price-preferences/:id", () => {
151 it("updates a price preference", async () => {
152 const response = (
153 await api.post(
154 `/admin/price-preferences/${pricePreference1.id}`,
155 {
156 attribute: "region_id",
157 value: "region-2",
158 is_tax_inclusive: false,
159 },
160 adminHeaders
161 )
162 ).data.price_preference
163
164 expect(response).toEqual(
165 expect.objectContaining({
166 attribute: "region_id",
167 value: "region-2",
168 is_tax_inclusive: false,
169 })
170 )
171 })
172 it("updates the tax inclusivity in the price preference", async () => {
173 const response = (
174 await api.post(
175 `/admin/price-preferences/${pricePreference1.id}`,
176 {
177 is_tax_inclusive: false,
178 },
179 adminHeaders
180 )
181 ).data.price_preference
182
183 expect(response).toEqual(
184 expect.objectContaining({
185 attribute: "region_id",
186 value: "region-1",
187 is_tax_inclusive: false,
188 })
189 )
190 })
191 })
192
193 describe("DELETE /admin/price-preferences/:id", () => {
194 it("Deletes a price preference", async () => {
195 const deleteResponse = await api.delete(
196 `/admin/price-preferences/${pricePreference1.id}`,
197 adminHeaders
198 )
199
200 const remainingPricePreferences = (
201 await api.get("/admin/price-preferences", adminHeaders)
202 ).data.price_preferences
203
204 expect(deleteResponse.data).toEqual(
205 expect.objectContaining({
206 id: pricePreference1.id,
207 object: "price_preference",
208 deleted: true,
209 })
210 )
211
212 expect(remainingPricePreferences).toEqual(
213 expect.arrayContaining([
214 expect.objectContaining({
215 attribute: "currency_code",
216 value: "EUR",
217 is_tax_inclusive: true,
218 }),
219 expect.objectContaining({
220 attribute: "currency_code",
221 value: "eur",
222 is_tax_inclusive: false,
223 }),
224 ])
225 )
226 })
227 })
228 })
229 },
230 })