Files
medusa-store/integration-tests/modules/__tests__/notification/admin/notification.spec.ts
Stevche Radevski ee924b1b28 feat: Add a simple configurable notifications subscriber (#7331)
* feat: Add a simple configurable notifications subscriber that is configurable

* Proposal on awaiting all subscribers to run

* fix: Clean up wait subscribers util and notifications test

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
2024-05-16 13:36:09 +02:00

196 lines
6.3 KiB
TypeScript

import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
CreateNotificationDTO,
IEventBusModuleService,
INotificationModuleService,
Logger,
} from "@medusajs/types"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { medusaIntegrationTestRunner, TestEventUtils } from "medusa-test-utils"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
medusaIntegrationTestRunner({
env,
testSuite: ({ getContainer }) => {
describe("Notifications", () => {
let service: INotificationModuleService
let logger: Logger
beforeAll(async () => {
service = getContainer().resolve(ModuleRegistrationName.NOTIFICATION)
logger = getContainer().resolve(ContainerRegistrationKeys.LOGGER)
})
afterEach(() => {
jest.restoreAllMocks()
})
describe("Notifications module", () => {
it("should successfully send a notification for an available channel", async () => {
const logSpy = jest.spyOn(logger, "info")
const notification = {
to: "test@medusajs.com",
channel: "email",
template: "order-created",
data: { username: "john-doe" },
trigger_type: "order-created",
resource_id: "order-id",
resource_type: "order",
} as CreateNotificationDTO
const result = await service.create(notification)
const fromDB = await service.retrieve(result.id)
expect(result).toEqual(
expect.objectContaining({
id: expect.any(String),
to: "test@medusajs.com",
provider_id: "local-notification-provider",
})
)
expect(result).toEqual(
expect.objectContaining({
to: "test@medusajs.com",
channel: "email",
data: {
username: "john-doe",
},
id: expect.any(String),
provider_id: "local-notification-provider",
resource_id: "order-id",
resource_type: "order",
template: "order-created",
trigger_type: "order-created",
})
)
expect(fromDB).toEqual(
expect.objectContaining({
to: "test@medusajs.com",
channel: "email",
data: {
username: "john-doe",
},
id: expect.any(String),
provider_id: "local-notification-provider",
resource_id: "order-id",
resource_type: "order",
template: "order-created",
trigger_type: "order-created",
})
)
expect(logSpy).toHaveBeenCalledWith(
`Attempting to send a notification to: 'test@medusajs.com' on the channel: 'email' with template: 'order-created' and data: '{\"username\":\"john-doe\"}'`
)
})
it("should throw an exception if there is no provider for the channel", async () => {
const notification = {
to: "test@medusajs.com",
channel: "sms",
} as CreateNotificationDTO
const error = await service.create(notification).catch((e) => e)
expect(error.message).toEqual(
"Could not find a notification provider for channel: sms"
)
})
it("should allow listing all notifications with filters", async () => {
const notification1 = {
to: "test@medusajs.com",
channel: "email",
template: "order-created",
} as CreateNotificationDTO
const notification2 = {
to: "test@medusajs.com",
channel: "log",
template: "product-created",
} as CreateNotificationDTO
await service.create([notification1, notification2])
const notifications = await service.list({ channel: "log" })
expect(notifications).toHaveLength(1)
expect(notifications[0]).toEqual(
expect.objectContaining({
to: "test@medusajs.com",
channel: "log",
template: "product-created",
})
)
})
it("should allow retrieving a notification", async () => {
const notification1 = {
to: "test@medusajs.com",
channel: "email",
template: "order-created",
} as CreateNotificationDTO
const notification2 = {
to: "test@medusajs.com",
channel: "log",
template: "product-created",
} as CreateNotificationDTO
const [first] = await service.create([notification1, notification2])
const notification = await service.retrieve(first.id)
expect(notification).toEqual(
expect.objectContaining({
to: "test@medusajs.com",
channel: "email",
template: "order-created",
})
)
})
})
describe("Configurable notification subscriber", () => {
let eventBus: IEventBusModuleService
beforeAll(async () => {
eventBus = getContainer().resolve(ModuleRegistrationName.EVENT_BUS)
})
it("should successfully sent a notification when an order is created (based on configuration)", async () => {
const subscriberExecution = TestEventUtils.waitSubscribersExecution(
"order.created",
eventBus
)
const logSpy = jest.spyOn(logger, "info")
await eventBus.emit("order.created", {
data: {
order: {
id: "1234",
email: "test@medusajs.com",
},
},
})
await subscriberExecution
const notifications = await service.list()
expect(logSpy).toHaveBeenLastCalledWith(
`Attempting to send a notification to: 'test@medusajs.com' on the channel: 'email' with template: 'order-created-template' and data: '{\"order_id\":\"1234\"}'`
)
expect(notifications).toHaveLength(1)
expect(notifications[0]).toEqual(
expect.objectContaining({
to: "test@medusajs.com",
channel: "email",
template: "order-created-template",
})
)
})
})
})
},
})