1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { ICustomerModuleService } from "@medusajs/types"
3 import { Modules } from "@medusajs/utils"
4 import { createAdminUser } from "../../../../helpers/create-admin-user"
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 describe("POST /admin/customers/:id/addresses/:address_id", () => {
17 let appContainer
18 let customerModuleService: ICustomerModuleService
19
20 beforeAll(async () => {
21 appContainer = getContainer()
22 customerModuleService = appContainer.resolve(Modules.CUSTOMER)
23 })
24
25 beforeAll(async () => {
26 await createAdminUser(dbConnection, adminHeaders, appContainer)
27
28 await dbUtils.snapshot()
29 })
30
31 it("should update a customer address", async () => {
32 const customer = await customerModuleService.createCustomers({
33 first_name: "John",
34 last_name: "Doe",
35 })
36
37 const address = await customerModuleService.createCustomerAddresses({
38 customer_id: customer.id,
39 first_name: "John",
40 last_name: "Doe",
41 address_1: "Test street 1",
42 })
43
44 const response = await api.post(
45 `/admin/customers/${customer.id}/addresses/${address.id}?fields=*addresses`,
46 {
47 first_name: "Jane",
48 },
49 adminHeaders
50 )
51
52 expect(response.status).toEqual(200)
53 expect(response.data.customer.addresses).toEqual(
54 expect.arrayContaining([
55 expect.objectContaining({
56 id: expect.any(String),
57 first_name: "Jane",
58 last_name: "Doe",
59 }),
60 ])
61 )
62 })
63
64 it("updates a new address to be default and unsets old one", async () => {
65 const customer = await customerModuleService.createCustomers({
66 first_name: "John",
67 last_name: "Doe",
68 })
69 const [, address] = await customerModuleService.createCustomerAddresses(
70 [
71 {
72 customer_id: customer.id,
73 first_name: "John",
74 last_name: "Doe",
75 address_1: "Test street 1",
76 is_default_shipping: true,
77 },
78 {
79 customer_id: customer.id,
80 first_name: "John",
81 last_name: "Doe",
82 address_1: "Test street 2",
83 },
84 ]
85 )
86
87 const response = await api.post(
88 `/admin/customers/${customer.id}/addresses/${address.id}`,
89 {
90 first_name: "jane",
91 is_default_shipping: true,
92 },
93 adminHeaders
94 )
95
96 expect(response.status).toEqual(200)
97
98 const [defaultAddress] =
99 await customerModuleService.listCustomerAddresses({
100 customer_id: customer.id,
101 is_default_shipping: true,
102 })
103
104 expect(defaultAddress.first_name).toEqual("jane")
105 expect(defaultAddress.address_1).toEqual("Test street 2")
106 })
107
108 // do the same as above but for billing address
109 it("updates a new address to be default and unsets old one", async () => {
110 const customer = await customerModuleService.createCustomers({
111 first_name: "John",
112 last_name: "Doe",
113 })
114 const [, address] = await customerModuleService.createCustomerAddresses(
115 [
116 {
117 customer_id: customer.id,
118 first_name: "John",
119 last_name: "Doe",
120 address_1: "Test street 1",
121 is_default_billing: true,
122 },
123 {
124 customer_id: customer.id,
125 first_name: "John",
126 last_name: "Doe",
127 address_1: "Test street 2",
128 },
129 ]
130 )
131
132 const response = await api.post(
133 `/admin/customers/${customer.id}/addresses/${address.id}`,
134 {
135 first_name: "jane",
136 is_default_billing: true,
137 },
138 adminHeaders
139 )
140
141 expect(response.status).toEqual(200)
142
143 const [defaultAddress] =
144 await customerModuleService.listCustomerAddresses({
145 customer_id: customer.id,
146 is_default_billing: true,
147 })
148
149 expect(defaultAddress.first_name).toEqual("jane")
150 expect(defaultAddress.address_1).toEqual("Test street 2")
151 })
152 })
153 },
154 })