Files
Adrien de Peretti e8822f3e69 chore(): Module Internal Events (#13296)
* chore(): Ensure the product module emits all necessary events

* chore(): Ensure the product module emits all necessary events

* Update events tests

* more events and fixes

* more tests and category fixes

* more tests and category fixes

* Add todo

* update updateProduct_ event emitting and adjust test

* Adjust update products implementation to rely on already computed events

* rm unnecessary update variants events

* Fix formatting in changeset for product events

* refactor: Manage event emitting automatically (WIP)

* refactor: Manage event emitting automatically (WIP)

* chore(api-key): Add missing emit events and refactoring

* chore(cart): Add missing emit events and refactoring

* chore(customer): Add missing emit events and refactoring

* chore(fufillment, utils): Add missing emit events and refactoring

* chore(fufillment, utils): Add missing emit events and refactoring

* chore(inventory): Add missing emit events and refactoring

* chore(notification): Add missing emit events and refactoring

* chore(utils): Remove medusa service event handling legacy

* chore(product): Add missing emit events and refactoring

* chore(order): Add missing emit events and refactoring

* chore(payment): Add missing emit events and refactoring

* chore(pricing, util): Add missing emit events and refactoring, fix internal service upsertWithReplace event dispatching

* chore(promotions): Add missing emit events and refactoring

* chore(region): Add missing emit events and refactoring

* chore(sales-channel): Add missing emit events and refactoring

* chore(settings): Add missing emit events and refactoring

* chore(stock-location): Add missing emit events and refactoring

* chore(store): Add missing emit events and refactoring

* chore(taxes): Add missing emit events and refactoring

* chore(user): Add missing emit events and refactoring

* fix unit tests

* rm changeset for regeneration

* Create changeset for Medusa.js patch updates

Add a changeset for patch updates to multiple Medusa.js modules.

* rm unused product event builders

* address feedback

* remove old changeset

* fix event action for token generated

* fix user module events

* fix import

* fix promotion events

* add new module integration tests shard

* fix medusa service

* revert shard

* fix event action

* fix pipeline

* fix pipeline

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
2025-09-10 14:37:38 +02:00

278 lines
7.1 KiB
TypeScript

import { IUserModuleService } from "@medusajs/framework/types"
import { Module, Modules, UserEvents } from "@medusajs/framework/utils"
import { UserModuleService } from "@services"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
jest.setTimeout(30000)
const defaultUserData = [
{
id: "1",
email: "user_1@test.com",
},
{
id: "2",
email: "user_2@test.com",
},
]
moduleIntegrationTestRunner<IUserModuleService>({
moduleName: Modules.USER,
moduleOptions: {
jwt_secret: "test",
},
injectedDependencies: {
[Modules.EVENT_BUS]: new MockEventBusService(),
},
testSuite: ({ service }) => {
it(`should export the appropriate linkable configuration`, () => {
const linkable = Module(Modules.USER, {
service: UserModuleService,
}).linkable
expect(Object.keys(linkable)).toEqual(["user", "invite"])
Object.keys(linkable).forEach((key) => {
delete linkable[key].toJSON
})
expect(linkable).toEqual({
user: {
id: {
linkable: "user_id",
entity: "User",
primaryKey: "id",
serviceName: "user",
field: "user",
},
},
invite: {
id: {
linkable: "invite_id",
entity: "Invite",
primaryKey: "id",
serviceName: "user",
field: "invite",
},
},
})
})
describe("UserModuleService - User", () => {
afterEach(async () => {
jest.clearAllMocks()
})
describe("list", () => {
it("should list users", async () => {
await service.createUsers(defaultUserData)
const users = await service.listUsers()
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list users by id", async () => {
await service.createUsers(defaultUserData)
const users = await service.listUsers({
id: ["1"],
})
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("listAndCount", () => {
it("should list and count users", async () => {
await service.createUsers(defaultUserData)
const [users, count] = await service.listAndCountUsers()
expect(count).toEqual(2)
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list and count users by id", async () => {
await service.createUsers(defaultUserData)
const [Users, count] = await service.listAndCountUsers({
id: "1",
})
expect(count).toEqual(1)
expect(Users).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("retrieve", () => {
const id = "1"
it("should return an user for the given id", async () => {
await service.createUsers(defaultUserData)
const user = await service.retrieveUser(id)
expect(user).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when an user with the given id does not exist", async () => {
const error = await service
.retrieveUser("does-not-exist")
.catch((e) => e)
expect(error.message).toEqual(
"User with id: does-not-exist was not found"
)
})
it("should throw an error when a userId is not provided", async () => {
const error = await service
.retrieveUser(undefined as unknown as string)
.catch((e) => e)
expect(error.message).toEqual("user - id must be defined")
})
it("should return user based on config select param", async () => {
await service.createUsers(defaultUserData)
const User = await service.retrieveUser(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(User))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "1"
it("should delete the users given an id successfully", async () => {
await service.createUsers(defaultUserData)
await service.deleteUsers([id])
const users = await service.listUsers({
id: [id],
})
expect(users).toHaveLength(0)
})
})
describe("update", () => {
it("should throw an error when a id does not exist", async () => {
const error = await service
.updateUsers([
{
id: "does-not-exist",
},
])
.catch((e) => e)
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.createUsers(defaultUserData)
jest.clearAllMocks()
await service.updateUsers([
{
id: "1",
first_name: "John",
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
expect.objectContaining({
data: { id: "1" },
name: UserEvents.USER_UPDATED,
}),
],
{
internal: true,
}
)
})
})
describe("create", () => {
it("should create a user successfully", async () => {
await service.createUsers(defaultUserData)
const [User, count] = await service.listAndCountUsers({
id: ["1"],
})
expect(count).toEqual(1)
expect(User[0]).toEqual(
expect.objectContaining({
id: "1",
})
)
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.createUsers(defaultUserData)
// 2 events: 2 user created
expect(eventBusSpy.mock.calls[0][0]).toHaveLength(2)
const events = eventBusSpy.mock.calls[0][0]
expect(events).toHaveLength(2)
expect(events).toEqual(
expect.arrayContaining([
expect.objectContaining({
data: { id: "1" },
name: UserEvents.USER_CREATED,
}),
expect.objectContaining({
data: { id: "2" },
name: UserEvents.USER_CREATED,
}),
])
)
})
})
})
},
})