|
| 1 | +import type { Client } from '@sentry/core'; |
| 2 | +import { primitiveTagIntegration } from '../../src/js/integrations/primitiveTagIntegration'; |
| 3 | +import { NATIVE } from '../../src/js/wrapper'; |
| 4 | +import { setupTestClient } from '../mocks/client'; |
| 5 | + |
| 6 | +describe('primitiveTagIntegration', () => { |
| 7 | + beforeEach(() => { |
| 8 | + jest.clearAllMocks(); |
| 9 | + setupTestClient(); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + jest.resetAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('integration setup', () => { |
| 17 | + it('sets up beforeSendEvent handler', () => { |
| 18 | + const integration = primitiveTagIntegration(); |
| 19 | + const mockClient = { |
| 20 | + on: jest.fn(), |
| 21 | + } as any; |
| 22 | + |
| 23 | + integration.setup!(mockClient); |
| 24 | + |
| 25 | + expect(mockClient.on).toHaveBeenCalledWith('beforeSendEvent', expect.any(Function)); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('beforeSendEvent processing', () => { |
| 30 | + let beforeSendEventHandler: (event: any) => void; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + const integration = primitiveTagIntegration(); |
| 34 | + const mockClient = { |
| 35 | + on: jest.fn((eventName, handler) => { |
| 36 | + if (eventName === 'beforeSendEvent') { |
| 37 | + beforeSendEventHandler = handler; |
| 38 | + } |
| 39 | + }), |
| 40 | + } as any; |
| 41 | + |
| 42 | + integration.setup!(mockClient); |
| 43 | + }); |
| 44 | + |
| 45 | + it('handles events without tags', () => { |
| 46 | + const event = { message: 'test' }; |
| 47 | + |
| 48 | + expect(() => beforeSendEventHandler(event)).not.toThrow(); |
| 49 | + expect(event).toEqual({ message: 'test' }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles events with empty tags object', () => { |
| 53 | + const event = { tags: {} }; |
| 54 | + |
| 55 | + expect(() => beforeSendEventHandler(event)).not.toThrow(); |
| 56 | + expect(event.tags).toEqual({}); |
| 57 | + }); |
| 58 | + |
| 59 | + it('handles events with null tags', () => { |
| 60 | + const event = { tags: null }; |
| 61 | + |
| 62 | + expect(() => beforeSendEventHandler(event)).not.toThrow(); |
| 63 | + expect(event.tags).toBeNull(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('integration with native processor', () => { |
| 68 | + it('sets primitiveProcessor to PrimitiveToString function', () => { |
| 69 | + const integration = primitiveTagIntegration(); |
| 70 | + NATIVE.enableNative = true; |
| 71 | + jest.spyOn(NATIVE, '_setPrimitiveProcessor'); |
| 72 | + |
| 73 | + integration.afterAllSetup!({ getOptions: () => ({}) } as Client); |
| 74 | + |
| 75 | + expect(NATIVE._setPrimitiveProcessor).toHaveBeenCalledWith(expect.any(Function)); |
| 76 | + |
| 77 | + // Verify the function passed is PrimitiveToString |
| 78 | + const passedFunction = (NATIVE._setPrimitiveProcessor as jest.Mock).mock.calls[0][0]; |
| 79 | + expect(passedFunction(true)).toBe('True'); |
| 80 | + expect(passedFunction(false)).toBe('False'); |
| 81 | + expect(passedFunction(null)).toBe(''); |
| 82 | + expect(passedFunction(42)).toBe('42'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not set processor when native is disabled', () => { |
| 86 | + const integration = primitiveTagIntegration(); |
| 87 | + NATIVE.enableNative = false; |
| 88 | + jest.spyOn(NATIVE, '_setPrimitiveProcessor'); |
| 89 | + |
| 90 | + integration.afterAllSetup!({ getOptions: () => ({}) } as Client); |
| 91 | + |
| 92 | + expect(NATIVE._setPrimitiveProcessor).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments