|
| 1 | +import { describe, it, assert } from "../testrunner.ts"; |
| 2 | +import { validateEventName } from "../../src/event-handler/validate-event-name.ts"; |
| 3 | + |
| 4 | +describe("validateEventName", () => { |
| 5 | + it("validates 'push'", () => { |
| 6 | + validateEventName("push"); |
| 7 | + }); |
| 8 | + |
| 9 | + [null, undefined, {}, [], true, false, 1].forEach((value) => { |
| 10 | + it(`throws on invalid event name data type - ${JSON.stringify(value)}`, () => { |
| 11 | + try { |
| 12 | + validateEventName(value as any); |
| 13 | + throw new Error("Should have thrown"); |
| 14 | + } catch (error) { |
| 15 | + assert(error instanceof TypeError === true); |
| 16 | + assert((error as Error).message === "eventName must be of type string"); |
| 17 | + } |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('throws on invalid event name - "error"', () => { |
| 22 | + try { |
| 23 | + validateEventName("error"); |
| 24 | + throw new Error("Should have thrown"); |
| 25 | + } catch (error) { |
| 26 | + assert(error instanceof TypeError === true); |
| 27 | + assert( |
| 28 | + (error as Error).message === |
| 29 | + 'Using the "error" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onError() method instead', |
| 30 | + ); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + it('throws on invalid event name - "error" with onUnkownEventName set to "warn"', () => { |
| 35 | + try { |
| 36 | + validateEventName("error", { onUnknownEventName: "warn" }); |
| 37 | + throw new Error("Should have thrown"); |
| 38 | + } catch (error) { |
| 39 | + assert(error instanceof TypeError === true); |
| 40 | + assert( |
| 41 | + (error as Error).message === |
| 42 | + 'Using the "error" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onError() method instead', |
| 43 | + ); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it('throws on invalid event name - "error" with onUnkownEventName set to "ignore"', () => { |
| 48 | + try { |
| 49 | + validateEventName("error", { onUnknownEventName: "ignore" }); |
| 50 | + throw new Error("Should have thrown"); |
| 51 | + } catch (error) { |
| 52 | + assert(error instanceof TypeError === true); |
| 53 | + assert( |
| 54 | + (error as Error).message === |
| 55 | + 'Using the "error" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onError() method instead', |
| 56 | + ); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it('throws on invalid event name - "*"', () => { |
| 61 | + try { |
| 62 | + validateEventName("*"); |
| 63 | + throw new Error("Should have thrown"); |
| 64 | + } catch (error) { |
| 65 | + assert(error instanceof TypeError === true); |
| 66 | + assert( |
| 67 | + (error as Error).message === |
| 68 | + 'Using the "*" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onAny() method instead', |
| 69 | + ); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it('throws on invalid event name - "*" with onUnkownEventName set to "warn"', () => { |
| 74 | + try { |
| 75 | + validateEventName("*", { onUnknownEventName: "warn" }); |
| 76 | + throw new Error("Should have thrown"); |
| 77 | + } catch (error) { |
| 78 | + assert(error instanceof TypeError === true); |
| 79 | + assert( |
| 80 | + (error as Error).message === |
| 81 | + 'Using the "*" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onAny() method instead', |
| 82 | + ); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('throws on invalid event name - "*" with onUnkownEventName set to "ignore"', () => { |
| 87 | + try { |
| 88 | + validateEventName("*", { onUnknownEventName: "ignore" }); |
| 89 | + throw new Error("Should have thrown"); |
| 90 | + } catch (error) { |
| 91 | + assert(error instanceof TypeError === true); |
| 92 | + assert( |
| 93 | + (error as Error).message === |
| 94 | + 'Using the "*" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.onAny() method instead', |
| 95 | + ); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it('throws on invalid event name - "invalid"', () => { |
| 100 | + try { |
| 101 | + validateEventName("invalid"); |
| 102 | + throw new Error("Should have thrown"); |
| 103 | + } catch (error) { |
| 104 | + assert(error instanceof TypeError === true); |
| 105 | + assert( |
| 106 | + (error as Error).message === |
| 107 | + '"invalid" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)', |
| 108 | + ); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it('logs on invalid event name - "invalid" and onUnknownEventName is "warn" - console.warn', () => { |
| 113 | + const consoleWarn = console.warn; |
| 114 | + const logWarnCalls: string[] = []; |
| 115 | + console.warn = Array.prototype.push.bind(logWarnCalls); |
| 116 | + |
| 117 | + validateEventName("invalid", { onUnknownEventName: "warn" }); |
| 118 | + try { |
| 119 | + assert(logWarnCalls.length === 1); |
| 120 | + assert( |
| 121 | + logWarnCalls[0] === |
| 122 | + '"invalid" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)', |
| 123 | + ); |
| 124 | + } catch (error) { |
| 125 | + throw error; |
| 126 | + } finally { |
| 127 | + console.warn = consoleWarn; // restore original console.warn |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + it('logs on invalid event name - "invalid" and onUnknownEventName is "warn" - custom logger', () => { |
| 132 | + const logWarnCalls: string[] = []; |
| 133 | + const log = { |
| 134 | + warn: (message: string) => { |
| 135 | + logWarnCalls.push(message); |
| 136 | + }, |
| 137 | + }; |
| 138 | + validateEventName("invalid", { onUnknownEventName: "warn", log }); |
| 139 | + assert(logWarnCalls.length === 1); |
| 140 | + assert( |
| 141 | + logWarnCalls[0] === |
| 142 | + '"invalid" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)', |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it('logs nothing on invalid event name - "invalid" and onUnknownEventName is "ignore" - custom logger', () => { |
| 147 | + const logWarnCalls: string[] = []; |
| 148 | + const log = { |
| 149 | + warn: (message: string) => { |
| 150 | + logWarnCalls.push(message); |
| 151 | + }, |
| 152 | + }; |
| 153 | + validateEventName("invalid", { onUnknownEventName: "ignore", log }); |
| 154 | + assert(logWarnCalls.length === 0); |
| 155 | + }); |
| 156 | +}); |
0 commit comments