feat(modules-sdk, types, user, utils):init user module events (#6431)

* init user module events

* refactor utils

* undo test script update

* fix feedback

* add eventbus service to module test-runner

* add injected dependencies

* move events to utils

* use const eventname in tests

* rm withTransaction
This commit is contained in:
Philip Korsholm
2024-02-23 09:31:02 +08:00
committed by GitHub
parent 36a61658f9
commit 3fc2aea752
19 changed files with 289 additions and 11 deletions
@@ -1,7 +1,9 @@
import { IUserModuleService } from "@medusajs/types/dist/user"
import { MikroOrmWrapper } from "../../../utils"
import { MockEventBusService } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { UserEvents } from "@medusajs/utils"
import { createInvites } from "../../../__fixtures__/invite"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
@@ -44,6 +46,7 @@ describe("UserModuleService - Invite", () => {
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
testManager = MikroOrmWrapper.forkManager()
jest.clearAllMocks()
})
afterEach(async () => {
@@ -171,6 +174,30 @@ describe("UserModuleService - Invite", () => {
expect(error.message).toEqual('Invite with id "does-not-exist" not found')
})
it("should emit invite updated events", async () => {
await createInvites(testManager, defaultInviteData)
jest.clearAllMocks()
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.updateInvites([
{
id: "1",
accepted: true,
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_updated,
}),
])
})
})
describe("createInvitie", () => {
@@ -188,5 +215,26 @@ describe("UserModuleService - Invite", () => {
})
)
})
it("should emit invite created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.createInvites(defaultInviteData)
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.invite_created,
}),
])
})
})
})
@@ -1,7 +1,9 @@
import { IUserModuleService } from "@medusajs/types/dist/user"
import { MikroOrmWrapper } from "../../../utils"
import { MockEventBusService } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { UserEvents } from "@medusajs/utils"
import { createUsers } from "../../../__fixtures__/user"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
@@ -41,6 +43,7 @@ describe("UserModuleService - User", () => {
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
jest.clearAllMocks()
})
afterAll(async () => {
@@ -182,6 +185,30 @@ describe("UserModuleService - User", () => {
expect(error.message).toEqual('User with id "does-not-exist" not found')
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
jest.clearAllMocks()
await service.update([
{
id: "1",
first_name: "John",
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.updated,
}),
])
})
})
describe("create", () => {
@@ -199,5 +226,26 @@ describe("UserModuleService - User", () => {
})
)
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.created,
}),
])
})
})
})