|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { SinonStub, stub } from 'sinon'; |
| 3 | +import { Status, Transports } from '../../src'; |
| 4 | + |
| 5 | +const testDSN = 'https://[email protected]/42'; |
| 6 | +const transportUrl = 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7'; |
| 7 | +const payload = { |
| 8 | + event_id: '1337', |
| 9 | + message: 'Pickle Rick', |
| 10 | + user: { |
| 11 | + username: 'Morty', |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +let sendBeacon: SinonStub; |
| 16 | +let transport: Transports.BaseTransport; |
| 17 | + |
| 18 | +describe('BeaconTransport', () => { |
| 19 | + beforeEach(() => { |
| 20 | + sendBeacon = stub(window.navigator, 'sendBeacon'); |
| 21 | + transport = new Transports.BeaconTransport({ dsn: testDSN }); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + sendBeacon.restore(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('inherits composeEndpointUrl() implementation', () => { |
| 29 | + expect(transport.url).equal(transportUrl); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('send()', async () => { |
| 33 | + it('sends a request to Sentry servers', async () => { |
| 34 | + sendBeacon.returns(true); |
| 35 | + |
| 36 | + return transport.send(payload).then(res => { |
| 37 | + expect(res.status).equal(Status.Success); |
| 38 | + expect(sendBeacon.calledOnce).equal(true); |
| 39 | + expect(sendBeacon.calledWith(transportUrl, JSON.stringify(payload))).equal(true); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('rejects with failed status', async () => { |
| 44 | + sendBeacon.returns(false); |
| 45 | + |
| 46 | + return transport.send(payload).catch(res => { |
| 47 | + expect(res.status).equal(Status.Failed); |
| 48 | + expect(sendBeacon.calledOnce).equal(true); |
| 49 | + expect(sendBeacon.calledWith(transportUrl, JSON.stringify(payload))).equal(true); |
| 50 | + }); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments