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("GET /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
31 it("should get all customer addresses and its count", async () => {
32 const [customer] = await customerModuleService.createCustomers([
33 {
34 first_name: "Test",
35 last_name: "Test",
36 email: "test@me.com",
37 addresses: [
38 {
39 first_name: "Test",
40 last_name: "Test",
41 address_1: "Test street 1",
42 },
43 {
44 first_name: "Test",
45 last_name: "Test",
46 address_1: "Test street 2",
47 },
48 {
49 first_name: "Test",
50 last_name: "Test",
51 address_1: "Test street 3",
52 },
53 ],
54 },
55 {
56 first_name: "Test Test",
57 last_name: "Test Test",
58 addresses: [
59 {
60 first_name: "Test TEST",
61 last_name: "Test TEST",
62 address_1: "NOT street 1",
63 },
64 ],
65 },
66 ])
67
68 const response = await api.get(
69 `/admin/customers/${customer.id}/addresses`,
70 adminHeaders
71 )
72
73 expect(response.status).toEqual(200)
74 expect(response.data.count).toEqual(3)
75 expect(response.data.addresses).toEqual(
76 expect.arrayContaining([
77 expect.objectContaining({
78 id: expect.any(String),
79 customer_id: customer.id,
80 address_1: "Test street 1",
81 }),
82 expect.objectContaining({
83 id: expect.any(String),
84 customer_id: customer.id,
85 address_1: "Test street 2",
86 }),
87 expect.objectContaining({
88 id: expect.any(String),
89 customer_id: customer.id,
90 address_1: "Test street 3",
91 }),
92 ])
93 )
94 })
95
96 it("should support searching through the addresses", async () => {
97 const [customer] = await customerModuleService.createCustomers([
98 {
99 first_name: "Test",
100 last_name: "Test",
101 email: "test@me.com",
102 addresses: [
103 {
104 first_name: "John",
105 last_name: "Doe",
106 address_1: "Huntingdon 123",
107 },
108 {
109 first_name: "Jane",
110 last_name: "Doe",
111 address_1: "Horseshoe 555",
112 },
113 {
114 first_name: "Jack",
115 last_name: "Doe",
116 address_1: "Hawkins 12",
117 },
118 ],
119 },
120 ])
121
122 const response = await api.get(
123 `/admin/customers/${customer.id}/addresses?q=12`,
124 adminHeaders
125 )
126
127 expect(response.status).toEqual(200)
128 expect(response.data.addresses).toHaveLength(2)
129 expect(response.data.addresses).toEqual(
130 expect.arrayContaining([
131 expect.objectContaining({
132 id: expect.any(String),
133 address_1: "Huntingdon 123",
134 }),
135 expect.objectContaining({
136 id: expect.any(String),
137 address_1: "Hawkins 12",
138 }),
139 ])
140 )
141 })
142 })
143 },
144 })