|
| 1 | +import { consoleLoggingIntegration } from '@sentry/browser'; |
| 2 | +import type { Integration } from '@sentry/core'; |
| 3 | +import { getDefaultIntegrations } from '../../src/js/integrations/default'; |
| 4 | +import { logEnricherIntegration } from '../../src/js/integrations/logEnricherIntegration'; |
| 5 | +import type { ReactNativeClientOptions } from '../../src/js/options'; |
| 6 | +import { notWeb } from '../../src/js/utils/environment'; |
| 7 | + |
| 8 | +jest.mock('../../src/js/utils/environment', () => { |
| 9 | + const actual = jest.requireActual('../../src/js/utils/environment'); |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + notWeb: jest.fn(() => true), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +const logEnricherIntegrationName = logEnricherIntegration().name; |
| 17 | +const consoleLoggingIntegrationName = consoleLoggingIntegration().name; |
| 18 | + |
| 19 | +describe('getDefaultIntegrations - logging integrations', () => { |
| 20 | + beforeEach(() => { |
| 21 | + (notWeb as jest.Mock).mockReturnValue(true); |
| 22 | + }); |
| 23 | + |
| 24 | + const createOptions = (overrides: Partial<ReactNativeClientOptions>): ReactNativeClientOptions => { |
| 25 | + return { |
| 26 | + dsn: 'https://example.com/1', |
| 27 | + enableNative: true, |
| 28 | + ...overrides, |
| 29 | + } as ReactNativeClientOptions; |
| 30 | + }; |
| 31 | + |
| 32 | + const getIntegrationNames = (options: ReactNativeClientOptions): string[] => { |
| 33 | + const integrations = getDefaultIntegrations(options); |
| 34 | + return integrations.map((integration: Integration) => integration.name); |
| 35 | + }; |
| 36 | + |
| 37 | + it('does not add logging integrations when enableLogs is falsy', () => { |
| 38 | + const names = getIntegrationNames(createOptions({ enableLogs: false })); |
| 39 | + |
| 40 | + expect(names).not.toContain(logEnricherIntegrationName); |
| 41 | + expect(names).not.toContain(consoleLoggingIntegrationName); |
| 42 | + }); |
| 43 | + |
| 44 | + it('adds logging integrations when enableLogs is true and logsOrigin is not native', () => { |
| 45 | + const names = getIntegrationNames(createOptions({ enableLogs: true })); |
| 46 | + |
| 47 | + expect(names).toContain(logEnricherIntegrationName); |
| 48 | + expect(names).toContain(consoleLoggingIntegrationName); |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not add logging integrations when logsOrigin is native', () => { |
| 52 | + const names = getIntegrationNames( |
| 53 | + createOptions({ |
| 54 | + enableLogs: true, |
| 55 | + logsOrigin: 'native' as unknown as ReactNativeClientOptions['logsOrigin'], |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + expect(names).not.toContain(logEnricherIntegrationName); |
| 60 | + expect(names).not.toContain(consoleLoggingIntegrationName); |
| 61 | + }); |
| 62 | + |
| 63 | + it.each([ |
| 64 | + ['all', true], |
| 65 | + ['js', true], |
| 66 | + ['native', false], |
| 67 | + ])('handles logsOrigin %s correctly', (logsOrigin, shouldInclude) => { |
| 68 | + const names = getIntegrationNames( |
| 69 | + createOptions({ |
| 70 | + enableLogs: true, |
| 71 | + logsOrigin: logsOrigin as unknown as ReactNativeClientOptions['logsOrigin'], |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + expect(names.includes(logEnricherIntegrationName)).toBe(shouldInclude); |
| 76 | + expect(names.includes(consoleLoggingIntegrationName)).toBe(shouldInclude); |
| 77 | + }); |
| 78 | +}); |
0 commit comments