1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { createAdminUser } from "../../../../helpers/create-admin-user"
3 import { Modules } from "@medusajs/utils"
4 import { updateTaxRegionsWorkflow } from "@medusajs/core-flows"
5
6 jest.setTimeout(50000)
7
8 const env = {}
9 const adminHeaders = {
10 headers: { "x-medusa-access-token": "test_token" },
11 }
12
13 medusaIntegrationTestRunner({
14 env,
15 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
16 let container
17
18 beforeAll(async () => {
19 container = getContainer()
20 })
21
22 describe("/admin/tax-regions", () => {
23 beforeAll(async () => {
24 await createAdminUser(dbConnection, adminHeaders, container)
25
26 await dbUtils.snapshot()
27 })
28
29 describe("POST /admin/tax-regions/:id", () => {
30 let taxRegion
31
32 beforeEach(async () => {
33 const parentRegion = await api.post(
34 "/admin/tax-regions",
35 {
36 country_code: "us",
37 provider_id: "tp_system",
38 },
39 adminHeaders
40 )
41
42 taxRegion = (
43 await api.post(
44 "/admin/tax-regions",
45 {
46 country_code: "us",
47 province_code: "tx",
48 parent_id: parentRegion.data.tax_region.id,
49 metadata: { test: "created" },
50 },
51 adminHeaders
52 )
53 ).data.tax_region
54 })
55
56 it("should successfully update a tax region's fieleds", async () => {
57 let taxRegionResponse = await api.post(
58 `/admin/tax-regions/${taxRegion.id}`,
59 {
60 province_code: "ny",
61 metadata: { test: "updated" },
62 },
63 adminHeaders
64 )
65
66 expect(taxRegionResponse.status).toEqual(200)
67 expect(taxRegionResponse.data.tax_region).toEqual(
68 expect.objectContaining({
69 id: expect.any(String),
70 province_code: "ny",
71 metadata: { test: "updated" },
72 })
73 )
74
75 taxRegionResponse = await api.post(
76 `/admin/tax-regions/${taxRegion.id}`,
77 { metadata: { test: "updated 2" } },
78 adminHeaders
79 )
80
81 expect(taxRegionResponse.status).toEqual(200)
82 expect(taxRegionResponse.data.tax_region).toEqual(
83 expect.objectContaining({
84 id: expect.any(String),
85 province_code: "ny",
86 metadata: { test: "updated 2" },
87 })
88 )
89
90 taxRegionResponse = await api.post(
91 `/admin/tax-regions/${taxRegion.id}`,
92 { province_code: "ca" },
93 adminHeaders
94 )
95
96 expect(taxRegionResponse.status).toEqual(200)
97 expect(taxRegionResponse.data.tax_region).toEqual(
98 expect.objectContaining({
99 id: expect.any(String),
100 province_code: "ca",
101 metadata: { test: "updated 2" },
102 })
103 )
104 })
105
106 it("should create a province tax region without a provider", async () => {
107 const response = await api.post(
108 `/admin/tax-regions`,
109 {
110 country_code: "us",
111 parent_id: taxRegion.id,
112 province_code: "ny",
113 },
114 adminHeaders
115 )
116
117 expect(response.status).toEqual(200)
118 expect(response.data.tax_region).toEqual(
119 expect.objectContaining({
120 id: expect.any(String),
121 country_code: "us",
122 province_code: "ny",
123 provider_id: null,
124 })
125 )
126 })
127
128 it("should fail to create a country tax region without a provider", async () => {
129 const {
130 response: { status, data },
131 } = await api
132 .post(
133 `/admin/tax-regions`,
134 {
135 country_code: "uk",
136 },
137 adminHeaders
138 )
139 .catch((err) => err)
140
141 expect(status).toEqual(400)
142 expect(data).toEqual({
143 message:
144 "Invalid request: Provider is required when creating a non-province tax region.",
145 type: "invalid_data",
146 })
147 })
148
149 it("should throw if tax region does not exist", async () => {
150 const {
151 response: { status, data },
152 } = await api
153 .post(
154 `/admin/tax-regions/does-not-exist`,
155 { province_code: "ny", metadata: { test: "updated" } },
156 adminHeaders
157 )
158 .catch((err) => err)
159
160 expect(status).toEqual(404)
161 expect(data).toEqual({
162 message: 'TaxRegion with id "does-not-exist" not found',
163 type: "not_found",
164 })
165 })
166 })
167
168 describe("workflows", () => {
169 it("updateTaxRegionsWorkflow - should not call listTaxRegions when data is empty", async () => {
170 const taxService = container.resolve(Modules.TAX)
171 const listTaxRegionsSpy = jest.spyOn(taxService, "listTaxRegions")
172
173 const { result } = await updateTaxRegionsWorkflow(container).run({
174 input: [],
175 })
176
177 expect(result).toEqual([])
178 expect(listTaxRegionsSpy).not.toHaveBeenCalled()
179
180 listTaxRegionsSpy.mockRestore()
181 })
182 })
183 })
184 },
185 })