1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../helpers/create-admin-user"
6 import { Modules } from "@medusajs/utils"
7 import { updateRefundReasonsWorkflow } from "@medusajs/core-flows"
8
9 jest.setTimeout(30000)
10
11 medusaIntegrationTestRunner({
12 testSuite: ({ dbConnection, api, getContainer, dbUtils }) => {
13 let refundReason1
14 let refundReason2
15 let appContainer
16
17 beforeAll(async () => {
18 appContainer = getContainer()
19 await createAdminUser(dbConnection, adminHeaders, appContainer)
20
21 refundReason1 = (
22 await api.post(
23 "/admin/refund-reasons",
24 { label: "reason 1 - too big", code: "too_big" },
25 adminHeaders
26 )
27 ).data.refund_reason
28
29 refundReason2 = (
30 await api.post(
31 "/admin/refund-reasons",
32 { label: "reason 2 - too small", code: "too_small" },
33 adminHeaders
34 )
35 ).data.refund_reason
36
37 await dbUtils.snapshot()
38 })
39
40 describe("GET /admin/refund-reasons", () => {
41 it("should list refund reasons and query count", async () => {
42 const response = await api
43 .get("/admin/refund-reasons", adminHeaders)
44 .catch((err) => {
45 console.log(err)
46 })
47
48 expect(response.status).toEqual(200)
49 expect(response.data.count).toEqual(5) // There are 3 default ones
50 expect(response.data.refund_reasons).toEqual(
51 expect.arrayContaining([
52 expect.objectContaining({
53 label: "Customer Care Adjustment",
54 code: "customer_care_adjustment",
55 }),
56 expect.objectContaining({
57 label: "Shipping Issue",
58 code: "shipping_issue",
59 }),
60 expect.objectContaining({
61 label: "Pricing Error",
62 code: "pricing_error",
63 }),
64 expect.objectContaining({
65 label: "reason 1 - too big",
66 code: "too_big",
67 }),
68 expect.objectContaining({
69 label: "reason 2 - too small",
70 code: "too_small",
71 }),
72 ])
73 )
74 })
75
76 it("should list refund-reasons with specific query", async () => {
77 const response = await api.get(
78 "/admin/refund-reasons?q=1",
79 adminHeaders
80 )
81
82 expect(response.status).toEqual(200)
83 expect(response.data.count).toEqual(1)
84 expect(response.data.refund_reasons).toEqual(
85 expect.arrayContaining([
86 expect.objectContaining({
87 label: "reason 1 - too big",
88 }),
89 ])
90 )
91 })
92 })
93
94 describe("POST /admin/refund-reasons", () => {
95 it("should create a refund reason", async () => {
96 const response = await api.post(
97 "/admin/refund-reasons",
98 {
99 label: "reason test",
100 code: "reason_test",
101 description: "test description",
102 },
103 adminHeaders
104 )
105
106 expect(response.status).toEqual(200)
107 expect(response.data.refund_reason).toEqual(
108 expect.objectContaining({
109 label: "reason test",
110 code: "reason_test",
111 description: "test description",
112 })
113 )
114 })
115 })
116
117 describe("POST /admin/refund-reasons/:id", () => {
118 it("should correctly update refund reason", async () => {
119 const response = await api.post(
120 `/admin/refund-reasons/${refundReason1.id}`,
121 {
122 label: "reason test",
123 code: "reason_test",
124 description: "test description",
125 },
126 adminHeaders
127 )
128
129 expect(response.status).toEqual(200)
130 expect(response.data.refund_reason).toEqual(
131 expect.objectContaining({
132 label: "reason test",
133 code: "reason_test",
134 description: "test description",
135 })
136 )
137 })
138 })
139
140 describe("GET /admin/refund-reasons/:id", () => {
141 it("should fetch a refund reason", async () => {
142 const response = await api.get(
143 `/admin/refund-reasons/${refundReason1.id}`,
144 adminHeaders
145 )
146
147 expect(response.status).toEqual(200)
148 expect(response.data.refund_reason).toEqual(
149 expect.objectContaining({
150 id: refundReason1.id,
151 })
152 )
153 })
154 })
155
156 describe("DELETE /admin/refund-reasons/:id", () => {
157 it("should remove refund reasons", async () => {
158 const deleteResponse = await api.delete(
159 `/admin/refund-reasons/${refundReason1.id}`,
160 adminHeaders
161 )
162
163 expect(deleteResponse.data).toEqual({
164 id: refundReason1.id,
165 object: "refund_reason",
166 deleted: true,
167 })
168
169 await api
170 .get(`/admin/refund-reasons/${refundReason1.id}`, adminHeaders)
171 .catch((error) => {
172 expect(error.response.data.type).toEqual("not_found")
173 expect(error.response.data.message).toEqual(
174 `Refund reason with id: ${refundReason1.id.id} not found`
175 )
176 })
177 })
178 })
179
180 describe("workflows", () => {
181 it("updateRefundReasonsWorkflow - should not call listRefundReasons when data is empty", async () => {
182 const paymentService = appContainer.resolve(Modules.PAYMENT)
183 const listRefundReasonsSpy = jest.spyOn(
184 paymentService,
185 "listRefundReasons"
186 )
187
188 const { result } = await updateRefundReasonsWorkflow(appContainer).run({
189 input: [],
190 })
191
192 expect(result).toEqual([])
193 expect(listRefundReasonsSpy).not.toHaveBeenCalled()
194
195 listRefundReasonsSpy.mockRestore()
196 })
197 })
198 },
199 })