Files
medusa-store/integration-tests/modules/__tests__/notification/admin/notification.spec.ts
Adrien de Peretti 48963f55ef Chore/rm main entity concept (#7709)
**What**
Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate.
This pr also includes various fixes in different modules

Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
2024-06-19 13:02:16 +00:00

204 lines
6.5 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.createNotifications(notification)
const fromDB = await service.retrieveNotification(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
.createNotifications(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.createNotifications([notification1, notification2])
const notifications = await service.listNotifications({
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.createNotifications([
notification1,
notification2,
])
const notification = await service.retrieveNotification(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({
eventName: "order.created",
data: {
order: {
id: "1234",
email: "test@medusajs.com",
},
},
})
await subscriberExecution
const notifications = await service.listNotifications()
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",
})
)
})
})
})
},
})