1 import {
2 CreateNotificationDTO,
3 IEventBusModuleService,
4 INotificationModuleService,
5 Logger,
6 } from "@medusajs/types"
7 import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
8 import {
9 TestEventUtils,
10 medusaIntegrationTestRunner,
11 } from "@medusajs/test-utils"
12
13 jest.setTimeout(50000)
14
15 medusaIntegrationTestRunner({
16 testSuite: ({ getContainer }) => {
17 describe("Notifications", () => {
18 let service: INotificationModuleService
19 let logger: Logger
20
21 beforeAll(async () => {
22 service = getContainer().resolve(Modules.NOTIFICATION)
23 logger = getContainer().resolve(ContainerRegistrationKeys.LOGGER)
24 })
25
26 afterEach(() => {
27 jest.restoreAllMocks()
28 })
29
30 describe("Notifications module", () => {
31 it("should successfully send a notification for an available channel", async () => {
32 const logSpy = jest.spyOn(logger, "info")
33 const notification = {
34 to: "test@medusajs.com",
35 channel: "email",
36 template: "order-created",
37 data: { username: "john-doe" },
38 trigger_type: "order-created",
39 resource_id: "order-id",
40 resource_type: "order",
41 content: {
42 subject: "We received you order",
43 html: "<p>Thank you<p>",
44 },
45 } as CreateNotificationDTO
46
47 const result = await service.createNotifications(notification)
48 const fromDB = await service.retrieveNotification(result.id)
49
50 expect(result).toEqual(
51 expect.objectContaining({
52 id: expect.any(String),
53 to: "test@medusajs.com",
54 provider_id: "local-notification-provider",
55 })
56 )
57
58 expect(result).toEqual(
59 expect.objectContaining({
60 to: "test@medusajs.com",
61 channel: "email",
62 data: {
63 username: "john-doe",
64 },
65 id: expect.any(String),
66 provider_id: "local-notification-provider",
67 resource_id: "order-id",
68 resource_type: "order",
69 template: "order-created",
70 trigger_type: "order-created",
71 })
72 )
73
74 expect(result).toHaveProperty("content")
75
76 expect(fromDB).toEqual(
77 expect.objectContaining({
78 to: "test@medusajs.com",
79 channel: "email",
80 data: {
81 username: "john-doe",
82 },
83 id: expect.any(String),
84 provider_id: "local-notification-provider",
85 resource_id: "order-id",
86 resource_type: "order",
87 template: "order-created",
88 trigger_type: "order-created",
89 })
90 )
91
92 expect(fromDB).not.toHaveProperty("content")
93
94 expect(logSpy).toHaveBeenCalledWith(
95 `Attempting to send a notification to: 'test@medusajs.com' on the channel: 'email' with template: 'order-created' and data: '{\"username\":\"john-doe\"}'`
96 )
97 })
98
99 it("should throw an exception if there is no provider for the channel", async () => {
100 const notification = {
101 to: "test@medusajs.com",
102 template: "order-created",
103 channel: "sms",
104 } as CreateNotificationDTO
105
106 const error = await service
107 .createNotifications(notification)
108 .catch((e) => e)
109
110 const [notificationResult] = await service.listNotifications()
111
112 expect(error.message).toEqual(
113 `Could not find a notification provider for channel: sms for notification id ${notificationResult.id}`
114 )
115 })
116
117 it("should allow listing all notifications with filters", async () => {
118 const notification1 = {
119 to: "test@medusajs.com",
120 channel: "email",
121 template: "order-created",
122 } as CreateNotificationDTO
123
124 const notification2 = {
125 to: "test@medusajs.com",
126 channel: "log",
127 template: "product-created",
128 } as CreateNotificationDTO
129
130 await service.createNotifications([notification1, notification2])
131
132 const notifications = await service.listNotifications({
133 channel: "log",
134 })
135 expect(notifications).toHaveLength(1)
136 expect(notifications[0]).toEqual(
137 expect.objectContaining({
138 to: "test@medusajs.com",
139 channel: "log",
140 template: "product-created",
141 })
142 )
143 })
144
145 it("should allow retrieving a notification", async () => {
146 const notification1 = {
147 to: "test@medusajs.com",
148 channel: "email",
149 template: "order-created",
150 } as CreateNotificationDTO
151
152 const notification2 = {
153 to: "test@medusajs.com",
154 channel: "log",
155 template: "product-created",
156 } as CreateNotificationDTO
157
158 const [first] = await service.createNotifications([
159 notification1,
160 notification2,
161 ])
162
163 const notification = await service.retrieveNotification(first.id)
164 expect(notification).toEqual(
165 expect.objectContaining({
166 to: "test@medusajs.com",
167 channel: "email",
168 template: "order-created",
169 })
170 )
171 })
172 })
173
174 describe("Configurable notification subscriber", () => {
175 let eventBus: IEventBusModuleService
176 beforeAll(async () => {
177 eventBus = getContainer().resolve(Modules.EVENT_BUS)
178 })
179
180 it("should successfully sent a notification when an order is created (based on configuration)", async () => {
181 const subscriberExecution = TestEventUtils.waitSubscribersExecution(
182 "order.created",
183 eventBus
184 )
185 const logSpy = jest.spyOn(logger, "info")
186
187 await eventBus.emit({
188 name: "order.created",
189 data: {
190 order: {
191 id: "1234",
192 email: "test@medusajs.com",
193 },
194 },
195 })
196 await subscriberExecution
197
198 const notifications = await service.listNotifications()
199
200 expect(logSpy).toHaveBeenCalledWith(
201 `Attempting to send a notification to: 'test@medusajs.com' on the channel: 'email' with template: 'order-created-template' and data: '{\"order_id\":\"1234\"}'`
202 )
203 expect(notifications).toHaveLength(1)
204 expect(notifications[0]).toEqual(
205 expect.objectContaining({
206 to: "test@medusajs.com",
207 channel: "email",
208 template: "order-created-template",
209 })
210 )
211 })
212 })
213 })
214 },
215 })