|
| 1 | +import { Outcome } from '@sentry/types'; |
| 2 | + |
1 | 3 | import { BaseTransport } from '../../../src/transports/base';
|
2 | 4 |
|
3 | 5 | const testDsn = 'https://[email protected]/42';
|
| 6 | +const envelopeEndpoint = 'https://sentry.io/api/42/envelope/?sentry_key=123&sentry_version=7'; |
4 | 7 |
|
5 | 8 | class SimpleTransport extends BaseTransport {}
|
6 | 9 |
|
7 | 10 | describe('BaseTransport', () => {
|
| 11 | + describe('Client Reports', () => { |
| 12 | + const sendBeaconSpy = jest.fn(); |
| 13 | + let visibilityState: string; |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + navigator.sendBeacon = sendBeaconSpy; |
| 17 | + Object.defineProperty(document, 'visibilityState', { |
| 18 | + configurable: true, |
| 19 | + get: function() { |
| 20 | + return visibilityState; |
| 21 | + }, |
| 22 | + }); |
| 23 | + jest.spyOn(Date, 'now').mockImplementation(() => 12345); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + sendBeaconSpy.mockClear(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('attaches visibilitychange handler if sendClientReport is set to true', () => { |
| 31 | + const eventListenerSpy = jest.spyOn(document, 'addEventListener'); |
| 32 | + new SimpleTransport({ dsn: testDsn, sendClientReports: true }); |
| 33 | + expect(eventListenerSpy.mock.calls[0][0]).toBe('visibilitychange'); |
| 34 | + eventListenerSpy.mockRestore(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('doesnt attach visibilitychange handler if sendClientReport is set to false', () => { |
| 38 | + const eventListenerSpy = jest.spyOn(document, 'addEventListener'); |
| 39 | + new SimpleTransport({ dsn: testDsn, sendClientReports: false }); |
| 40 | + expect(eventListenerSpy).not.toHaveBeenCalled(); |
| 41 | + eventListenerSpy.mockRestore(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('sends beacon request when there are outcomes captured and visibility changed to `hidden`', () => { |
| 45 | + const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true }); |
| 46 | + |
| 47 | + transport.recordLostEvent(Outcome.BeforeSend, 'event'); |
| 48 | + |
| 49 | + visibilityState = 'hidden'; |
| 50 | + document.dispatchEvent(new Event('visibilitychange')); |
| 51 | + |
| 52 | + const outcomes = [{ reason: Outcome.BeforeSend, category: 'error', quantity: 1 }]; |
| 53 | + |
| 54 | + expect(sendBeaconSpy).toHaveBeenCalledWith( |
| 55 | + envelopeEndpoint, |
| 56 | + `{}\n{"type":"client_report"}\n{"timestamp":12.345,"discarded_events":${JSON.stringify(outcomes)}}`, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('doesnt send beacon request when there are outcomes captured, but visibility state did not change to `hidden`', () => { |
| 61 | + const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true }); |
| 62 | + transport.recordLostEvent(Outcome.BeforeSend, 'event'); |
| 63 | + |
| 64 | + visibilityState = 'visible'; |
| 65 | + document.dispatchEvent(new Event('visibilitychange')); |
| 66 | + |
| 67 | + expect(sendBeaconSpy).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('correctly serializes request with different categories/reasons pairs', () => { |
| 71 | + const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true }); |
| 72 | + |
| 73 | + transport.recordLostEvent(Outcome.BeforeSend, 'event'); |
| 74 | + transport.recordLostEvent(Outcome.BeforeSend, 'event'); |
| 75 | + transport.recordLostEvent(Outcome.SampleRate, 'transaction'); |
| 76 | + transport.recordLostEvent(Outcome.NetworkError, 'session'); |
| 77 | + transport.recordLostEvent(Outcome.NetworkError, 'session'); |
| 78 | + transport.recordLostEvent(Outcome.RateLimitBackoff, 'event'); |
| 79 | + |
| 80 | + visibilityState = 'hidden'; |
| 81 | + document.dispatchEvent(new Event('visibilitychange')); |
| 82 | + |
| 83 | + const outcomes = [ |
| 84 | + { reason: Outcome.BeforeSend, category: 'error', quantity: 2 }, |
| 85 | + { reason: Outcome.SampleRate, category: 'transaction', quantity: 1 }, |
| 86 | + { reason: Outcome.NetworkError, category: 'session', quantity: 2 }, |
| 87 | + { reason: Outcome.RateLimitBackoff, category: 'error', quantity: 1 }, |
| 88 | + ]; |
| 89 | + |
| 90 | + expect(sendBeaconSpy).toHaveBeenCalledWith( |
| 91 | + envelopeEndpoint, |
| 92 | + `{}\n{"type":"client_report"}\n{"timestamp":12.345,"discarded_events":${JSON.stringify(outcomes)}}`, |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
8 | 97 | it('doesnt provide sendEvent() implementation', () => {
|
9 | 98 | const transport = new SimpleTransport({ dsn: testDsn });
|
10 | 99 |
|
|
0 commit comments