1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { createStoreCreditAccountsWorkflow } from "@medusajs/loyalty-plugin/workflows"
3 import {
4 adminHeaders,
5 createAdminUser,
6 generatePublishableKey,
7 generateStoreHeaders,
8 } from "../../../../helpers/create-admin-user"
9 import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
10
11 jest.setTimeout(60 * 1000)
12
13 medusaIntegrationTestRunner({
14 testSuite: ({ dbConnection, api, getContainer, dbUtils }) => {
15 let customer, storeCreditAccount, storeHeaders
16
17 beforeAll(async () => {
18 await createAdminUser(dbConnection, adminHeaders, getContainer())
19 const publishableKey = await generatePublishableKey(getContainer())
20 storeHeaders = generateStoreHeaders({ publishableKey })
21
22 const user = await createAuthenticatedCustomer(api, storeHeaders, {
23 email: "initial@customer.com",
24 })
25 storeHeaders.headers["Authorization"] = `Bearer ${user.jwt}`
26 customer = user.customer
27
28 await dbUtils.snapshot()
29 })
30
31 describe("createStoreCreditAccountsWorkflow", () => {
32 it("successfully creates a store credit account for customer", async () => {
33 const { result: storeCreditAccounts } =
34 await createStoreCreditAccountsWorkflow.run({
35 input: [
36 {
37 customer_id: customer.id,
38 currency_code: "USD",
39 },
40 ],
41 container: getContainer(),
42 })
43
44 storeCreditAccount = storeCreditAccounts[0]
45
46 expect(storeCreditAccount).toEqual(
47 expect.objectContaining({
48 customer_id: customer.id,
49 currency_code: "USD",
50 })
51 )
52 })
53
54 it("successfully creates a store credit account for anonymous customer", async () => {
55 const { result: storeCreditAccounts } =
56 await createStoreCreditAccountsWorkflow.run({
57 input: [
58 {
59 currency_code: "USD",
60 },
61 ],
62 container: getContainer(),
63 })
64
65 storeCreditAccount = storeCreditAccounts[0]
66
67 expect(storeCreditAccount).toEqual(
68 expect.objectContaining({
69 customer_id: null,
70 currency_code: "USD",
71 })
72 )
73 })
74
75 it("successfully create two anonymous store credit accounts", async () => {
76 const { result: storeCreditAccountsOne } =
77 await createStoreCreditAccountsWorkflow.run({
78 input: [
79 {
80 currency_code: "USD",
81 },
82 {
83 currency_code: "USD",
84 },
85 ],
86 container: getContainer(),
87 })
88
89 expect(storeCreditAccountsOne).toEqual(
90 expect.arrayContaining([
91 expect.objectContaining({
92 customer_id: null,
93 currency_code: "USD",
94 }),
95 expect.objectContaining({
96 customer_id: null,
97 currency_code: "USD",
98 }),
99 ])
100 )
101 })
102
103 it("fail to create store credit account if currency_code + customer combination already exists", async () => {
104 const { result: storeCreditAccountsOne } =
105 await createStoreCreditAccountsWorkflow.run({
106 input: [
107 {
108 customer_id: customer.id,
109 currency_code: "USD",
110 },
111 ],
112 container: getContainer(),
113 })
114
115 storeCreditAccount = storeCreditAccountsOne[0]
116
117 expect(storeCreditAccount).toEqual(
118 expect.objectContaining({
119 customer_id: customer.id,
120 currency_code: "USD",
121 })
122 )
123
124 const { errors } = await createStoreCreditAccountsWorkflow.run({
125 input: [
126 {
127 currency_code: "USD",
128 customer_id: customer.id,
129 },
130 ],
131 container: getContainer(),
132 throwOnError: false,
133 })
134
135 const error = errors[0]
136
137 expect(error.error.message).toEqual(
138 `Store credit account with customer_id: ${customer.id}, currency_code: USD, already exists.`
139 )
140 })
141
142 it("should fail to create a store credit account if currency code is not provided", async () => {
143 const { errors } = await createStoreCreditAccountsWorkflow.run({
144 input: [{}],
145 container: getContainer(),
146 throwOnError: false,
147 })
148
149 const error = errors[0]
150
151 expect(error.error.message).toEqual(
152 "Currency code is required to create a store credit account"
153 )
154 })
155 })
156 },
157 })