|
| 1 | +import type { Destination, WalkerOS } from '@elbwalker/types'; |
| 2 | +import { createEvent, clone } from '@elbwalker/utils'; |
| 3 | +import { pushToDestinations } from '../'; |
| 4 | + |
| 5 | +describe('Destination', () => { |
| 6 | + let event: WalkerOS.Event; |
| 7 | + let destination: Destination.Destination; |
| 8 | + let config: Destination.Config; |
| 9 | + let mockInit: jest.Mock; |
| 10 | + let mockPush: jest.Mock; |
| 11 | + |
| 12 | + function createDestination( |
| 13 | + args?: Partial<Destination.Destination>, |
| 14 | + ): Destination.Destination { |
| 15 | + return { |
| 16 | + init: mockInit, |
| 17 | + push: mockPush, |
| 18 | + config: {}, |
| 19 | + ...args, |
| 20 | + } as Destination.Destination; |
| 21 | + } |
| 22 | + |
| 23 | + function createInstance( |
| 24 | + args?: Partial<WalkerOS.Instance>, |
| 25 | + ): WalkerOS.Instance { |
| 26 | + return { |
| 27 | + config: {}, |
| 28 | + destinations: { foo: destination }, |
| 29 | + globals: {}, |
| 30 | + hooks: {}, |
| 31 | + user: {}, |
| 32 | + consent: {}, |
| 33 | + queue: [], |
| 34 | + ...args, |
| 35 | + } as unknown as WalkerOS.Instance; |
| 36 | + } |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + event = createEvent(); |
| 40 | + mockInit = jest.fn(); //.mockImplementation(console.log); |
| 41 | + mockPush = jest.fn(); //.mockImplementation(console.log); |
| 42 | + |
| 43 | + config = { init: false }; |
| 44 | + |
| 45 | + destination = { |
| 46 | + init: mockInit, |
| 47 | + push: mockPush, |
| 48 | + config, |
| 49 | + }; |
| 50 | + }); |
| 51 | + |
| 52 | + test('preventing data manipulation', async () => { |
| 53 | + const clonedEvent = clone(event); |
| 54 | + const mockPushUpdate = jest.fn().mockImplementation((event) => { |
| 55 | + event.data.foo = 'bar'; |
| 56 | + }); |
| 57 | + |
| 58 | + const destinationUpdate = { |
| 59 | + init: mockInit, |
| 60 | + push: mockPushUpdate, |
| 61 | + config: {}, |
| 62 | + }; |
| 63 | + |
| 64 | + await pushToDestinations(createInstance(), event, { |
| 65 | + destinationUpdate, |
| 66 | + destination, |
| 67 | + }); |
| 68 | + expect(mockPushUpdate).toHaveBeenCalledTimes(1); |
| 69 | + expect(mockPush).toHaveBeenCalledTimes(1); |
| 70 | + expect(mockPush).toHaveBeenCalledWith( |
| 71 | + clonedEvent, |
| 72 | + { init: true }, |
| 73 | + undefined, |
| 74 | + expect.anything(), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + test('failing init', async () => { |
| 79 | + // Simulate a failed init |
| 80 | + mockInit.mockImplementation(() => false); |
| 81 | + |
| 82 | + await pushToDestinations(createInstance(), event); |
| 83 | + expect(mockInit).toHaveBeenCalledTimes(1); |
| 84 | + expect(mockPush).toHaveBeenCalledTimes(0); |
| 85 | + expect(destination.config.init).toBeFalsy(); |
| 86 | + }); |
| 87 | + |
| 88 | + test('DLQ', async () => { |
| 89 | + const event = createEvent(); |
| 90 | + // Simulate a failing push |
| 91 | + mockPush.mockImplementation(() => { |
| 92 | + throw new Error('kaputt'); |
| 93 | + }); |
| 94 | + |
| 95 | + const destination = createDestination(); |
| 96 | + const result = await pushToDestinations(createInstance(), event, { |
| 97 | + destination, |
| 98 | + }); |
| 99 | + expect(result.failed).toHaveLength(1); |
| 100 | + expect(result.status.ok).toBeFalsy(); |
| 101 | + expect(mockPush).toHaveBeenCalledTimes(1); |
| 102 | + expect(destination.dlq).toContainEqual([event, new Error('kaputt')]); |
| 103 | + }); |
| 104 | + |
| 105 | + test('skip on denied consent', async () => {}); |
| 106 | +}); |
0 commit comments