|
| 1 | +import { SegmentClient } from '../analytics'; |
| 2 | +import { Plugin } from '../plugin'; |
| 3 | +import { Timeline } from '../timeline'; |
| 4 | +import { EventType, PluginType, SegmentEvent, TrackEventType } from '../types'; |
| 5 | +import { getMockLogger } from './__helpers__/mockLogger'; |
| 6 | +import { MockSegmentStore } from './__helpers__/mockSegmentStore'; |
| 7 | + |
| 8 | +jest |
| 9 | + .spyOn(Date.prototype, 'toISOString') |
| 10 | + .mockReturnValue('2010-01-01T00:00:00.000Z'); |
| 11 | + |
| 12 | +describe('timeline', () => { |
| 13 | + const initialUserInfo = { |
| 14 | + traits: { |
| 15 | + name: 'Stacy', |
| 16 | + age: 30, |
| 17 | + }, |
| 18 | + userId: 'current-user-id', |
| 19 | + anonymousId: 'very-anonymous', |
| 20 | + }; |
| 21 | + |
| 22 | + const store = new MockSegmentStore({ |
| 23 | + userInfo: initialUserInfo, |
| 24 | + settings: { |
| 25 | + '1': true, |
| 26 | + '2': true, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + const clientArgs = { |
| 31 | + config: { |
| 32 | + writeKey: 'mock-write-key', |
| 33 | + }, |
| 34 | + logger: getMockLogger(), |
| 35 | + store: store, |
| 36 | + }; |
| 37 | + |
| 38 | + const client = new SegmentClient(clientArgs); |
| 39 | + |
| 40 | + class MockPlugin extends Plugin { |
| 41 | + constructor( |
| 42 | + private readonly executeFunc: ( |
| 43 | + event: SegmentEvent |
| 44 | + ) => SegmentEvent | undefined, |
| 45 | + type: PluginType |
| 46 | + ) { |
| 47 | + super(); |
| 48 | + this.type = type; |
| 49 | + this.analytics = client; |
| 50 | + } |
| 51 | + |
| 52 | + execute(event: SegmentEvent) { |
| 53 | + return this.executeFunc(event); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + it('processes each destination independently', () => { |
| 58 | + const timeline = new Timeline(); |
| 59 | + |
| 60 | + const goodPlugin = jest.fn().mockImplementation((e) => e); |
| 61 | + const badPlugin = jest.fn().mockImplementation(() => undefined); |
| 62 | + timeline.add(new MockPlugin(badPlugin, PluginType.destination)); |
| 63 | + timeline.add(new MockPlugin(goodPlugin, PluginType.destination)); |
| 64 | + |
| 65 | + const expectedEvent: TrackEventType = { |
| 66 | + type: EventType.TrackEvent, |
| 67 | + event: 'test', |
| 68 | + properties: { |
| 69 | + test: 'sample', |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + const result = timeline.process(expectedEvent); |
| 74 | + |
| 75 | + expect(result).toEqual(expectedEvent); |
| 76 | + expect(goodPlugin).toHaveBeenCalled(); |
| 77 | + expect(badPlugin).toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('handles errors from plugins execution', () => { |
| 81 | + const timeline = new Timeline(); |
| 82 | + |
| 83 | + const goodPlugin = jest.fn().mockImplementation((e) => e); |
| 84 | + const badPlugin = jest.fn().mockImplementation(() => { |
| 85 | + throw 'ERROR'; |
| 86 | + }); |
| 87 | + timeline.add(new MockPlugin(badPlugin, PluginType.before)); |
| 88 | + timeline.add(new MockPlugin(goodPlugin, PluginType.before)); |
| 89 | + |
| 90 | + const expectedEvent: TrackEventType = { |
| 91 | + type: EventType.TrackEvent, |
| 92 | + event: 'test', |
| 93 | + properties: { |
| 94 | + test: 'sample', |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + const result = timeline.process(expectedEvent); |
| 99 | + |
| 100 | + expect(result).toEqual(expectedEvent); |
| 101 | + expect(goodPlugin).toHaveBeenCalled(); |
| 102 | + expect(badPlugin).toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('shortcircuits plugin execution if a plugin return undefined', () => { |
| 106 | + const timeline = new Timeline(); |
| 107 | + |
| 108 | + const goodPlugin = jest.fn().mockImplementation((e) => e); |
| 109 | + const badPlugin = jest.fn().mockImplementation(() => undefined); |
| 110 | + timeline.add(new MockPlugin(badPlugin, PluginType.before)); |
| 111 | + timeline.add(new MockPlugin(goodPlugin, PluginType.before)); |
| 112 | + timeline.add(new MockPlugin(goodPlugin, PluginType.destination)); |
| 113 | + |
| 114 | + const expectedEvent: TrackEventType = { |
| 115 | + type: EventType.TrackEvent, |
| 116 | + event: 'test', |
| 117 | + properties: { |
| 118 | + test: 'sample', |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + const result = timeline.process(expectedEvent); |
| 123 | + |
| 124 | + expect(result).toEqual(undefined); |
| 125 | + expect(goodPlugin).not.toHaveBeenCalled(); |
| 126 | + expect(badPlugin).toHaveBeenCalled(); |
| 127 | + }); |
| 128 | +}); |
0 commit comments