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 customer group customers", () => {
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 groups and its count", async () => {
32 const group = await customerModuleService.createCustomerGroups({
33 name: "Test",
34 })
35
36 const customers = await customerModuleService.createCustomers([
37 {
38 first_name: "Test",
39 last_name: "Test",
40 },
41 {
42 first_name: "Test2",
43 last_name: "Test2",
44 },
45 ])
46
47 // add to group
48
49 await customerModuleService.addCustomerToGroup(
50 customers.map((c) => ({
51 customer_id: c.id,
52 customer_group_id: group.id,
53 }))
54 )
55
56 const response = await api.get(
57 `/admin/customers?groups[]=${group.id}`,
58 adminHeaders
59 )
60
61 expect(response.status).toEqual(200)
62 expect(response.data.count).toEqual(2)
63 expect(response.data.customers).toEqual(
64 expect.arrayContaining([
65 expect.objectContaining({
66 id: customers[0].id,
67 }),
68 expect.objectContaining({
69 id: customers[1].id,
70 }),
71 ])
72 )
73 })
74 })
75 },
76 })