|
| 1 | +import { NoopTransport } from '@sentry/core'; |
| 2 | +import { FetchTransport, setupBrowserTransport, XHRTransport } from '../../../src/transports'; |
| 3 | +import { SimpleTransport } from '../mocks/simpletransport'; |
| 4 | + |
| 5 | +const DSN = 'https://username@domain/123'; |
| 6 | + |
| 7 | +let fetchSupported = true; |
| 8 | +let getNativeFetchImplCalled = false; |
| 9 | + |
| 10 | +jest.mock('@sentry/utils', () => { |
| 11 | + const original = jest.requireActual('@sentry/utils'); |
| 12 | + return { |
| 13 | + ...original, |
| 14 | + supportsFetch(): boolean { |
| 15 | + return fetchSupported; |
| 16 | + }, |
| 17 | + getGlobalObject(): any { |
| 18 | + return { |
| 19 | + fetch: () => {}, |
| 20 | + }; |
| 21 | + }, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +jest.mock('@sentry/browser/src/transports/utils', () => { |
| 26 | + const original = jest.requireActual('@sentry/browser/src/transports/utils'); |
| 27 | + return { |
| 28 | + ...original, |
| 29 | + getNativeFetchImplementation() { |
| 30 | + getNativeFetchImplCalled = true; |
| 31 | + return { |
| 32 | + fetch: () => {}, |
| 33 | + }; |
| 34 | + }, |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +describe('setupBrowserTransport', () => { |
| 39 | + beforeEach(() => { |
| 40 | + getNativeFetchImplCalled = false; |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns NoopTransport if no dsn is passed', () => { |
| 44 | + const { transport, newTransport } = setupBrowserTransport({}); |
| 45 | + |
| 46 | + expect(transport).toBeDefined(); |
| 47 | + expect(transport).toBeInstanceOf(NoopTransport); |
| 48 | + expect(newTransport).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns the instantiated transport passed via the options', () => { |
| 52 | + const options = { dsn: DSN, transport: SimpleTransport }; |
| 53 | + const { transport, newTransport } = setupBrowserTransport(options); |
| 54 | + |
| 55 | + expect(transport).toBeDefined(); |
| 56 | + expect(transport).toBeInstanceOf(SimpleTransport); |
| 57 | + expect(newTransport).toBeUndefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns fetchTransports if fetch is supported', () => { |
| 61 | + const options = { dsn: DSN }; |
| 62 | + const { transport, newTransport } = setupBrowserTransport(options); |
| 63 | + |
| 64 | + expect(transport).toBeDefined(); |
| 65 | + expect(transport).toBeInstanceOf(FetchTransport); |
| 66 | + expect(newTransport).toBeDefined(); |
| 67 | + // This is a weird way of testing that `newTransport` is using fetch but it works. |
| 68 | + // Given that the new transports are functions, we cannot test their instance. |
| 69 | + // Totally open for suggestions how to test this better here |
| 70 | + expect(getNativeFetchImplCalled).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns xhrTransports if fetch is not supported', () => { |
| 74 | + fetchSupported = false; |
| 75 | + |
| 76 | + const options = { dsn: DSN }; |
| 77 | + const { transport, newTransport } = setupBrowserTransport(options); |
| 78 | + |
| 79 | + expect(transport).toBeDefined(); |
| 80 | + expect(transport).toBeInstanceOf(XHRTransport); |
| 81 | + expect(newTransport).toBeDefined(); |
| 82 | + // This is a weird way of testing that `newTransport` is using fetch but it works. |
| 83 | + // Given that the new transports are functions, we cannot test their instance. |
| 84 | + // Totally open for suggestions how to test this better here |
| 85 | + expect(getNativeFetchImplCalled).toBe(false); |
| 86 | + }); |
| 87 | +}); |
0 commit comments