From f989c334f82905d450604eada6e638f570c7765c Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 2 Aug 2024 09:35:47 +0200 Subject: [PATCH] #10 - validate the link values from the function + tests --- src/processResults.test.ts | 120 +++++++++++++++++++++++++++++++++++++ src/processResults.ts | 24 ++++---- 2 files changed, 134 insertions(+), 10 deletions(-) diff --git a/src/processResults.test.ts b/src/processResults.test.ts index 001fc5b..1aa5deb 100644 --- a/src/processResults.test.ts +++ b/src/processResults.test.ts @@ -345,6 +345,58 @@ describe("processResults", () => { consoleLogSpy.mockReset(); }); + it("should not include the link from the function when not a string (number)", async () => { + const fakeLink = 123; + const consoleLogSpy = jest + .spyOn(console, "log") + .mockImplementation((message) => { + if (message.includes("message") && !message.includes(fakeLink)) { + console.log(`Did not include ${fakeLink}`); + } + }); + const fetchMock = jest + .fn() + .mockResolvedValue({ ok: true, text: () => "1" }); + global.fetch = fetchMock; + const options: MsTeamsReporterOptions = { + ...DEFAULT_OPTIONS, + webhookUrl: FLOW_WEBHOOK_URL, + webhookType: "powerautomate", + linkToResultsUrl: (): any => fakeLink, + debug: true, + }; + await processResults(SUITE_MOCK_FAILED as any, options); + expect(consoleLogSpy).toHaveBeenCalledWith(`Did not include ${fakeLink}`); + + consoleLogSpy.mockReset(); + }); + + it("should not include the link from the function when not a string (undefined)", async () => { + const fakeLink = undefined; + const consoleLogSpy = jest + .spyOn(console, "log") + .mockImplementation((message) => { + if (message.includes("message") && !message.includes(fakeLink)) { + console.log(`Did not include ${fakeLink}`); + } + }); + const fetchMock = jest + .fn() + .mockResolvedValue({ ok: true, text: () => "1" }); + global.fetch = fetchMock; + const options: MsTeamsReporterOptions = { + ...DEFAULT_OPTIONS, + webhookUrl: FLOW_WEBHOOK_URL, + webhookType: "powerautomate", + linkToResultsUrl: (): any => fakeLink, + debug: true, + }; + await processResults(SUITE_MOCK_FAILED as any, options); + expect(consoleLogSpy).toHaveBeenCalledWith(`Did not include ${fakeLink}`); + + consoleLogSpy.mockReset(); + }); + it("should include the failure link", async () => { const fakeFailureLink = "https://github.com/estruyf/playwright-msteams-reporter"; @@ -411,6 +463,74 @@ describe("processResults", () => { consoleLogSpy.mockReset(); }); + it("should not include the failure link from the function when not a string (number)", async () => { + const fakeFailureLink = 123; + const fakeFailureText = "View the failed tests"; + const consoleLogSpy = jest + .spyOn(console, "log") + .mockImplementation((message) => { + if ( + message.includes("message") && + !message.includes(fakeFailureLink) && + !message.includes(fakeFailureText) + ) { + console.log(`Did not include ${fakeFailureLink}`); + } + }); + const fetchMock = jest + .fn() + .mockResolvedValue({ ok: true, text: () => "1" }); + global.fetch = fetchMock; + const options: MsTeamsReporterOptions = { + ...DEFAULT_OPTIONS, + webhookUrl: FLOW_WEBHOOK_URL, + webhookType: "powerautomate", + linkUrlOnFailure: (): any => fakeFailureLink, + linkTextOnFailure: "View the failed tests", + debug: true, + }; + await processResults(SUITE_MOCK_FAILED as any, options); + expect(consoleLogSpy).toHaveBeenCalledWith( + `Did not include ${fakeFailureLink}` + ); + + consoleLogSpy.mockReset(); + }); + + it("should not include the failure link from the function when not a string (undefined)", async () => { + const fakeFailureLink = undefined; + const fakeFailureText = "View the failed tests"; + const consoleLogSpy = jest + .spyOn(console, "log") + .mockImplementation((message) => { + if ( + message.includes("message") && + !message.includes(fakeFailureLink) && + !message.includes(fakeFailureText) + ) { + console.log(`Did not include ${fakeFailureLink}`); + } + }); + const fetchMock = jest + .fn() + .mockResolvedValue({ ok: true, text: () => "1" }); + global.fetch = fetchMock; + const options: MsTeamsReporterOptions = { + ...DEFAULT_OPTIONS, + webhookUrl: FLOW_WEBHOOK_URL, + webhookType: "powerautomate", + linkUrlOnFailure: (): any => fakeFailureLink, + linkTextOnFailure: "View the failed tests", + debug: true, + }; + await processResults(SUITE_MOCK_FAILED as any, options); + expect(consoleLogSpy).toHaveBeenCalledWith( + `Did not include ${fakeFailureLink}` + ); + + consoleLogSpy.mockReset(); + }); + it("should show debug message", async () => { const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(); const fetchMock = jest diff --git a/src/processResults.ts b/src/processResults.ts index 46fc69b..310495e 100644 --- a/src/processResults.ts +++ b/src/processResults.ts @@ -146,11 +146,13 @@ export const processResults = async ( linkToResultsUrl = options.linkToResultsUrl(); } - adaptiveCard.actions.push({ - type: "Action.OpenUrl", - title: options.linkToResultsText, - url: linkToResultsUrl, - }); + if (linkToResultsUrl && typeof linkToResultsUrl === "string") { + adaptiveCard.actions.push({ + type: "Action.OpenUrl", + title: options.linkToResultsText, + url: linkToResultsUrl, + }); + } } if (!isSuccess && options.linkTextOnFailure && options.linkUrlOnFailure) { @@ -161,11 +163,13 @@ export const processResults = async ( linkUrlOnFailure = options.linkUrlOnFailure(); } - adaptiveCard.actions.push({ - type: "Action.OpenUrl", - title: options.linkTextOnFailure, - url: linkUrlOnFailure, - }); + if (linkUrlOnFailure && typeof linkUrlOnFailure === "string") { + adaptiveCard.actions.push({ + type: "Action.OpenUrl", + title: options.linkTextOnFailure, + url: linkUrlOnFailure, + }); + } } if (options.webhookType === "powerautomate") {