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/customer-groups/:id/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 batch add customers to a group", async () => {
32 const group = await customerModuleService.createCustomerGroups({
33 name: "VIP",
34 })
35 const customers = await customerModuleService.createCustomers([
36 {
37 first_name: "Test",
38 last_name: "Test",
39 },
40 {
41 first_name: "Test2",
42 last_name: "Test2",
43 },
44 {
45 first_name: "Test3",
46 last_name: "Test3",
47 },
48 ])
49
50 const response = await api.post(
51 `/admin/customer-groups/${group.id}/customers`,
52 {
53 add: customers.map((c) => c.id),
54 },
55 adminHeaders
56 )
57
58 expect(response.status).toEqual(200)
59
60 const updatedGroup = await customerModuleService.retrieveCustomerGroup(
61 group.id,
62 {
63 relations: ["customers"],
64 }
65 )
66 expect(updatedGroup.customers?.length).toEqual(3)
67 })
68 })
69 },
70 })