|
1 | | -import { replaceMockable, registerCleanupTask } from '../../test' |
| 1 | +import type { MockFetch } from '../../test' |
| 2 | +import { mockFetch, mockXhr, registerCleanupTask, replaceMockable, withXhr } from '../../test' |
2 | 3 | import { Observable } from '../tools/observable' |
| 4 | +import { resetFetchObservable } from '../browser/fetchObservable' |
| 5 | +import { resetXhrObservable } from '../browser/xhrObservable' |
3 | 6 | import { clocksNow } from '../tools/utils/timeUtils' |
| 7 | +import { ConsoleApiName } from '../tools/display' |
| 8 | +import { resetConsoleObservable } from './console/consoleObservable' |
4 | 9 | import { BufferedDataType, startBufferingData } from './bufferedData' |
5 | 10 | import { ErrorHandling, ErrorSource, type RawError } from './error/error.types' |
6 | 11 | import { trackRuntimeError } from './error/trackRuntimeError' |
@@ -28,9 +33,85 @@ describe('startBufferingData', () => { |
28 | 33 | observable.subscribe((data) => { |
29 | 34 | expect(data).toEqual({ |
30 | 35 | type: BufferedDataType.RUNTIME_ERROR, |
31 | | - error: rawError, |
| 36 | + data: rawError, |
32 | 37 | }) |
33 | 38 | done() |
34 | 39 | }) |
35 | 40 | }) |
| 41 | + |
| 42 | + it('collects fetch requests', (done) => { |
| 43 | + const mockFetchManager = mockFetch() |
| 44 | + const { observable, stop } = startBufferingData() |
| 45 | + const fetch = window.fetch as MockFetch |
| 46 | + |
| 47 | + registerCleanupTask(() => { |
| 48 | + stop() |
| 49 | + resetFetchObservable() |
| 50 | + }) |
| 51 | + |
| 52 | + const collected: BufferedDataType[] = [] |
| 53 | + observable.subscribe((data) => { |
| 54 | + if (data.type === BufferedDataType.FETCH) { |
| 55 | + collected.push(data.type) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + fetch('http://fake-url/').resolveWith({ status: 200, responseText: 'ok' }) |
| 60 | + |
| 61 | + mockFetchManager.whenAllComplete(() => { |
| 62 | + expect(collected.length).toBeGreaterThan(0) |
| 63 | + done() |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('collects xhr requests', (done) => { |
| 68 | + mockXhr() |
| 69 | + const { observable, stop } = startBufferingData() |
| 70 | + |
| 71 | + registerCleanupTask(() => { |
| 72 | + stop() |
| 73 | + resetXhrObservable() |
| 74 | + }) |
| 75 | + |
| 76 | + observable.subscribe((data) => { |
| 77 | + if (data.type === BufferedDataType.XHR && data.data.state === 'complete') { |
| 78 | + expect(data.data.url).toContain('fake-url') |
| 79 | + expect(data.data.status).toBe(200) |
| 80 | + done() |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + withXhr({ |
| 85 | + setup(xhr) { |
| 86 | + xhr.open('GET', 'http://fake-url/') |
| 87 | + xhr.send() |
| 88 | + xhr.complete(200, 'ok') |
| 89 | + }, |
| 90 | + onComplete: noop, |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + it('collects console logs', (done) => { |
| 95 | + const { observable, stop } = startBufferingData() |
| 96 | + |
| 97 | + registerCleanupTask(() => { |
| 98 | + stop() |
| 99 | + resetConsoleObservable() |
| 100 | + }) |
| 101 | + |
| 102 | + observable.subscribe((data) => { |
| 103 | + if (data.type === BufferedDataType.CONSOLE && data.data.api === ConsoleApiName.error) { |
| 104 | + expect(data.data.message).toContain('buffered data test error') |
| 105 | + done() |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + /* eslint-disable no-console */ |
| 110 | + console.error('buffered data test error') |
| 111 | + /* eslint-enable no-console */ |
| 112 | + }) |
36 | 113 | }) |
| 114 | + |
| 115 | +function noop() { |
| 116 | + // intentionally empty |
| 117 | +} |
0 commit comments