|
| 1 | +import config from 'config' |
| 2 | + |
| 3 | +import { AmplitudeEventTracker, initAmplitude } from './amplitude' |
| 4 | + |
| 5 | +const mockIdentifySet = vi.hoisted(() => vi.fn()) |
| 6 | +const mockIdentifyConstructor = vi.hoisted(() => vi.fn()) |
| 7 | +const mockAmplitude = vi.hoisted(() => { |
| 8 | + class MockIdentify { |
| 9 | + constructor() { |
| 10 | + mockIdentifyConstructor() |
| 11 | + } |
| 12 | + set(key: string, value: any) { |
| 13 | + mockIdentifySet(key, value) |
| 14 | + } |
| 15 | + } |
| 16 | + return { |
| 17 | + init: vi.fn(), |
| 18 | + track: vi.fn(), |
| 19 | + identify: vi.fn(), |
| 20 | + setUserId: vi.fn(), |
| 21 | + Identify: MockIdentify, |
| 22 | + } |
| 23 | +}) |
| 24 | +vi.mock('@amplitude/analytics-browser', () => mockAmplitude) |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + vi.resetAllMocks() |
| 28 | +}) |
| 29 | + |
| 30 | +describe('when initAmplitude is called', () => { |
| 31 | + describe('and AMPLITUDE_API_KEY is not defined', () => { |
| 32 | + it('throws an error', () => { |
| 33 | + config.AMPLITUDE_API_KEY = undefined |
| 34 | + try { |
| 35 | + initAmplitude() |
| 36 | + } catch (e) { |
| 37 | + expect(e).toEqual( |
| 38 | + new Error( |
| 39 | + 'AMPLITUDE_API_KEY is not defined. Amplitude events will not be tracked.' |
| 40 | + ) |
| 41 | + ) |
| 42 | + } |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + describe('and AMPLITUDE_API_KEY is defined', () => { |
| 47 | + it('calls amplitude.init() with api key', () => { |
| 48 | + config.AMPLITUDE_API_KEY = 'asdf1234' |
| 49 | + initAmplitude() |
| 50 | + expect(mockAmplitude.init).toHaveBeenCalled() |
| 51 | + }) |
| 52 | + }) |
| 53 | +}) |
| 54 | + |
| 55 | +describe('AmplitudeEventTracker', () => { |
| 56 | + describe('identify', () => { |
| 57 | + describe('when identify is called', () => { |
| 58 | + it('calls appropriate sdk functions', () => { |
| 59 | + const tracker = new AmplitudeEventTracker() |
| 60 | + tracker.identify({ |
| 61 | + provider: 'gh', |
| 62 | + userOwnerId: 123, |
| 63 | + }) |
| 64 | + expect(mockAmplitude.setUserId).toHaveBeenCalledWith('123') |
| 65 | + expect(mockIdentifyConstructor).toHaveBeenCalled() |
| 66 | + expect(mockIdentifySet).toHaveBeenCalledWith('provider', 'github') |
| 67 | + expect(mockAmplitude.identify).toHaveBeenCalled() |
| 68 | + expect(tracker.identity).toEqual({ |
| 69 | + userOwnerId: 123, |
| 70 | + provider: 'gh', |
| 71 | + }) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('when identify is called multiple times with the same identity', () => { |
| 76 | + it('does not make any amplitude calls', () => { |
| 77 | + const tracker = new AmplitudeEventTracker() |
| 78 | + tracker.identify({ |
| 79 | + provider: 'gh', |
| 80 | + userOwnerId: 123, |
| 81 | + }) |
| 82 | + |
| 83 | + vi.resetAllMocks() |
| 84 | + |
| 85 | + tracker.identify({ |
| 86 | + provider: 'gh', |
| 87 | + userOwnerId: 123, |
| 88 | + }) |
| 89 | + |
| 90 | + expect(mockAmplitude.setUserId).not.toHaveBeenCalled() |
| 91 | + |
| 92 | + expect(tracker.identity).toEqual({ |
| 93 | + userOwnerId: 123, |
| 94 | + provider: 'gh', |
| 95 | + }) |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe('track', () => { |
| 101 | + describe('when track is called with no context', () => { |
| 102 | + it('does not populate any context', () => { |
| 103 | + const tracker = new AmplitudeEventTracker() |
| 104 | + tracker.track({ |
| 105 | + type: 'Button Clicked', |
| 106 | + properties: { |
| 107 | + buttonType: 'Configure Repo', |
| 108 | + }, |
| 109 | + }) |
| 110 | + |
| 111 | + expect(mockAmplitude.track).toHaveBeenCalledWith({ |
| 112 | + event_type: 'Button Clicked', |
| 113 | + event_properties: { |
| 114 | + buttonType: 'Configure Repo', |
| 115 | + }, |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe('when track is called with context', () => { |
| 121 | + it('populates context as event properties', () => { |
| 122 | + const tracker = new AmplitudeEventTracker() |
| 123 | + tracker.setContext({ |
| 124 | + owner: { |
| 125 | + id: 123, |
| 126 | + }, |
| 127 | + repo: { |
| 128 | + id: 321, |
| 129 | + isPrivate: false, |
| 130 | + }, |
| 131 | + }) |
| 132 | + |
| 133 | + tracker.track({ |
| 134 | + type: 'Button Clicked', |
| 135 | + properties: { |
| 136 | + buttonType: 'Configure Repo', |
| 137 | + }, |
| 138 | + }) |
| 139 | + |
| 140 | + expect(mockAmplitude.track).toHaveBeenCalledWith({ |
| 141 | + event_type: 'Button Clicked', |
| 142 | + event_properties: { |
| 143 | + buttonType: 'Configure Repo', |
| 144 | + owner: { |
| 145 | + id: 123, |
| 146 | + }, |
| 147 | + repo: { |
| 148 | + id: 321, |
| 149 | + isPrivate: false, |
| 150 | + }, |
| 151 | + }, |
| 152 | + groups: { |
| 153 | + org: 123, |
| 154 | + }, |
| 155 | + }) |
| 156 | + }) |
| 157 | + }) |
| 158 | + }) |
| 159 | +}) |
0 commit comments