|
1 | | -import { H } from 'highlight.run' |
2 | | -import { LDClientMin, HighlightPublicInterface } from '../client' |
3 | | - |
4 | | -// Don't run tests for now. Need to move code from firstload to client for backend errors. |
5 | | -describe.skip('should work outside of the browser in unit test', () => { |
6 | | - let highlight: HighlightPublicInterface |
| 1 | +import { LDClientMin } from '../client' |
| 2 | +import { expect, vi } from 'vitest' |
| 3 | +import { |
| 4 | + setSessionData, |
| 5 | + setSessionSecureID, |
| 6 | +} from '../client/utils/sessionStorage/highlightSession' |
| 7 | +import * as otel from '../client/otel' |
| 8 | +import { Highlight } from '../client' |
| 9 | +import { Observe } from '../api/observe' |
| 10 | +import { ObserveSDK } from '../sdk/observe' |
| 11 | + |
| 12 | +const sessionData = { |
| 13 | + sessionSecureID: 'foo', |
| 14 | + projectID: 1, |
| 15 | + payloadID: 1, |
| 16 | + lastPushTime: new Date().getTime(), |
| 17 | + sessionStartTime: new Date().getTime(), |
| 18 | +} |
| 19 | + |
| 20 | +describe('should work outside of the browser in unit test', () => { |
| 21 | + let highlight: Highlight |
| 22 | + let observe: Observe |
7 | 23 |
|
8 | 24 | beforeEach(() => { |
9 | | - jest.useFakeTimers() |
10 | | - highlight = H |
| 25 | + vi.useFakeTimers() |
| 26 | + highlight = new Highlight({ |
| 27 | + organizationID: undefined, |
| 28 | + sessionSecureID: '', |
| 29 | + }) |
| 30 | + observe = new ObserveSDK({ |
| 31 | + otlpEndpoint: '', |
| 32 | + projectId: undefined, |
| 33 | + sessionSecureId: '', |
| 34 | + }) |
| 35 | + |
| 36 | + setSessionSecureID('foo') |
| 37 | + setSessionData(sessionData) |
11 | 38 | }) |
12 | 39 |
|
13 | 40 | afterEach(() => { |
14 | | - jest.useRealTimers() |
| 41 | + vi.useRealTimers() |
15 | 42 | }) |
16 | 43 |
|
17 | 44 | it('should handle init', () => { |
18 | | - highlight.init('test') |
| 45 | + highlight.initialize({}) |
19 | 46 | }) |
20 | 47 |
|
21 | 48 | it('should handle consumeError', () => { |
22 | 49 | const error = new Error('test error') |
23 | | - highlight.consumeError(error) |
24 | | - }) |
25 | | - |
26 | | - it('should handle error', () => { |
27 | | - highlight.error('test message') |
| 50 | + highlight.consumeError(error, {}) |
28 | 51 | }) |
29 | 52 |
|
30 | 53 | it('should handle track', () => { |
31 | | - highlight.track('test message', {}) |
| 54 | + highlight.addProperties('test message', {}) |
32 | 55 | }) |
33 | 56 |
|
34 | 57 | it('should handle start', () => { |
35 | | - highlight.init('test', { manualStart: true }) |
36 | | - |
37 | | - highlight.start() |
| 58 | + highlight.initialize({ forceNew: true }) |
38 | 59 | }) |
39 | 60 |
|
40 | 61 | it('should handle stop', () => { |
41 | | - highlight.stop() |
| 62 | + highlight.stopRecording() |
42 | 63 | }) |
43 | 64 |
|
44 | 65 | it('should handle identify', () => { |
45 | 66 | highlight.identify('123', {}) |
46 | 67 | }) |
47 | 68 |
|
48 | | - it('should handle getSessionURL', () => { |
49 | | - highlight.getSessionURL() |
| 69 | + it('should handle getSessionURL', async () => { |
| 70 | + setSessionData(sessionData) |
| 71 | + highlight.initialize() |
| 72 | + |
| 73 | + expect(await highlight.getCurrentSessionURL()).toBe( |
| 74 | + 'https://app.highlight.io/1/sessions/foo', |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('startSpan', () => { |
| 79 | + it('it returns the value of the callback', () => |
| 80 | + new Promise(async (done) => { |
| 81 | + let tracer: any |
| 82 | + await vi.waitFor(() => { |
| 83 | + tracer = otel.getTracer() |
| 84 | + expect(tracer).toBeDefined() |
| 85 | + }) |
| 86 | + |
| 87 | + vi.spyOn(tracer, 'startActiveSpan') |
| 88 | + |
| 89 | + const value = observe.startSpan('test', () => 'test') |
| 90 | + expect(value).toBe('test') |
| 91 | + |
| 92 | + expect(tracer.startActiveSpan).toHaveBeenCalledWith( |
| 93 | + 'test', |
| 94 | + expect.any(Function), |
| 95 | + ) |
| 96 | + |
| 97 | + done(true) |
| 98 | + })) |
50 | 99 | }) |
51 | 100 |
|
52 | | - it('should handle getSessionDetails', () => { |
53 | | - highlight.getSessionDetails() |
| 101 | + describe('startManualSpan', () => { |
| 102 | + it('it returns the value of the callback', () => |
| 103 | + new Promise(async (done) => { |
| 104 | + let tracer: any |
| 105 | + await vi.waitFor(() => { |
| 106 | + tracer = otel.getTracer() |
| 107 | + expect(tracer).toBeDefined() |
| 108 | + }) |
| 109 | + |
| 110 | + vi.spyOn(tracer, 'startActiveSpan') |
| 111 | + |
| 112 | + const value = observe.startManualSpan('test', (span) => { |
| 113 | + span.end() |
| 114 | + return 'test' |
| 115 | + }) |
| 116 | + expect(value).toBe('test') |
| 117 | + |
| 118 | + expect(tracer.startActiveSpan).toHaveBeenCalledWith( |
| 119 | + 'test', |
| 120 | + expect.any(Function), |
| 121 | + ) |
| 122 | + |
| 123 | + done(true) |
| 124 | + })) |
54 | 125 | }) |
55 | 126 | }) |
56 | 127 |
|
| 128 | +const sleep = (ms: number) => { |
| 129 | + const promise = new Promise((resolve) => setTimeout(resolve, ms)) |
| 130 | + vi.advanceTimersByTime(ms) |
| 131 | + return promise |
| 132 | +} |
| 133 | + |
57 | 134 | describe('LD integration', () => { |
58 | | - let highlight: HighlightPublicInterface |
| 135 | + let highlight: Highlight |
59 | 136 |
|
60 | 137 | beforeEach(() => { |
61 | | - jest.useFakeTimers() |
62 | | - highlight = H |
| 138 | + vi.useFakeTimers() |
| 139 | + highlight = new Highlight({ |
| 140 | + organizationID: '', |
| 141 | + sessionSecureID: '', |
| 142 | + }) |
63 | 143 | }) |
64 | 144 |
|
65 | 145 | afterEach(() => { |
66 | | - jest.useRealTimers() |
| 146 | + vi.useRealTimers() |
67 | 147 | }) |
68 | 148 |
|
69 | 149 | it('should handle register', () => { |
| 150 | + const worker = (globalThis.Worker as unknown as typeof Worker).prototype |
| 151 | + worker.postMessage = vi.fn( |
| 152 | + (_message: unknown, _options?: unknown) => null, |
| 153 | + ) |
| 154 | + |
70 | 155 | const client: LDClientMin = { |
71 | | - track: jest.fn(), |
72 | | - identify: jest.fn(), |
73 | | - addHook: jest.fn(), |
| 156 | + track: vi.fn(), |
| 157 | + identify: vi.fn(), |
| 158 | + addHook: vi.fn(), |
74 | 159 | } |
75 | 160 | highlight.registerLD(client) |
76 | 161 |
|
77 | | - expect(client.addHook).toHaveBeenCalled() |
| 162 | + expect(client.addHook).not.toHaveBeenCalled() |
78 | 163 | expect(client.identify).not.toHaveBeenCalled() |
79 | 164 | expect(client.track).not.toHaveBeenCalled() |
| 165 | + expect(worker.postMessage).not.toHaveBeenCalled() |
| 166 | + |
| 167 | + highlight.identify('123', {}) |
| 168 | + highlight.addProperties('test', {}) |
| 169 | + // noop for launchdarkly |
| 170 | + expect(client.identify).not.toHaveBeenCalled() |
| 171 | + expect(client.track).toHaveBeenCalled() |
| 172 | + expect(worker.postMessage).toHaveBeenCalled() |
80 | 173 | }) |
81 | 174 | }) |
0 commit comments