|
| 1 | +import { ClientReport } from '@sentry/types'; |
| 2 | + |
| 3 | +import { createClientReportEnvelope } from '../src/clientreport'; |
| 4 | +import { serializeEnvelope } from '../src/envelope'; |
| 5 | + |
| 6 | +const DEFAULT_DISCARDED_EVENTS: Array<ClientReport['discarded_events']> = [ |
| 7 | + { |
| 8 | + reason: 'before_send', |
| 9 | + category: 'event', |
| 10 | + quantity: 30, |
| 11 | + }, |
| 12 | + { |
| 13 | + reason: 'network_error', |
| 14 | + category: 'transaction', |
| 15 | + quantity: 23, |
| 16 | + }, |
| 17 | +]; |
| 18 | + |
| 19 | +const MOCK_DSN = 'https://[email protected]/1'; |
| 20 | + |
| 21 | +describe('createClientReportEnvelope', () => { |
| 22 | + const testTable: Array< |
| 23 | + [string, Parameters<typeof createClientReportEnvelope>[0], Parameters<typeof createClientReportEnvelope>[1]] |
| 24 | + > = [ |
| 25 | + ['with no discard reasons', [], undefined], |
| 26 | + ['with a dsn', [], MOCK_DSN], |
| 27 | + ['with discard reasons', DEFAULT_DISCARDED_EVENTS, MOCK_DSN], |
| 28 | + ]; |
| 29 | + it.each(testTable)('%s', (_: string, discardedEvents, dsn) => { |
| 30 | + const env = createClientReportEnvelope(discardedEvents, dsn); |
| 31 | + |
| 32 | + expect(env[0]).toEqual(dsn ? { dsn } : {}); |
| 33 | + |
| 34 | + const items = env[1]; |
| 35 | + expect(items).toHaveLength(1); |
| 36 | + const clientReportItem = items[0]; |
| 37 | + |
| 38 | + expect(clientReportItem[0]).toEqual({ type: 'client_report' }); |
| 39 | + expect(clientReportItem[1]).toEqual({ timestamp: expect.any(Number), discarded_events: discardedEvents }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('serializes an envelope', () => { |
| 43 | + const env = createClientReportEnvelope(DEFAULT_DISCARDED_EVENTS, MOCK_DSN, 123456); |
| 44 | + const serializedEnv = serializeEnvelope(env); |
| 45 | + expect(serializedEnv).toMatchInlineSnapshot(` |
| 46 | + "{\\"dsn\\":\\"https://[email protected]/1\\"} |
| 47 | + {\\"type\\":\\"client_report\\"} |
| 48 | + {\\"timestamp\\":123456,\\"discarded_events\\":[{\\"reason\\":\\"before_send\\",\\"category\\":\\"event\\",\\"quantity\\":30},{\\"reason\\":\\"network_error\\",\\"category\\":\\"transaction\\",\\"quantity\\":23}]}" |
| 49 | + `); |
| 50 | + }); |
| 51 | +}); |
0 commit comments