/
sdk.test.ts
107 lines (96 loc) · 4.12 KB
/
sdk.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
import { Integration } from '@sentry/types';
import { installedIntegrations } from '../../src/integration';
import { initAndBind } from '../../src/sdk';
import { TestClient } from '../mocks/client';
declare var global: any;
const PUBLIC_DSN = 'https://username@domain/123';
jest.mock('@sentry/hub', () => ({
getCurrentHub(): {
bindClient(client: any): boolean;
getClient(): boolean;
} {
return {
getClient(): boolean {
return false;
},
bindClient(client: any): boolean {
client.setupIntegrations();
return true;
},
};
},
}));
class MockIntegration implements Integration {
public constructor(name: string) {
this.name = name;
}
public name: string;
public setupOnce: () => void = jest.fn();
}
describe('SDK', () => {
beforeEach(() => {
global.__SENTRY__ = {};
installedIntegrations.splice(0);
});
describe('initAndBind', () => {
test('installs default integrations', () => {
const DEFAULT_INTEGRATIONS: Integration[] = [
new MockIntegration('MockIntegration 1'),
new MockIntegration('MockIntegration 2'),
];
initAndBind(TestClient, { dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS });
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
});
test('not installs default integrations', () => {
const DEFAULT_INTEGRATIONS: Integration[] = [
new MockIntegration('MockIntegration 1'),
new MockIntegration('MockIntegration 2'),
];
initAndBind(TestClient, { dsn: PUBLIC_DSN, defaultIntegrations: false });
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(0);
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(0);
});
test('installs integrations provided through options', () => {
const integrations: Integration[] = [
new MockIntegration('MockIntegration 1'),
new MockIntegration('MockIntegration 2'),
];
initAndBind(TestClient, { dsn: PUBLIC_DSN, integrations });
expect((integrations[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
expect((integrations[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
});
test('installs merged default integrations, with overrides provided through options', () => {
const DEFAULT_INTEGRATIONS: Integration[] = [
new MockIntegration('MockIntegration 1'),
new MockIntegration('MockIntegration 2'),
];
const integrations: Integration[] = [
new MockIntegration('MockIntegration 1'),
new MockIntegration('MockIntegration 3'),
];
initAndBind(TestClient, { dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS, integrations });
// 'MockIntegration 1' should be overridden by the one with the same name provided through options
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(0);
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
expect((integrations[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
expect((integrations[1].setupOnce as jest.Mock).mock.calls.length).toBe(1);
});
test('installs integrations returned from a callback function', () => {
const DEFAULT_INTEGRATIONS: Integration[] = [
new MockIntegration('MockIntegration 1'),
new MockIntegration('MockIntegration 2'),
];
const newIntegration = new MockIntegration('MockIntegration 3');
initAndBind(TestClient, {
// Take only the first one and add a new one to it
defaultIntegrations: DEFAULT_INTEGRATIONS,
dsn: PUBLIC_DSN,
integrations: (integrations: Integration[]) => integrations.slice(0, 1).concat(newIntegration),
});
expect((DEFAULT_INTEGRATIONS[0].setupOnce as jest.Mock).mock.calls.length).toBe(1);
expect((newIntegration.setupOnce as jest.Mock).mock.calls.length).toBe(1);
expect((DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).mock.calls.length).toBe(0);
});
});
});