|
| 1 | +/** |
| 2 | + * Properly type mocked functions to make it easy to do assertions |
| 3 | + * for example, myModule.mock.calls[0] will have the typed parameters instead of any. |
| 4 | + * |
| 5 | + * TODO: share with rest of project |
| 6 | + */ |
| 7 | +type JestMockedFn<Fn> = Fn extends (...args: infer Args) => infer ReturnT |
| 8 | + ? jest.Mock<ReturnT, Args> |
| 9 | + : never |
| 10 | + |
| 11 | +const isOnline = jest.fn().mockReturnValue(true) |
| 12 | +const isOffline = jest.fn().mockReturnValue(false) |
| 13 | + |
| 14 | +jest.mock('../../connection', () => ({ |
| 15 | + isOnline, |
| 16 | + isOffline, |
| 17 | +})) |
| 18 | + |
| 19 | +const fetcher: JestMockedFn<typeof import('node-fetch')['default']> = jest.fn() |
| 20 | +jest.mock('node-fetch', () => fetcher) |
| 21 | + |
| 22 | +const invokeCallback: JestMockedFn< |
| 23 | + typeof import('../../callback')['invokeCallback'] |
| 24 | +> = jest.fn() |
| 25 | +jest.mock('../../callback', () => ({ |
| 26 | + invokeCallback: invokeCallback, |
| 27 | +})) |
| 28 | + |
| 29 | +import { EventQueue } from '../../queue/event-queue' |
| 30 | +import { Emitter } from '../../emitter' |
| 31 | +import { dispatch, getDelay } from '../dispatch' |
| 32 | +import { PriorityQueue } from '../../priority-queue' |
| 33 | +import { CoreContext } from '../../context' |
| 34 | + |
| 35 | +let emitter!: Emitter |
| 36 | +let queue!: EventQueue |
| 37 | +const dispatchSingleSpy = jest.spyOn(EventQueue.prototype, 'dispatchSingle') |
| 38 | +const dispatchSpy = jest.spyOn(EventQueue.prototype, 'dispatch') |
| 39 | +const screenCtxMatcher = expect.objectContaining<Partial<CoreContext>>({ |
| 40 | + event: { type: 'screen' }, |
| 41 | +}) |
| 42 | +describe('Dispatch', () => { |
| 43 | + beforeEach(() => { |
| 44 | + jest.resetAllMocks() |
| 45 | + dispatchSingleSpy.mockImplementationOnce((ctx) => Promise.resolve(ctx)) |
| 46 | + invokeCallback.mockImplementationOnce((ctx) => Promise.resolve(ctx)) |
| 47 | + dispatchSpy.mockImplementationOnce((ctx) => Promise.resolve(ctx)) |
| 48 | + queue = new EventQueue(new PriorityQueue(4, [])) |
| 49 | + queue.isEmpty = jest.fn().mockReturnValue(false) |
| 50 | + emitter = new Emitter() |
| 51 | + }) |
| 52 | + |
| 53 | + it('should not dispatch if client is currently offline and retries are *disabled* for the main event queue', async () => { |
| 54 | + isOnline.mockReturnValue(false) |
| 55 | + isOffline.mockReturnValue(true) |
| 56 | + |
| 57 | + const ctx = await dispatch({ type: 'screen' }, queue, emitter, { |
| 58 | + retryQueue: false, |
| 59 | + }) |
| 60 | + expect(ctx).toEqual(screenCtxMatcher) |
| 61 | + const called = Boolean( |
| 62 | + dispatchSingleSpy.mock.calls.length || dispatchSpy.mock.calls.length |
| 63 | + ) |
| 64 | + expect(called).toBeFalsy() |
| 65 | + }) |
| 66 | + |
| 67 | + it('should be allowed to dispatch if client is currently offline and retries are *enabled* for the main event queue', async () => { |
| 68 | + isOnline.mockReturnValue(false) |
| 69 | + isOffline.mockReturnValue(true) |
| 70 | + |
| 71 | + await dispatch({ type: 'screen' }, queue, emitter, { |
| 72 | + retryQueue: true, |
| 73 | + }) |
| 74 | + const called = Boolean( |
| 75 | + dispatchSingleSpy.mock.calls.length || dispatchSpy.mock.calls.length |
| 76 | + ) |
| 77 | + expect(called).toBeTruthy() |
| 78 | + }) |
| 79 | + |
| 80 | + it('should call dispatchSingle correctly if queue is empty', async () => { |
| 81 | + queue.isEmpty = jest.fn().mockReturnValue(true) |
| 82 | + await dispatch({ type: 'screen' }, queue, emitter) |
| 83 | + expect(dispatchSingleSpy).toBeCalledWith(screenCtxMatcher) |
| 84 | + expect(dispatchSpy).not.toBeCalled() |
| 85 | + }) |
| 86 | + |
| 87 | + it('should call dispatch correctly if queue has items', async () => { |
| 88 | + await dispatch({ type: 'screen' }, queue, emitter) |
| 89 | + expect(dispatchSpy).toBeCalledWith(screenCtxMatcher) |
| 90 | + expect(dispatchSingleSpy).not.toBeCalled() |
| 91 | + }) |
| 92 | + |
| 93 | + it('should only call invokeCallback if callback is passed', async () => { |
| 94 | + await dispatch({ type: 'screen' }, queue, emitter) |
| 95 | + expect(invokeCallback).not.toBeCalled() |
| 96 | + |
| 97 | + const cb = jest.fn() |
| 98 | + await dispatch({ type: 'screen' }, queue, emitter, { callback: cb }) |
| 99 | + expect(invokeCallback).toBeCalledTimes(1) |
| 100 | + }) |
| 101 | + it('should call invokeCallback with correct args', async () => { |
| 102 | + const cb = jest.fn() |
| 103 | + await dispatch({ type: 'screen' }, queue, emitter, { |
| 104 | + callback: cb, |
| 105 | + }) |
| 106 | + expect(dispatchSpy).toBeCalledWith(screenCtxMatcher) |
| 107 | + expect(invokeCallback).toBeCalledTimes(1) |
| 108 | + const [ctx, _cb] = invokeCallback.mock.calls[0] |
| 109 | + expect(ctx).toEqual(screenCtxMatcher) |
| 110 | + expect(_cb).toBe(cb) |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +describe(getDelay, () => { |
| 115 | + it('should calculate the amount of time to delay before invoking the callback', () => { |
| 116 | + const aShortTimeAgo = Date.now() - 200 |
| 117 | + const timeout = 5000 |
| 118 | + expect(Math.round(getDelay(aShortTimeAgo, timeout))).toBe(4800) |
| 119 | + }) |
| 120 | + |
| 121 | + it('should have a sensible default', () => { |
| 122 | + const aShortTimeAgo = Date.now() - 200 |
| 123 | + expect(Math.round(getDelay(aShortTimeAgo))).toBe(100) |
| 124 | + }) |
| 125 | +}) |
0 commit comments