|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { beforeSendHandler, updateConsentContext } from '../helpers' |
| 3 | + |
| 4 | +const mockScope = { |
| 5 | + setContext: vi.fn(), |
| 6 | +} |
| 7 | + |
| 8 | +const isDevMock = vi.hoisted(() => vi.fn(() => false)) |
| 9 | +const getConsentSnapshotMock = vi.hoisted(() => vi.fn(() => ({ |
| 10 | + analytics: true, |
| 11 | +}))) |
| 12 | + |
| 13 | +vi.mock('@components/scripts/utils/environmentClient', () => ({ |
| 14 | + isDev: isDevMock, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@components/scripts/store/consent', () => ({ |
| 18 | + getConsentSnapshot: getConsentSnapshotMock, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@sentry/browser', () => ({ |
| 22 | + getCurrentScope: () => mockScope, |
| 23 | +})) |
| 24 | + |
| 25 | +const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 26 | + |
| 27 | +const createEvent = (): Parameters<typeof beforeSendHandler>[0] => ({ |
| 28 | + user: { 'ip_address': '127.0.0.1' }, |
| 29 | + request: { headers: { 'user-agent': 'test' } }, |
| 30 | + breadcrumbs: [{ message: 'clicked' }], |
| 31 | +}) as Parameters<typeof beforeSendHandler>[0] |
| 32 | + |
| 33 | +describe('sentry helpers', () => { |
| 34 | + beforeEach(() => { |
| 35 | + vi.clearAllMocks() |
| 36 | + isDevMock.mockReturnValue(false) |
| 37 | + getConsentSnapshotMock.mockReturnValue({ analytics: true }) |
| 38 | + consoleLogSpy.mockClear() |
| 39 | + }) |
| 40 | + |
| 41 | + describe('beforeSendHandler', () => { |
| 42 | + it('skips sending events in development', () => { |
| 43 | + isDevMock.mockReturnValue(true) |
| 44 | + const event = createEvent() |
| 45 | + |
| 46 | + const result = beforeSendHandler(event) |
| 47 | + |
| 48 | + expect(result).toBeNull() |
| 49 | + expect(getConsentSnapshotMock).not.toHaveBeenCalled() |
| 50 | + }) |
| 51 | + |
| 52 | + it('returns event unchanged when analytics consent exists', () => { |
| 53 | + getConsentSnapshotMock.mockReturnValue({ analytics: true }) |
| 54 | + |
| 55 | + const event = createEvent() |
| 56 | + const result = beforeSendHandler(event) |
| 57 | + |
| 58 | + expect(result).toBe(event) |
| 59 | + expect(event.user?.ip_address).toBe('127.0.0.1') |
| 60 | + expect(event.request?.headers).toEqual({ 'user-agent': 'test' }) |
| 61 | + expect(event.breadcrumbs).toHaveLength(1) |
| 62 | + }) |
| 63 | + |
| 64 | + it('scrubs PII when analytics consent is missing', () => { |
| 65 | + getConsentSnapshotMock.mockReturnValue({ analytics: false }) |
| 66 | + |
| 67 | + const event = createEvent() |
| 68 | + const result = beforeSendHandler(event) |
| 69 | + |
| 70 | + expect(result).toBe(event) |
| 71 | + expect(event.user?.ip_address).toBeUndefined() |
| 72 | + expect(event.request?.headers).toBeUndefined() |
| 73 | + expect(event.breadcrumbs).toHaveLength(0) |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe('updateConsentContext', () => { |
| 78 | + it('sets consent context and logs status', () => { |
| 79 | + updateConsentContext(true) |
| 80 | + |
| 81 | + expect(mockScope.setContext).toHaveBeenCalledWith('consent', { |
| 82 | + analytics: true, |
| 83 | + timestamp: expect.any(String), |
| 84 | + }) |
| 85 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 86 | + '🔒 Sentry PII enabled based on analytics consent' |
| 87 | + ) |
| 88 | + }) |
| 89 | + }) |
| 90 | +}) |
0 commit comments