|
| 1 | +import { Integration } from '@sentry/types'; |
| 2 | +import { getIntegrationsToSetup } from '../../../src/integrations'; |
| 3 | + |
| 4 | +/** JSDoc */ |
| 5 | +class MockIntegration implements Integration { |
| 6 | + public constructor(name: string) { |
| 7 | + this.name = name; |
| 8 | + } |
| 9 | + public name: string; |
| 10 | +} |
| 11 | + |
| 12 | +describe('getIntegrationsToSetup', () => { |
| 13 | + it('works with empty array', () => { |
| 14 | + const integrations = getIntegrationsToSetup({ |
| 15 | + integrations: [], |
| 16 | + }); |
| 17 | + |
| 18 | + expect(integrations.map(i => i.name)).toEqual([]); |
| 19 | + }); |
| 20 | + |
| 21 | + it('works with single item', () => { |
| 22 | + const integrations = getIntegrationsToSetup({ |
| 23 | + integrations: [new MockIntegration('foo')], |
| 24 | + }); |
| 25 | + |
| 26 | + expect(integrations.map(i => i.name)).toEqual(['foo']); |
| 27 | + }); |
| 28 | + |
| 29 | + it('works with multiple items', () => { |
| 30 | + const integrations = getIntegrationsToSetup({ |
| 31 | + integrations: [new MockIntegration('foo'), new MockIntegration('bar')], |
| 32 | + }); |
| 33 | + |
| 34 | + expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
| 35 | + }); |
| 36 | + |
| 37 | + it('filter duplicated items', () => { |
| 38 | + const integrations = getIntegrationsToSetup({ |
| 39 | + integrations: [new MockIntegration('foo'), new MockIntegration('foo'), new MockIntegration('bar')], |
| 40 | + }); |
| 41 | + |
| 42 | + expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('filter duplicated items and always let first win', () => { |
| 46 | + const first = new MockIntegration('foo'); |
| 47 | + (first as any).order = 'first'; |
| 48 | + const second = new MockIntegration('foo'); |
| 49 | + (second as any).order = 'second'; |
| 50 | + |
| 51 | + const integrations = getIntegrationsToSetup({ |
| 52 | + integrations: [first, second, new MockIntegration('bar')], |
| 53 | + }); |
| 54 | + |
| 55 | + expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
| 56 | + expect((integrations[0] as any).order).toEqual('first'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('work with empty defaults', () => { |
| 60 | + const integrations = getIntegrationsToSetup({ |
| 61 | + defaultIntegrations: [], |
| 62 | + }); |
| 63 | + |
| 64 | + expect(integrations.map(i => i.name)).toEqual([]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('work with single defaults', () => { |
| 68 | + const integrations = getIntegrationsToSetup({ |
| 69 | + defaultIntegrations: [new MockIntegration('foo')], |
| 70 | + }); |
| 71 | + |
| 72 | + expect(integrations.map(i => i.name)).toEqual(['foo']); |
| 73 | + }); |
| 74 | + |
| 75 | + it('work with multiple defaults', () => { |
| 76 | + const integrations = getIntegrationsToSetup({ |
| 77 | + defaultIntegrations: [new MockIntegration('foo'), new MockIntegration('bar')], |
| 78 | + }); |
| 79 | + |
| 80 | + expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('work with user integrations and defaults and pick defaults first', () => { |
| 84 | + const integrations = getIntegrationsToSetup({ |
| 85 | + defaultIntegrations: [new MockIntegration('foo')], |
| 86 | + integrations: [new MockIntegration('bar')], |
| 87 | + }); |
| 88 | + |
| 89 | + expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
| 90 | + }); |
| 91 | + |
| 92 | + it('work with user integrations and defaults and filter duplicates', () => { |
| 93 | + const integrations = getIntegrationsToSetup({ |
| 94 | + defaultIntegrations: [new MockIntegration('foo'), new MockIntegration('foo')], |
| 95 | + integrations: [new MockIntegration('bar'), new MockIntegration('bar')], |
| 96 | + }); |
| 97 | + |
| 98 | + expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
| 99 | + }); |
| 100 | + |
| 101 | + it('user integrations override defaults', () => { |
| 102 | + const firstDefault = new MockIntegration('foo'); |
| 103 | + (firstDefault as any).order = 'firstDefault'; |
| 104 | + const secondDefault = new MockIntegration('bar'); |
| 105 | + (secondDefault as any).order = 'secondDefault'; |
| 106 | + const firstUser = new MockIntegration('foo'); |
| 107 | + (firstUser as any).order = 'firstUser'; |
| 108 | + const secondUser = new MockIntegration('bar'); |
| 109 | + (secondUser as any).order = 'secondUser'; |
| 110 | + |
| 111 | + const integrations = getIntegrationsToSetup({ |
| 112 | + defaultIntegrations: [firstDefault, secondDefault], |
| 113 | + integrations: [firstUser, secondUser], |
| 114 | + }); |
| 115 | + |
| 116 | + expect(integrations.map(i => i.name)).toEqual(['foo', 'bar']); |
| 117 | + expect((integrations[0] as any).order).toEqual('firstUser'); |
| 118 | + expect((integrations[1] as any).order).toEqual('secondUser'); |
| 119 | + }); |
| 120 | +}); |
0 commit comments