|
| 1 | +import { AnalyticsBrowser } from '..' |
| 2 | +import unfetch from 'unfetch' |
| 3 | +import { mocked } from 'ts-jest/utils' |
| 4 | +import { Analytics } from '../analytics' |
| 5 | + |
| 6 | +jest.mock('unfetch') |
| 7 | + |
| 8 | +const mockFetchSettingsResponse = () => { |
| 9 | + const settingsResponse = Promise.resolve({ |
| 10 | + json: () => |
| 11 | + Promise.resolve({ |
| 12 | + integrations: {}, |
| 13 | + }), |
| 14 | + }) as Promise<Response> |
| 15 | + mocked(unfetch).mockImplementationOnce(() => settingsResponse) |
| 16 | +} |
| 17 | + |
| 18 | +const writeKey = 'foo' |
| 19 | + |
| 20 | +const sleep = (time: number): Promise<void> => |
| 21 | + new Promise((resolve) => { |
| 22 | + setTimeout(resolve, time) |
| 23 | + }) |
| 24 | + |
| 25 | +describe('Pre-initialization', () => { |
| 26 | + beforeEach(() => { |
| 27 | + AnalyticsBrowser._resetGlobalState() |
| 28 | + mockFetchSettingsResponse() |
| 29 | + delete window.analytics |
| 30 | + }) |
| 31 | + test('load should instantiate an analytics object', async () => { |
| 32 | + expect(AnalyticsBrowser.instance).toBeUndefined() |
| 33 | + await AnalyticsBrowser.load({ writeKey }) |
| 34 | + expect(AnalyticsBrowser.instance).toBeDefined() |
| 35 | + }) |
| 36 | + test('If a user sends a single preinitiozed track event, that event gets flushed', async () => { |
| 37 | + const trackSpy = jest.spyOn(Analytics.prototype, 'track') |
| 38 | + const trackCtxPromise = AnalyticsBrowser.track('foo', { name: 'john' }) |
| 39 | + void AnalyticsBrowser.load({ writeKey }) |
| 40 | + await trackCtxPromise |
| 41 | + expect(trackSpy).toBeCalledWith('foo', { name: 'john' }) |
| 42 | + expect(trackSpy).toBeCalledTimes(1) |
| 43 | + }) |
| 44 | + |
| 45 | + test('If a user sends multiple events, all of those event gets flushed', async () => { |
| 46 | + const trackSpy = jest.spyOn(Analytics.prototype, 'track') |
| 47 | + const identifySpy = jest.spyOn(Analytics.prototype, 'identify') |
| 48 | + |
| 49 | + const trackCtxPromise = AnalyticsBrowser.track('foo', { name: 'john' }) |
| 50 | + const trackCtxPromise2 = AnalyticsBrowser.track('bar', { age: 123 }) |
| 51 | + const identifyCtxPromise = AnalyticsBrowser.identify('hello') |
| 52 | + |
| 53 | + void AnalyticsBrowser.load({ writeKey }) |
| 54 | + await Promise.all([trackCtxPromise, trackCtxPromise2, identifyCtxPromise]) |
| 55 | + |
| 56 | + expect(trackSpy).toBeCalledWith('foo', { name: 'john' }) |
| 57 | + expect(trackSpy).toBeCalledWith('bar', { age: 123 }) |
| 58 | + expect(trackSpy).toBeCalledTimes(2) |
| 59 | + |
| 60 | + expect(identifySpy).toBeCalledWith('hello') |
| 61 | + expect(identifySpy).toBeCalledTimes(1) |
| 62 | + }) |
| 63 | + describe('snippet API with standalone', () => { |
| 64 | + test('If a snippet user sends multiple events, all of those event gets flushed', async () => { |
| 65 | + const onTrackCb = jest.fn() |
| 66 | + const onTrack = ['on', 'track', onTrackCb] |
| 67 | + const track = ['track', 'foo'] |
| 68 | + const track2 = ['track', 'bar'] |
| 69 | + const identify = ['identify'] |
| 70 | + |
| 71 | + ;(window as any).analytics = [onTrack, track, track2, identify] |
| 72 | + |
| 73 | + const onSpy = jest.spyOn(Analytics.prototype, 'on') |
| 74 | + const trackSpy = jest.spyOn(Analytics.prototype, 'track') |
| 75 | + const identifySpy = jest.spyOn(Analytics.prototype, 'identify') |
| 76 | + |
| 77 | + await AnalyticsBrowser.standalone(writeKey) |
| 78 | + |
| 79 | + await sleep(100) // the snippet does not return a promise (pre-initialization) ... it sometimes has a callback as the third argument. |
| 80 | + expect(trackSpy).toBeCalledWith('foo') |
| 81 | + expect(trackSpy).toBeCalledWith('bar') |
| 82 | + expect(trackSpy).toBeCalledTimes(2) |
| 83 | + |
| 84 | + expect(identifySpy).toBeCalledWith() |
| 85 | + expect(identifySpy).toBeCalledTimes(1) |
| 86 | + |
| 87 | + expect(onSpy).toBeCalledTimes(1) |
| 88 | + |
| 89 | + expect(onTrackCb).toBeCalledTimes(2) // gets called once for each track event |
| 90 | + expect(onTrackCb).toBeCalledWith('foo', {}, undefined) |
| 91 | + expect(onTrackCb).toBeCalledWith('bar', {}, undefined) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('AnalyticsBrowser.on track', () => { |
| 96 | + test('If, before initialization, .on("track") is called, the .on method should be called after analytics load', async () => { |
| 97 | + const onSpy = jest.spyOn(Analytics.prototype, 'on') |
| 98 | + |
| 99 | + const args = ['track', jest.fn()] as const |
| 100 | + const promise = AnalyticsBrowser.on(...args) |
| 101 | + expect(onSpy).not.toHaveBeenCalledWith(...args) |
| 102 | + void AnalyticsBrowser.load({ writeKey }) |
| 103 | + |
| 104 | + await promise |
| 105 | + expect(onSpy).toBeCalledWith(...args) |
| 106 | + expect(onSpy).toHaveBeenCalledTimes(1) |
| 107 | + }) |
| 108 | + |
| 109 | + test('If, before initialization .on("track") is called and then .track is called, the callback method should be called after analytics loads', async () => { |
| 110 | + const onFnCb = jest.fn() |
| 111 | + const onSpy = jest.spyOn(Analytics.prototype, 'on') |
| 112 | + const analyticsPromise = AnalyticsBrowser.on('track', onFnCb) |
| 113 | + const trackCtxPromise = AnalyticsBrowser.track('foo', { name: 123 }) |
| 114 | + |
| 115 | + expect(onFnCb).not.toHaveBeenCalled() |
| 116 | + void AnalyticsBrowser.load({ writeKey }) |
| 117 | + |
| 118 | + await Promise.all([analyticsPromise, trackCtxPromise]) |
| 119 | + |
| 120 | + expect(onSpy).toBeCalledWith('track', onFnCb) |
| 121 | + expect(onSpy).toHaveBeenCalledTimes(1) |
| 122 | + |
| 123 | + expect(onFnCb).toHaveBeenCalledWith('foo', { name: 123 }, undefined) |
| 124 | + expect(onFnCb).toHaveBeenCalledTimes(1) |
| 125 | + }) |
| 126 | + |
| 127 | + test('If, before initialization, .ready is called, the callback method should be called after analytics loads', async () => { |
| 128 | + const onReadyCb = jest.fn() |
| 129 | + const onReadySpy = jest.spyOn(Analytics.prototype, 'ready') |
| 130 | + const onReadyPromise = AnalyticsBrowser.ready(onReadyCb) |
| 131 | + expect(onReadyCb).not.toHaveBeenCalled() |
| 132 | + void AnalyticsBrowser.load({ writeKey }) |
| 133 | + await onReadyPromise |
| 134 | + expect(onReadySpy).toHaveBeenCalledTimes(1) // ERROR |
| 135 | + expect(onReadyCb).toHaveBeenCalledTimes(1) // ERROR |
| 136 | + }) |
| 137 | + |
| 138 | + test('Should work with "on" events if a track event is called after load', async () => { |
| 139 | + const onTrackCb = jest.fn() |
| 140 | + const analyticsPromise = AnalyticsBrowser.on('track', onTrackCb) |
| 141 | + void AnalyticsBrowser.load({ writeKey }) |
| 142 | + const trackCtxPromise = AnalyticsBrowser.track('foo', { name: 123 }) |
| 143 | + |
| 144 | + await Promise.all([analyticsPromise, trackCtxPromise]) |
| 145 | + expect(onTrackCb).toBeCalledTimes(1) |
| 146 | + }) |
| 147 | + |
| 148 | + test('Should work with "on" events if a track event is called after load is complete', async () => { |
| 149 | + const onTrackCb = jest.fn() |
| 150 | + const analyticsPromise = AnalyticsBrowser.on('track', onTrackCb) |
| 151 | + await AnalyticsBrowser.load({ writeKey }) |
| 152 | + const trackCtxPromise = AnalyticsBrowser.track('foo', { name: 123 }) |
| 153 | + |
| 154 | + await Promise.all([analyticsPromise, trackCtxPromise]) |
| 155 | + expect(onTrackCb).toBeCalledTimes(1) |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
0 commit comments