|
| 1 | +import config from "../../../config/config"; |
| 2 | +import JSONResponse from "../../../src/utils/JsonResponse"; |
| 3 | +import { sendTaskUpdate } from "../../../src/utils/sendTaskUpdates"; |
| 4 | + |
| 5 | +describe("sendTaskUpdate function", () => { |
| 6 | + const mockEnv = { DISCORD_TOKEN: "mockToken" }; |
| 7 | + const completed = "Task completed successfully"; |
| 8 | + const planned = "Plan for the next phase"; |
| 9 | + const blockers = "No blockers"; |
| 10 | + |
| 11 | + const assertFetchCall = (url: string, bodyObj: any, mockEnv: any) => { |
| 12 | + expect(global.fetch).toHaveBeenCalledWith(url, { |
| 13 | + method: "POST", |
| 14 | + headers: { |
| 15 | + "Content-Type": "application/json", |
| 16 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 17 | + }, |
| 18 | + body: JSON.stringify(bodyObj), |
| 19 | + }); |
| 20 | + }; |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + jest.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + test("should send the task update to discord tracking channel when all fields are present", async () => { |
| 27 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 28 | + const formattedString = `**Completed**: ${completed}\n\n**Planned**: ${planned}\n\n**Blockers**: ${blockers}`; |
| 29 | + const bodyObj = { |
| 30 | + content: formattedString, |
| 31 | + }; |
| 32 | + |
| 33 | + jest |
| 34 | + .spyOn(global, "fetch") |
| 35 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 36 | + |
| 37 | + await sendTaskUpdate(completed, planned, blockers, mockEnv); |
| 38 | + |
| 39 | + assertFetchCall(url, bodyObj, mockEnv); |
| 40 | + }); |
| 41 | + |
| 42 | + test("should send the task update to discord tracking channel when only completed is present", async () => { |
| 43 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 44 | + const formattedString = `**Completed**: ${completed}\n\n**Planned**: \n\n**Blockers**: `; |
| 45 | + const bodyObj = { |
| 46 | + content: formattedString, |
| 47 | + }; |
| 48 | + |
| 49 | + jest |
| 50 | + .spyOn(global, "fetch") |
| 51 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 52 | + |
| 53 | + await sendTaskUpdate(completed, "", "", mockEnv); |
| 54 | + |
| 55 | + assertFetchCall(url, bodyObj, mockEnv); |
| 56 | + }); |
| 57 | + |
| 58 | + test("should send the task update to discord tracking channel when only planned is present", async () => { |
| 59 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 60 | + const formattedString = `**Completed**: \n\n**Planned**: ${planned}\n\n**Blockers**: `; |
| 61 | + const bodyObj = { |
| 62 | + content: formattedString, |
| 63 | + }; |
| 64 | + |
| 65 | + jest |
| 66 | + .spyOn(global, "fetch") |
| 67 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 68 | + |
| 69 | + await sendTaskUpdate("", planned, "", mockEnv); |
| 70 | + |
| 71 | + assertFetchCall(url, bodyObj, mockEnv); |
| 72 | + }); |
| 73 | + |
| 74 | + test("should send the task update to discord tracking channel when only blockers is present", async () => { |
| 75 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 76 | + const formattedString = `**Completed**: \n\n**Planned**: \n\n**Blockers**: ${blockers}`; |
| 77 | + const bodyObj = { |
| 78 | + content: formattedString, |
| 79 | + }; |
| 80 | + |
| 81 | + jest |
| 82 | + .spyOn(global, "fetch") |
| 83 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 84 | + |
| 85 | + await sendTaskUpdate("", "", blockers, mockEnv); |
| 86 | + |
| 87 | + assertFetchCall(url, bodyObj, mockEnv); |
| 88 | + }); |
| 89 | + |
| 90 | + test("should send the task update to discord tracking channel when only completed and planned are present", async () => { |
| 91 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 92 | + const formattedString = `**Completed**: ${completed}\n\n**Planned**: ${planned}\n\n**Blockers**: `; |
| 93 | + const bodyObj = { |
| 94 | + content: formattedString, |
| 95 | + }; |
| 96 | + |
| 97 | + jest |
| 98 | + .spyOn(global, "fetch") |
| 99 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 100 | + |
| 101 | + await sendTaskUpdate(completed, planned, "", mockEnv); |
| 102 | + |
| 103 | + assertFetchCall(url, bodyObj, mockEnv); |
| 104 | + }); |
| 105 | + |
| 106 | + test("should send the task update to discord tracking channel when only completed and blockers are present", async () => { |
| 107 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 108 | + const formattedString = `**Completed**: ${completed}\n\n**Planned**: \n\n**Blockers**: ${blockers}`; |
| 109 | + const bodyObj = { |
| 110 | + content: formattedString, |
| 111 | + }; |
| 112 | + |
| 113 | + jest |
| 114 | + .spyOn(global, "fetch") |
| 115 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 116 | + |
| 117 | + await sendTaskUpdate(completed, "", blockers, mockEnv); |
| 118 | + |
| 119 | + assertFetchCall(url, bodyObj, mockEnv); |
| 120 | + }); |
| 121 | + |
| 122 | + test("should send the task update to discord tracking channel when only planned and blockers are present", async () => { |
| 123 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 124 | + const formattedString = `**Completed**: \n\n**Planned**: ${planned}\n\n**Blockers**: ${blockers}`; |
| 125 | + const bodyObj = { |
| 126 | + content: formattedString, |
| 127 | + }; |
| 128 | + |
| 129 | + jest |
| 130 | + .spyOn(global, "fetch") |
| 131 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 132 | + |
| 133 | + await sendTaskUpdate("", planned, blockers, mockEnv); |
| 134 | + |
| 135 | + assertFetchCall(url, bodyObj, mockEnv); |
| 136 | + }); |
| 137 | +}); |
0 commit comments