|
1 | | -import { replaceMockable, registerCleanupTask } from '../../test' |
| 1 | +import type { MockFetch } from '../../test' |
| 2 | +import { collectAsyncCalls, 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 { noop } from '../tools/utils/functionUtils' |
| 9 | +import { resetConsoleObservable } from './console/consoleObservable' |
| 10 | +import type { BufferedData } from './bufferedData' |
4 | 11 | import { BufferedDataType, startBufferingData } from './bufferedData' |
5 | 12 | import { ErrorHandling, ErrorSource, type RawError } from './error/error.types' |
6 | 13 | import { trackRuntimeError } from './error/trackRuntimeError' |
@@ -28,9 +35,123 @@ describe('startBufferingData', () => { |
28 | 35 | observable.subscribe((data) => { |
29 | 36 | expect(data).toEqual({ |
30 | 37 | type: BufferedDataType.RUNTIME_ERROR, |
31 | | - error: rawError, |
| 38 | + data: rawError, |
32 | 39 | }) |
33 | 40 | done() |
34 | 41 | }) |
35 | 42 | }) |
| 43 | + |
| 44 | + it('collects fetch requests', async () => { |
| 45 | + mockFetch() |
| 46 | + const { observable, stop } = startBufferingData() |
| 47 | + const fetch = window.fetch as MockFetch |
| 48 | + const collected: BufferedData[] = [] |
| 49 | + const bufferedDataCollectedSpy = jasmine.createSpy() |
| 50 | + |
| 51 | + registerCleanupTask(() => { |
| 52 | + stop() |
| 53 | + resetFetchObservable() |
| 54 | + }) |
| 55 | + |
| 56 | + observable.subscribe((data) => { |
| 57 | + if (data.type === BufferedDataType.FETCH) { |
| 58 | + collected.push(data) |
| 59 | + bufferedDataCollectedSpy() |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + fetch('http://fake-url/').resolveWith({ status: 200, responseText: 'ok' }) |
| 64 | + |
| 65 | + await collectAsyncCalls(bufferedDataCollectedSpy, 2) |
| 66 | + |
| 67 | + expect(collected).toEqual([ |
| 68 | + { |
| 69 | + type: BufferedDataType.FETCH, |
| 70 | + data: jasmine.objectContaining({ |
| 71 | + state: 'start', |
| 72 | + url: 'http://fake-url/', |
| 73 | + method: 'GET', |
| 74 | + }), |
| 75 | + }, |
| 76 | + { |
| 77 | + type: BufferedDataType.FETCH, |
| 78 | + data: jasmine.objectContaining({ |
| 79 | + state: 'resolve', |
| 80 | + url: 'http://fake-url/', |
| 81 | + method: 'GET', |
| 82 | + status: 200, |
| 83 | + }), |
| 84 | + }, |
| 85 | + ]) |
| 86 | + }) |
| 87 | + |
| 88 | + it('collects xhr requests', async () => { |
| 89 | + mockXhr() |
| 90 | + const { observable, stop } = startBufferingData() |
| 91 | + const collected: BufferedData[] = [] |
| 92 | + const bufferedDataCollectedSpy = jasmine.createSpy() |
| 93 | + |
| 94 | + registerCleanupTask(() => { |
| 95 | + stop() |
| 96 | + resetXhrObservable() |
| 97 | + }) |
| 98 | + |
| 99 | + withXhr({ |
| 100 | + setup(xhr) { |
| 101 | + xhr.open('GET', 'http://fake-url/') |
| 102 | + xhr.send() |
| 103 | + xhr.complete(200, 'ok') |
| 104 | + }, |
| 105 | + onComplete: noop, |
| 106 | + }) |
| 107 | + |
| 108 | + observable.subscribe((data) => { |
| 109 | + if (data.type === BufferedDataType.XHR) { |
| 110 | + collected.push(data) |
| 111 | + bufferedDataCollectedSpy() |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + await collectAsyncCalls(bufferedDataCollectedSpy, 2) |
| 116 | + |
| 117 | + expect(collected).toEqual([ |
| 118 | + { |
| 119 | + type: BufferedDataType.XHR, |
| 120 | + data: jasmine.objectContaining({ |
| 121 | + state: 'start', |
| 122 | + url: 'http://fake-url/', |
| 123 | + method: 'GET', |
| 124 | + }), |
| 125 | + }, |
| 126 | + { |
| 127 | + type: BufferedDataType.XHR, |
| 128 | + data: jasmine.objectContaining({ |
| 129 | + state: 'complete', |
| 130 | + url: 'http://fake-url/', |
| 131 | + method: 'GET', |
| 132 | + status: 200, |
| 133 | + }), |
| 134 | + }, |
| 135 | + ]) |
| 136 | + }) |
| 137 | + |
| 138 | + it('collects console logs', (done) => { |
| 139 | + const { observable, stop } = startBufferingData() |
| 140 | + |
| 141 | + registerCleanupTask(() => { |
| 142 | + stop() |
| 143 | + resetConsoleObservable() |
| 144 | + }) |
| 145 | + |
| 146 | + observable.subscribe((data) => { |
| 147 | + if (data.type === BufferedDataType.CONSOLE && data.data.api === ConsoleApiName.error) { |
| 148 | + expect(data.data.message).toContain('buffered data test error') |
| 149 | + done() |
| 150 | + } |
| 151 | + }) |
| 152 | + |
| 153 | + /* eslint-disable no-console */ |
| 154 | + console.error('buffered data test error') |
| 155 | + /* eslint-enable no-console */ |
| 156 | + }) |
36 | 157 | }) |
0 commit comments