-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbase.test.ts
129 lines (101 loc) · 4.81 KB
/
base.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { BaseTransport } from '../../../src/transports/base';
const testDsn = 'https://[email protected]/42';
const envelopeEndpoint = 'https://sentry.io/api/42/envelope/?sentry_key=123&sentry_version=7';
class SimpleTransport extends BaseTransport {}
describe('BaseTransport', () => {
describe('Client Reports', () => {
const sendBeaconSpy = jest.fn();
let visibilityState: string;
beforeAll(() => {
navigator.sendBeacon = sendBeaconSpy;
Object.defineProperty(document, 'visibilityState', {
configurable: true,
get: function() {
return visibilityState;
},
});
jest.spyOn(Date, 'now').mockImplementation(() => 12345);
});
beforeEach(() => {
sendBeaconSpy.mockClear();
});
it('attaches visibilitychange handler if sendClientReport is set to true', () => {
const eventListenerSpy = jest.spyOn(document, 'addEventListener');
new SimpleTransport({ dsn: testDsn, sendClientReports: true });
expect(eventListenerSpy.mock.calls[0][0]).toBe('visibilitychange');
eventListenerSpy.mockRestore();
});
it('doesnt attach visibilitychange handler if sendClientReport is set to false', () => {
const eventListenerSpy = jest.spyOn(document, 'addEventListener');
new SimpleTransport({ dsn: testDsn, sendClientReports: false });
expect(eventListenerSpy).not.toHaveBeenCalled();
eventListenerSpy.mockRestore();
});
it('sends beacon request when there are outcomes captured and visibility changed to `hidden`', () => {
const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true });
transport.recordLostEvent('before_send', 'event');
visibilityState = 'hidden';
document.dispatchEvent(new Event('visibilitychange'));
const outcomes = [{ reason: 'before_send', category: 'error', quantity: 1 }];
expect(sendBeaconSpy).toHaveBeenCalledWith(
envelopeEndpoint,
`{}\n{"type":"client_report"}\n{"timestamp":12.345,"discarded_events":${JSON.stringify(outcomes)}}`,
);
});
it('doesnt send beacon request when there are outcomes captured, but visibility state did not change to `hidden`', () => {
const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true });
transport.recordLostEvent('before_send', 'event');
visibilityState = 'visible';
document.dispatchEvent(new Event('visibilitychange'));
expect(sendBeaconSpy).not.toHaveBeenCalled();
});
it('correctly serializes request with different categories/reasons pairs', () => {
const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true });
transport.recordLostEvent('before_send', 'event');
transport.recordLostEvent('before_send', 'event');
transport.recordLostEvent('sample_rate', 'transaction');
transport.recordLostEvent('network_error', 'session');
transport.recordLostEvent('network_error', 'session');
transport.recordLostEvent('ratelimit_backoff', 'event');
visibilityState = 'hidden';
document.dispatchEvent(new Event('visibilitychange'));
const outcomes = [
{ reason: 'before_send', category: 'error', quantity: 2 },
{ reason: 'sample_rate', category: 'transaction', quantity: 1 },
{ reason: 'network_error', category: 'session', quantity: 2 },
{ reason: 'ratelimit_backoff', category: 'error', quantity: 1 },
];
expect(sendBeaconSpy).toHaveBeenCalledWith(
envelopeEndpoint,
`{}\n{"type":"client_report"}\n{"timestamp":12.345,"discarded_events":${JSON.stringify(outcomes)}}`,
);
});
it('attaches DSN to envelope header if tunnel is configured', () => {
const tunnel = 'https://hello.com/world';
const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true, tunnel });
transport.recordLostEvent('before_send', 'event');
visibilityState = 'hidden';
document.dispatchEvent(new Event('visibilitychange'));
const outcomes = [{ reason: 'before_send', category: 'error', quantity: 1 }];
expect(sendBeaconSpy).toHaveBeenCalledWith(
tunnel,
`{"dsn":"${testDsn}"}\n{"type":"client_report"}\n{"timestamp":12.345,"discarded_events":${JSON.stringify(
outcomes,
)}}`,
);
});
});
it('doesnt provide sendEvent() implementation', () => {
const transport = new SimpleTransport({ dsn: testDsn });
try {
void transport.sendEvent({});
} catch (e) {
expect(e.message).toBe('Transport Class has to implement `sendEvent` method');
}
});
it('has correct endpoint url', () => {
const transport = new SimpleTransport({ dsn: testDsn });
// eslint-disable-next-line deprecation/deprecation
expect(transport.url).toBe('https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7');
});
});