|
| 1 | +import { createCommercialQueue } from './guardian-commercial-queue'; |
| 2 | + |
| 3 | +window.guardian.config.page.adUnit = 'adUnit'; |
| 4 | + |
| 5 | +describe('createCommercialQueue', () => { |
| 6 | + it('should return an object with push and flush methods', () => { |
| 7 | + const queue = createCommercialQueue(); |
| 8 | + expect(queue).toBeDefined(); |
| 9 | + expect(typeof queue.push).toBe('function'); |
| 10 | + expect(typeof queue.flush).toBe('function'); |
| 11 | + }); |
| 12 | + it('should buffer a function without executing it', () => { |
| 13 | + const queue = createCommercialQueue(); |
| 14 | + const mockFn = jest.fn(); |
| 15 | + queue.push(mockFn); |
| 16 | + expect(mockFn).not.toHaveBeenCalled(); |
| 17 | + }); |
| 18 | + it('should buffer multiple functions without executing them', () => { |
| 19 | + const queue = createCommercialQueue(); |
| 20 | + const mockFn1 = jest.fn(); |
| 21 | + const mockFn2 = jest.fn(); |
| 22 | + const mockFn3 = jest.fn(); |
| 23 | + queue.push(mockFn1, mockFn2, mockFn3); |
| 24 | + [mockFn1, mockFn2, mockFn3].forEach((fn) => { |
| 25 | + expect(fn).not.toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + }); |
| 28 | + it('flush method should execute all buffered functions in order', () => { |
| 29 | + const queue = createCommercialQueue(); |
| 30 | + const mockFn1 = jest.fn(); |
| 31 | + const mockFn2 = jest.fn(); |
| 32 | + const mockFn3 = jest.fn(); |
| 33 | + queue.push(mockFn1, mockFn2, mockFn3); |
| 34 | + queue.flush(); |
| 35 | + |
| 36 | + expect(mockFn1.mock.invocationCallOrder[0]!).toBeLessThan( |
| 37 | + mockFn2.mock.invocationCallOrder[0]!, |
| 38 | + ); |
| 39 | + expect(mockFn2.mock.invocationCallOrder[0]!).toBeLessThan( |
| 40 | + mockFn3.mock.invocationCallOrder[0]!, |
| 41 | + ); |
| 42 | + [mockFn1, mockFn2, mockFn3].forEach((fn) => { |
| 43 | + expect(fn).toHaveBeenLastCalledWith(); |
| 44 | + }); |
| 45 | + }); |
| 46 | +}); |
0 commit comments