Skip to content

Commit

Permalink
[@dhealthdapps/backend] test(notifier): update & fix failed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nsonanh committed Jan 13, 2023
1 parent ef22a59 commit 3ae6466
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
57 changes: 41 additions & 16 deletions runtime/backend/tests/unit/notifier/listeners/AlertNotifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe("notifier/AlertNotifier", () => {
DappHelper, // requirement from AlertNotifier
NotifierFactory, // requirement from AlertNotifier
EmailNotifier, // requirement from AlertNotifier
ConfigService, // requirement from DappHelper
NetworkService, // requirement from DappHelper
MailerService, // requirement from EmailNotifier
{
Expand Down Expand Up @@ -118,16 +117,16 @@ describe("notifier/AlertNotifier", () => {
jest.clearAllMocks();
(service as any).alertsConfig = {
type: ["warn"],
transport: "mail",
recipient: "[email protected]",
};
const configServiceGetCall = jest
.spyOn(configService, "get")
.mockReturnValue("test-name");
.mockReturnValueOnce("test-dapp-name")
.mockReturnValueOnce("test-url");
const notifierSendInternalCall = jest
.spyOn(notifier, "sendInternal")
.mockResolvedValue();
const dappHelperCreateDetailsTableHTMLCall = jest
.spyOn(dappHelper, "createDetailsTableHTML")
.mockReturnValue("test-html-content");
const alertEvent: AlertEvent = {
timestamp: new Date(),
level: "warn",
Expand All @@ -141,8 +140,23 @@ describe("notifier/AlertNotifier", () => {

// assert
expect(configServiceGetCall).toHaveBeenNthCalledWith(1, "dappName");
expect(dappHelperCreateDetailsTableHTMLCall).toHaveBeenNthCalledWith(1, [alertEvent]);
expect(notifierSendInternalCall).toHaveBeenCalledTimes(1);
expect(notifierSendInternalCall).toHaveBeenNthCalledWith(1, {
to: "[email protected]",
subject: "[test-dapp-name] WARNING on dApp (test-url) at 1212-02-01 12:00:00",
context: {
alertLevel: "WARNING",
dappUrl: "test-url",
dateFormat: "1212-02-01 12:00:00",
details: {
context: "test-context",
level: "warn",
loggerContext: "test-logger-context",
message: "test-message",
timestamp: new Date(),
},
},
template: "AlertEmailTemplate",
});
});
});

Expand All @@ -159,9 +173,6 @@ describe("notifier/AlertNotifier", () => {
const notifierSendInternalCall = jest
.spyOn(notifier, "sendInternal")
.mockResolvedValue();
const dappHelperCreateDetailsTableHTMLCall = jest
.spyOn(dappHelper, "createDetailsTableHTML")
.mockReturnValue("test-html-content");
const alertEvent: AlertEvent = AlertEvent.create(
new Date(),
"error",
Expand All @@ -176,7 +187,6 @@ describe("notifier/AlertNotifier", () => {

// assert
expect(configServiceGetCall).toHaveBeenCalledTimes(0);
expect(dappHelperCreateDetailsTableHTMLCall).toHaveBeenCalledTimes(0);
expect(notifierSendInternalCall).toHaveBeenCalledTimes(0);
});

Expand All @@ -185,16 +195,15 @@ describe("notifier/AlertNotifier", () => {
jest.clearAllMocks();
(service as any).alertsConfig = {
type: ["error"],
transport: "mail",
recipient: "[email protected]",
};
const configServiceGetCall = jest
.spyOn(configService, "get")
.mockReturnValue("test-name");
const notifierSendInternalCall = jest
.spyOn(notifier, "sendInternal")
.mockResolvedValue();
const dappHelperCreateDetailsTableHTMLCall = jest
.spyOn(dappHelper, "createDetailsTableHTML")
.mockReturnValue("test-html-content");
const alertEvent: AlertEvent = {
timestamp: new Date(),
level: "error",
Expand All @@ -209,8 +218,24 @@ describe("notifier/AlertNotifier", () => {

// assert
expect(configServiceGetCall).toHaveBeenNthCalledWith(1, "dappName");
expect(dappHelperCreateDetailsTableHTMLCall).toHaveBeenNthCalledWith(1, [alertEvent]);
expect(notifierSendInternalCall).toHaveBeenCalledTimes(1);
expect(notifierSendInternalCall).toHaveBeenNthCalledWith(1, {
to: "[email protected]",
subject: "[test-name] ERROR on dApp (test-name) at 1212-02-01 12:00:00",
context: {
alertLevel: "ERROR",
dappUrl: "test-name",
dateFormat: "1212-02-01 12:00:00",
details: {
context: "test-context",
level: "error",
loggerContext: "test-logger-context",
message: "test-message",
timestamp: new Date(),
trace: "test-trace",
},
},
template: "AlertEmailTemplate",
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,14 @@ describe("notifier/ReportNotifier", () => {
{
to: "[email protected]",
subject: `[test-dapp-name] LOGS REPORT for dApp (test-url) from ${dateStart} to ${dateEnd}`,
text: `The production logs for test-dapp-name (test-url) from ${dateStart} to ${dateEnd} can be found as an attachment to this email.`,
html: `The production logs for test-dapp-name (test-url) from <b>${dateStart}</b> to <b>${dateEnd}</b> can be found as an attachment to this email.`,
context: {
alertLevel: "WARNING",
dappName: "test-dapp-name",
dappUrl: "test-url",
dateEnd: "2022-02-01",
dateStart: "2022-02-01",
},
template: "ReportEmailTemplate",
attachments: [
{
// binary buffer as an attachment
Expand Down

0 comments on commit 3ae6466

Please sign in to comment.