|
| 1 | +import { EventEmitter } from "events"; |
| 2 | +import { onCorrect, onceCorrect } from "../type-enforced-ipc"; |
| 3 | + |
| 4 | +describe("type enforced ipc tests", () => { |
| 5 | + describe("onCorrect tests", () => { |
| 6 | + it("should call the handler if the args are valid", () => { |
| 7 | + let called = false; |
| 8 | + const source = new EventEmitter(); |
| 9 | + const listener = () => called = true; |
| 10 | + const verifier = (args: unknown[]): args is [] => true; |
| 11 | + const channel = "foobar"; |
| 12 | + |
| 13 | + onCorrect({ source, listener, verifier, channel }); |
| 14 | + |
| 15 | + source.emit(channel); |
| 16 | + expect(called).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should not call the handler if the args are not valid", () => { |
| 20 | + let called = false; |
| 21 | + const source = new EventEmitter(); |
| 22 | + const listener = () => called = true; |
| 23 | + const verifier = (args: unknown[]): args is [] => false; |
| 24 | + const channel = "foobar"; |
| 25 | + |
| 26 | + onCorrect({ source, listener, verifier, channel }); |
| 27 | + |
| 28 | + source.emit(channel); |
| 29 | + expect(called).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should call the handler twice if the args are valid on two emits", () => { |
| 33 | + let called = 0; |
| 34 | + const source = new EventEmitter(); |
| 35 | + const listener = () => called += 1; |
| 36 | + const verifier = (args: unknown[]): args is [] => true; |
| 37 | + const channel = "foobar"; |
| 38 | + |
| 39 | + onCorrect({ source, listener, verifier, channel }); |
| 40 | + |
| 41 | + source.emit(channel); |
| 42 | + source.emit(channel); |
| 43 | + expect(called).toBe(2); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should call the handler twice if the args are [valid, invalid, valid]", () => { |
| 47 | + let called = 0; |
| 48 | + const source = new EventEmitter(); |
| 49 | + const listener = () => called += 1; |
| 50 | + const results = [true, false, true]; |
| 51 | + const verifier = (args: unknown[]): args is [] => results.pop(); |
| 52 | + const channel = "foobar"; |
| 53 | + |
| 54 | + onCorrect({ source, listener, verifier, channel }); |
| 55 | + |
| 56 | + source.emit(channel); |
| 57 | + source.emit(channel); |
| 58 | + source.emit(channel); |
| 59 | + expect(called).toBe(2); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("onceCorrect tests", () => { |
| 64 | + it("should call the handler if the args are valid", () => { |
| 65 | + let called = false; |
| 66 | + const source = new EventEmitter(); |
| 67 | + const listener = () => called = true; |
| 68 | + const verifier = (args: unknown[]): args is [] => true; |
| 69 | + const channel = "foobar"; |
| 70 | + |
| 71 | + onceCorrect({ source, listener, verifier, channel }); |
| 72 | + |
| 73 | + source.emit(channel); |
| 74 | + expect(called).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should not call the handler if the args are not valid", () => { |
| 78 | + let called = false; |
| 79 | + const source = new EventEmitter(); |
| 80 | + const listener = () => called = true; |
| 81 | + const verifier = (args: unknown[]): args is [] => false; |
| 82 | + const channel = "foobar"; |
| 83 | + |
| 84 | + onceCorrect({ source, listener, verifier, channel }); |
| 85 | + |
| 86 | + source.emit(channel); |
| 87 | + expect(called).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should call the handler only once even if args are valid multiple times", () => { |
| 91 | + let called = 0; |
| 92 | + const source = new EventEmitter(); |
| 93 | + const listener = () => called += 1; |
| 94 | + const verifier = (args: unknown[]): args is [] => true; |
| 95 | + const channel = "foobar"; |
| 96 | + |
| 97 | + onceCorrect({ source, listener, verifier, channel }); |
| 98 | + |
| 99 | + source.emit(channel); |
| 100 | + source.emit(channel); |
| 101 | + expect(called).toBe(1); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should call the handler on only the first valid set of args", () => { |
| 105 | + let called = ""; |
| 106 | + let verifierCalled = 0; |
| 107 | + const source = new EventEmitter(); |
| 108 | + const listener = (info: any, arg: string) => called = arg; |
| 109 | + const verifier = (args: unknown[]): args is [string] => (++verifierCalled) % 3 === 0; |
| 110 | + const channel = "foobar"; |
| 111 | + |
| 112 | + onceCorrect({ source, listener, verifier, channel }); |
| 113 | + |
| 114 | + source.emit(channel, {}, "a"); |
| 115 | + source.emit(channel, {}, "b"); |
| 116 | + source.emit(channel, {}, "c"); |
| 117 | + source.emit(channel, {}, "d"); |
| 118 | + source.emit(channel, {}, "e"); |
| 119 | + source.emit(channel, {}, "f"); |
| 120 | + source.emit(channel, {}, "g"); |
| 121 | + source.emit(channel, {}, "h"); |
| 122 | + source.emit(channel, {}, "i"); |
| 123 | + expect(called).toBe("c"); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments