Skip to content

Commit

Permalink
#10 - validate the link values from the function + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Aug 2, 2024
1 parent 9a242af commit f989c33
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 10 deletions.
120 changes: 120 additions & 0 deletions src/processResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions src/processResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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") {
Expand Down

0 comments on commit f989c33

Please sign in to comment.