|
| 1 | +import { OptableCommands } from "./commands"; |
| 2 | + |
| 3 | +describe("OptableCommands", () => { |
| 4 | + it("executes functions queued before construction, in order", () => { |
| 5 | + const calls: number[] = []; |
| 6 | + new OptableCommands([() => calls.push(1), () => calls.push(2)]); |
| 7 | + expect(calls).toEqual([1, 2]); |
| 8 | + }); |
| 9 | + |
| 10 | + it("ignores non-function entries in the queue", () => { |
| 11 | + const fn = jest.fn(); |
| 12 | + expect(() => new OptableCommands([null, "x", 42, fn])).not.toThrow(); |
| 13 | + expect(fn).toHaveBeenCalledTimes(1); |
| 14 | + }); |
| 15 | + |
| 16 | + it("logs a throwing queued function and continues draining", () => { |
| 17 | + const spy = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 18 | + const after = jest.fn(); |
| 19 | + const boom = new Error("boom"); |
| 20 | + expect( |
| 21 | + () => |
| 22 | + new OptableCommands([ |
| 23 | + () => { |
| 24 | + throw boom; |
| 25 | + }, |
| 26 | + after, |
| 27 | + ]) |
| 28 | + ).not.toThrow(); |
| 29 | + expect(after).toHaveBeenCalledTimes(1); |
| 30 | + expect(spy).toHaveBeenCalledWith(boom); |
| 31 | + spy.mockRestore(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("tolerates a missing or non-array queue", () => { |
| 35 | + expect(() => new OptableCommands()).not.toThrow(); |
| 36 | + expect(() => new OptableCommands(undefined)).not.toThrow(); |
| 37 | + expect(() => new OptableCommands({} as unknown)).not.toThrow(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("executes pushed functions immediately and returns their value", () => { |
| 41 | + const cmd = new OptableCommands([]); |
| 42 | + const fn = jest.fn(() => "done"); |
| 43 | + expect(cmd.push(fn)).toBe("done"); |
| 44 | + expect(fn).toHaveBeenCalledTimes(1); |
| 45 | + }); |
| 46 | +}); |
0 commit comments