From 81b973a01134bfaba4983735a30076b0d4ad548e Mon Sep 17 00:00:00 2001 From: Shaquille Hobbs-Daley Date: Thu, 16 May 2024 11:22:32 +0100 Subject: [PATCH] feat: add fn that sends a fetch req --- src/reportToSlack/index.test.ts | 72 +++++++++++++++++++++++++++++++++ src/reportToSlack/index.ts | 44 ++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/reportToSlack/index.test.ts create mode 100644 src/reportToSlack/index.ts diff --git a/src/reportToSlack/index.test.ts b/src/reportToSlack/index.test.ts new file mode 100644 index 0000000..5d296dc --- /dev/null +++ b/src/reportToSlack/index.test.ts @@ -0,0 +1,72 @@ +import { createSlackReport } from "./index"; + +describe("createSlackReport", () => { + it("should send a report to Slack", async () => { + const slackUrl = "https://slack.com/api/chat.postMessage"; + const message = "Test message"; + const additionalInfo = { key: "value" }; + + // Mock the fetch function + const mockFetch = jest.fn().mockResolvedValue({ + ok: true, + json: jest.fn().mockResolvedValue({ success: true }), + }); + global.fetch = mockFetch; + + const reportToSlack = createSlackReport(slackUrl); + await reportToSlack(message, additionalInfo); + + expect(mockFetch).toHaveBeenCalledWith(slackUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + blocks: [ + { + type: "section", + text: { + type: "plain_text", + text: message, + emoji: true, + }, + }, + { + type: "rich_text", + elements: [ + { + type: "rich_text_preformatted", + elements: [ + { + type: "text", + text: additionalInfo, + }, + ], + }, + ], + }, + ], + }), + }); + }); + + it("should throw an error if sending report to Slack fails", async () => { + const slackUrl = "https://slack.com/api/chat.postMessage"; + const message = "Test message"; + const additionalInfo = { key: "value" }; + + // Mock the fetch function to simulate a failed request + const mockFetch = jest.fn().mockResolvedValue({ + ok: false, + status: 500, + text: jest.fn().mockResolvedValue("Internal Server Error"), + }); + global.fetch = mockFetch; + + const reportToSlack = createSlackReport(slackUrl); + + await expect(reportToSlack(message, additionalInfo)).rejects.toThrow( + "Failed to send report to Slack: 500 Internal Server Error" + ); + }); +}); diff --git a/src/reportToSlack/index.ts b/src/reportToSlack/index.ts new file mode 100644 index 0000000..816811e --- /dev/null +++ b/src/reportToSlack/index.ts @@ -0,0 +1,44 @@ +export const createSlackReport = (slackUrl: string) => { + return async (message: string, additionalInfo: Record) => { + const res = await fetch(slackUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + blocks: [ + { + type: "section", + text: { + type: "plain_text", + text: message, + emoji: true, + }, + }, + { + type: "rich_text", + elements: [ + { + type: "rich_text_preformatted", + elements: [ + { + type: "text", + text: additionalInfo, + }, + ], + }, + ], + }, + ], + }), + }); + + if (!res.ok) { + throw new Error( + `Failed to send report to Slack: ${res.status} ${await res.text()}` + ); + } + + return res.json(); + }; +};