|
| 1 | +import { Integration } from '@sentry/types'; |
| 2 | +import { initAndBind } from '../../src/sdk'; |
| 3 | +import { TestClient } from '../mocks/client'; |
| 4 | + |
| 5 | +declare var global: any; |
| 6 | + |
| 7 | +class MockIntegration implements Integration { |
| 8 | + public name: string = 'MockIntegration'; |
| 9 | + public handler: () => void = jest.fn(); |
| 10 | + public install: () => void = () => { |
| 11 | + this.handler(); |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +describe('SDK', () => { |
| 16 | + beforeEach(() => { |
| 17 | + global.__SENTRY__ = { |
| 18 | + shim: undefined, |
| 19 | + stack: [], |
| 20 | + }; |
| 21 | + }); |
| 22 | + |
| 23 | + describe('initAndBind', () => { |
| 24 | + test('installs default integrations', () => { |
| 25 | + const DEFAULT_INTEGRATIONS: Integration[] = [ |
| 26 | + new MockIntegration(), |
| 27 | + new MockIntegration(), |
| 28 | + ]; |
| 29 | + initAndBind(TestClient, {}, DEFAULT_INTEGRATIONS); |
| 30 | + expect(DEFAULT_INTEGRATIONS[0].handler.mock.calls.length).toBe(1); |
| 31 | + expect(DEFAULT_INTEGRATIONS[1].handler.mock.calls.length).toBe(1); |
| 32 | + }); |
| 33 | + |
| 34 | + test('installs integrations provided through options', () => { |
| 35 | + const integrations: Integration[] = [ |
| 36 | + new MockIntegration(), |
| 37 | + new MockIntegration(), |
| 38 | + ]; |
| 39 | + initAndBind(TestClient, { integrations }, []); |
| 40 | + expect(integrations[0].handler.mock.calls.length).toBe(1); |
| 41 | + expect(integrations[1].handler.mock.calls.length).toBe(1); |
| 42 | + }); |
| 43 | + |
| 44 | + test('installs merged default integrations and one provided through options', () => { |
| 45 | + const DEFAULT_INTEGRATIONS: Integration[] = [ |
| 46 | + new MockIntegration(), |
| 47 | + new MockIntegration(), |
| 48 | + ]; |
| 49 | + const integrations: Integration[] = [ |
| 50 | + new MockIntegration(), |
| 51 | + new MockIntegration(), |
| 52 | + ]; |
| 53 | + initAndBind(TestClient, { integrations }, DEFAULT_INTEGRATIONS); |
| 54 | + expect(DEFAULT_INTEGRATIONS[0].handler.mock.calls.length).toBe(1); |
| 55 | + expect(DEFAULT_INTEGRATIONS[1].handler.mock.calls.length).toBe(1); |
| 56 | + expect(integrations[0].handler.mock.calls.length).toBe(1); |
| 57 | + expect(integrations[1].handler.mock.calls.length).toBe(1); |
| 58 | + }); |
| 59 | + |
| 60 | + test('installs integrations returned from a callback function', () => { |
| 61 | + const DEFAULT_INTEGRATIONS: Integration[] = [ |
| 62 | + new MockIntegration(), |
| 63 | + new MockIntegration(), |
| 64 | + ]; |
| 65 | + const newIntegration = new MockIntegration(); |
| 66 | + initAndBind( |
| 67 | + TestClient, |
| 68 | + { |
| 69 | + // Take only the first one and add a new one to it |
| 70 | + integrations: (integrations: Integration[]) => |
| 71 | + integrations.slice(0, 1).concat(newIntegration), |
| 72 | + }, |
| 73 | + DEFAULT_INTEGRATIONS, |
| 74 | + ); |
| 75 | + expect(DEFAULT_INTEGRATIONS[0].handler.mock.calls.length).toBe(1); |
| 76 | + expect(newIntegration.handler.mock.calls.length).toBe(1); |
| 77 | + expect(DEFAULT_INTEGRATIONS[1].handler.mock.calls.length).toBe(0); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments