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", () => {
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 it("should create a customer address", async () => {
31 // Create a customer
32 const customer = await customerModuleService.createCustomers({
33 first_name: "John",
34 last_name: "Doe",
35 })
36
37 const response = await api.post(
38 `/admin/customers/${customer.id}/addresses?fields=*addresses`,
39 {
40 first_name: "John",
41 last_name: "Doe",
42 address_1: "Test street 1",
43 },
44 adminHeaders
45 )
46
47 expect(response.status).toEqual(200)
48 expect(response.data.customer.addresses).toEqual(
49 expect.arrayContaining([
50 expect.objectContaining({
51 id: expect.any(String),
52 first_name: "John",
53 last_name: "Doe",
54 address_1: "Test street 1",
55 }),
56 ])
57 )
58
59 const customerWithAddresses =
60 await customerModuleService.retrieveCustomer(customer.id, {
61 relations: ["addresses"],
62 })
63
64 expect(customerWithAddresses.addresses?.length).toEqual(1)
65 })
66
67 it("sets new shipping address as default and unsets the old one", async () => {
68 const customer = await customerModuleService.createCustomers({
69 first_name: "John",
70 last_name: "Doe",
71 addresses: [
72 {
73 first_name: "John",
74 last_name: "Doe",
75 address_1: "Test street 1",
76 is_default_shipping: true,
77 },
78 ],
79 })
80
81 const response = await api.post(
82 `/admin/customers/${customer.id}/addresses`,
83 {
84 first_name: "John",
85 last_name: "Doe",
86 address_1: "Test street 2",
87 is_default_shipping: true,
88 },
89 adminHeaders
90 )
91
92 expect(response.status).toEqual(200)
93
94 const [address] = await customerModuleService.listCustomerAddresses({
95 customer_id: customer.id,
96 is_default_shipping: true,
97 })
98
99 expect(address.address_1).toEqual("Test street 2")
100 })
101
102 it("sets new billing address as default and unsets the old one", async () => {
103 const customer = await customerModuleService.createCustomers({
104 first_name: "John",
105 last_name: "Doe",
106 addresses: [
107 {
108 first_name: "John",
109 last_name: "Doe",
110 address_1: "Test street 1",
111 is_default_billing: true,
112 },
113 ],
114 })
115
116 const response = await api.post(
117 `/admin/customers/${customer.id}/addresses`,
118 {
119 first_name: "John",
120 last_name: "Doe",
121 address_1: "Test street 2",
122 is_default_billing: true,
123 },
124 adminHeaders
125 )
126
127 expect(response.status).toEqual(200)
128
129 const [address] = await customerModuleService.listCustomerAddresses({
130 customer_id: customer.id,
131 is_default_billing: true,
132 })
133
134 expect(address.address_1).toEqual("Test street 2")
135 })
136 })
137 },
138 })