|
| 1 | +import { addBreadcrumb, getClient } from '@sentry/core'; |
| 2 | + |
| 3 | +import { deeplinkIntegration } from '../../src/js/integrations/deeplink'; |
| 4 | + |
| 5 | +const mockGetInitialURL = jest.fn<Promise<string | null>, []>(); |
| 6 | +const mockAddEventListener = jest.fn<{ remove: () => void }, [string, (event: { url: string }) => void]>(); |
| 7 | + |
| 8 | +jest.mock('react-native', () => ({ |
| 9 | + Linking: { |
| 10 | + getInitialURL: (...args: unknown[]) => mockGetInitialURL(...args), |
| 11 | + addEventListener: (...args: Parameters<typeof mockAddEventListener>) => mockAddEventListener(...args), |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('@sentry/core', () => { |
| 16 | + const actual = jest.requireActual('@sentry/core'); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + addBreadcrumb: jest.fn(), |
| 20 | + getClient: jest.fn(), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +const mockAddBreadcrumb = addBreadcrumb as jest.Mock; |
| 25 | +const mockGetClient = getClient as jest.Mock; |
| 26 | + |
| 27 | +describe('deeplinkIntegration', () => { |
| 28 | + beforeEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + mockGetInitialURL.mockResolvedValue(null); |
| 31 | + mockAddEventListener.mockReturnValue({ remove: jest.fn() }); |
| 32 | + mockGetClient.mockReturnValue({ |
| 33 | + getOptions: () => ({ sendDefaultPii: false }), |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('cold start (getInitialURL)', () => { |
| 38 | + it('adds a breadcrumb when app opened via deep link', async () => { |
| 39 | + mockGetInitialURL.mockResolvedValue('myapp://profile/123?token=secret'); |
| 40 | + |
| 41 | + const integration = deeplinkIntegration(); |
| 42 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 43 | + |
| 44 | + await Promise.resolve(); // flush microtasks |
| 45 | + |
| 46 | + expect(mockAddBreadcrumb).toHaveBeenCalledWith( |
| 47 | + expect.objectContaining({ |
| 48 | + category: 'deeplink', |
| 49 | + type: 'navigation', |
| 50 | + }), |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('strips query params and ID segments when sendDefaultPii is false', async () => { |
| 55 | + mockGetInitialURL.mockResolvedValue('myapp://profile/123?token=secret'); |
| 56 | + |
| 57 | + const integration = deeplinkIntegration(); |
| 58 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 59 | + |
| 60 | + await Promise.resolve(); |
| 61 | + |
| 62 | + expect(mockAddBreadcrumb).toHaveBeenCalledWith( |
| 63 | + expect.objectContaining({ |
| 64 | + message: 'myapp://profile/<id>', |
| 65 | + data: { url: 'myapp://profile/<id>' }, |
| 66 | + }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('keeps full URL when sendDefaultPii is true', async () => { |
| 71 | + mockGetClient.mockReturnValue({ |
| 72 | + getOptions: () => ({ sendDefaultPii: true }), |
| 73 | + }); |
| 74 | + mockGetInitialURL.mockResolvedValue('myapp://profile/123?token=secret'); |
| 75 | + |
| 76 | + const integration = deeplinkIntegration(); |
| 77 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 78 | + |
| 79 | + await Promise.resolve(); |
| 80 | + |
| 81 | + expect(mockAddBreadcrumb).toHaveBeenCalledWith( |
| 82 | + expect.objectContaining({ |
| 83 | + message: 'myapp://profile/123?token=secret', |
| 84 | + data: { url: 'myapp://profile/123?token=secret' }, |
| 85 | + }), |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('does not add a breadcrumb when getInitialURL returns null', async () => { |
| 90 | + mockGetInitialURL.mockResolvedValue(null); |
| 91 | + |
| 92 | + const integration = deeplinkIntegration(); |
| 93 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 94 | + |
| 95 | + await Promise.resolve(); |
| 96 | + |
| 97 | + expect(mockAddBreadcrumb).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('does not throw when getInitialURL rejects', async () => { |
| 101 | + mockGetInitialURL.mockRejectedValue(new Error('Linking error')); |
| 102 | + |
| 103 | + const integration = deeplinkIntegration(); |
| 104 | + expect(() => |
| 105 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]), |
| 106 | + ).not.toThrow(); |
| 107 | + |
| 108 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 109 | + expect(mockAddBreadcrumb).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('warm open (url event)', () => { |
| 114 | + it('adds a breadcrumb when a url event is received', () => { |
| 115 | + const integration = deeplinkIntegration(); |
| 116 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 117 | + |
| 118 | + const handler = mockAddEventListener.mock.calls[0]?.[1]; |
| 119 | + handler?.({ url: 'myapp://notifications/456' }); |
| 120 | + |
| 121 | + expect(mockAddBreadcrumb).toHaveBeenCalledWith( |
| 122 | + expect.objectContaining({ |
| 123 | + category: 'deeplink', |
| 124 | + type: 'navigation', |
| 125 | + }), |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('strips query params and ID segments on url event when sendDefaultPii is false', () => { |
| 130 | + const integration = deeplinkIntegration(); |
| 131 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 132 | + |
| 133 | + const handler = mockAddEventListener.mock.calls[0]?.[1]; |
| 134 | + handler?.({ url: 'myapp://notifications/456?ref=push' }); |
| 135 | + |
| 136 | + expect(mockAddBreadcrumb).toHaveBeenCalledWith( |
| 137 | + expect.objectContaining({ |
| 138 | + message: 'myapp://notifications/<id>', |
| 139 | + data: { url: 'myapp://notifications/<id>' }, |
| 140 | + }), |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + it('keeps full URL on url event when sendDefaultPii is true', () => { |
| 145 | + mockGetClient.mockReturnValue({ |
| 146 | + getOptions: () => ({ sendDefaultPii: true }), |
| 147 | + }); |
| 148 | + |
| 149 | + const integration = deeplinkIntegration(); |
| 150 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 151 | + |
| 152 | + const handler = mockAddEventListener.mock.calls[0]?.[1]; |
| 153 | + handler?.({ url: 'myapp://notifications/456?ref=push' }); |
| 154 | + |
| 155 | + expect(mockAddBreadcrumb).toHaveBeenCalledWith( |
| 156 | + expect.objectContaining({ |
| 157 | + message: 'myapp://notifications/456?ref=push', |
| 158 | + data: { url: 'myapp://notifications/456?ref=push' }, |
| 159 | + }), |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + it('registers the url event listener on setup', () => { |
| 164 | + const integration = deeplinkIntegration(); |
| 165 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 166 | + |
| 167 | + expect(mockAddEventListener).toHaveBeenCalledWith('url', expect.any(Function)); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('URL sanitization', () => { |
| 172 | + it('does not alter non-ID path segments', async () => { |
| 173 | + mockGetInitialURL.mockResolvedValue('myapp://settings/profile'); |
| 174 | + |
| 175 | + const integration = deeplinkIntegration(); |
| 176 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 177 | + |
| 178 | + await Promise.resolve(); |
| 179 | + |
| 180 | + expect(mockAddBreadcrumb).toHaveBeenCalledWith( |
| 181 | + expect.objectContaining({ |
| 182 | + message: 'myapp://settings/profile', |
| 183 | + }), |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it('replaces UUID-like segments', async () => { |
| 188 | + mockGetInitialURL.mockResolvedValue('myapp://order/a1b2c3d4-e5f6-7890-abcd-ef1234567890'); |
| 189 | + |
| 190 | + const integration = deeplinkIntegration(); |
| 191 | + integration.setup?.({} as Parameters<NonNullable<typeof integration.setup>>[0]); |
| 192 | + |
| 193 | + await Promise.resolve(); |
| 194 | + |
| 195 | + const call = mockAddBreadcrumb.mock.calls[0]?.[0]; |
| 196 | + expect(call?.message).not.toContain('a1b2c3d4'); |
| 197 | + }); |
| 198 | + }); |
| 199 | +}); |
0 commit comments