1 import { Modules } from "@medusajs/utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6
7 import { ICustomerModuleService } from "@medusajs/types"
8
9 const { medusaIntegrationTestRunner } = require("@medusajs/test-utils")
10
11 jest.setTimeout(50000)
12
13 medusaIntegrationTestRunner({
14 env: {},
15 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
16 describe("GET /admin/customers", () => {
17 let appContainer
18 let shutdownServer
19 let customerModuleService: ICustomerModuleService
20
21 beforeAll(async () => {
22 appContainer = getContainer()
23 customerModuleService = appContainer.resolve(Modules.CUSTOMER)
24 })
25
26 beforeAll(async () => {
27 await createAdminUser(dbConnection, adminHeaders, appContainer)
28
29 await dbUtils.snapshot()
30 })
31
32 it("should get all customers and its count", async () => {
33 await customerModuleService.createCustomers([
34 {
35 first_name: "Test",
36 last_name: "Test",
37 email: "test@me.com",
38 },
39 ])
40
41 const response = await api.get(`/admin/customers`, adminHeaders)
42
43 expect(response.status).toEqual(200)
44 expect(response.data.count).toEqual(1)
45 expect(response.data.customers).toEqual([
46 expect.objectContaining({
47 id: expect.any(String),
48 first_name: "Test",
49 last_name: "Test",
50 email: "test@me.com",
51 }),
52 ])
53 })
54
55 it("should get all customers in specific customer group and its count", async () => {
56 const vipGroup = await customerModuleService.createCustomerGroups({
57 name: "VIP",
58 })
59
60 const [john] = await customerModuleService.createCustomers([
61 {
62 first_name: "John",
63 last_name: "Doe",
64 email: "john.doe@example.com",
65 },
66 {
67 first_name: "Jane",
68 last_name: "Smith",
69 email: "jane.smith@example.com",
70 },
71 ])
72
73 await customerModuleService.addCustomerToGroup({
74 customer_id: john.id,
75 customer_group_id: vipGroup.id,
76 })
77
78 const response = await api.get(
79 `/admin/customers?limit=20&offset=0&groups%5B0%5D=${vipGroup.id}`,
80 adminHeaders
81 )
82
83 expect(response.status).toEqual(200)
84 expect(response.data.count).toEqual(1)
85 expect(response.data.customers).toEqual([
86 expect.objectContaining({
87 first_name: "John",
88 last_name: "Doe",
89 email: "john.doe@example.com",
90 }),
91 ])
92 })
93
94 it("should filter customers by last name", async () => {
95 await customerModuleService.createCustomers([
96 {
97 first_name: "Jane",
98 last_name: "Doe",
99 email: "jane@me.com",
100 },
101 {
102 first_name: "John",
103 last_name: "Doe",
104 email: "john@me.com",
105 },
106 {
107 first_name: "LeBron",
108 last_name: "James",
109 email: "lebron@me.com",
110 },
111 {
112 first_name: "John",
113 last_name: "Silver",
114 email: "johns@me.com",
115 },
116 ])
117
118 const response = await api.get(
119 `/admin/customers?last_name=Doe`,
120 adminHeaders
121 )
122
123 expect(response.status).toEqual(200)
124 expect(response.data.count).toEqual(2)
125 expect(response.data.customers).toContainEqual(
126 expect.objectContaining({
127 id: expect.any(String),
128 first_name: "Jane",
129 last_name: "Doe",
130 email: "jane@me.com",
131 })
132 )
133 expect(response.data.customers).toContainEqual(
134 expect.objectContaining({
135 id: expect.any(String),
136 first_name: "John",
137 last_name: "Doe",
138 email: "john@me.com",
139 })
140 )
141 })
142
143 it("should support searching of customers", async () => {
144 await customerModuleService.createCustomers([
145 {
146 first_name: "Jane",
147 last_name: "Doe",
148 email: "jane@me.com",
149 },
150 {
151 first_name: "John",
152 last_name: "Doe",
153 email: "john@me.com",
154 },
155 {
156 first_name: "LeBron",
157 last_name: "James",
158 email: "lebron@me.com",
159 },
160 ])
161
162 const response = await api.get(`/admin/customers?q=do`, adminHeaders)
163
164 expect(response.status).toEqual(200)
165 expect(response.data.customers).toHaveLength(2)
166 expect(response.data.customers).toEqual(
167 expect.arrayContaining([
168 expect.objectContaining({
169 first_name: "Jane",
170 last_name: "Doe",
171 }),
172 expect.objectContaining({
173 first_name: "John",
174 last_name: "Doe",
175 }),
176 ])
177 )
178 })
179 })
180 },
181 })