|
1 | 1 | import { getClient, metrics, setCurrentClient } from '@sentry/core'; |
2 | 2 | import { ReactNativeClient } from '../src/js'; |
| 3 | +import { mobileReplayIntegration } from '../src/js/replay/mobilereplay'; |
3 | 4 | import { getDefaultTestClientOptions } from './mocks/client'; |
4 | 5 | import { NATIVE } from './mockWrapper'; |
5 | 6 |
|
@@ -176,5 +177,61 @@ describe('Metrics', () => { |
176 | 177 | const sentMetric = beforeSendMetric.mock.calls[0]?.[0]; |
177 | 178 | expect(sentMetric).toBeDefined(); |
178 | 179 | }); |
| 180 | + |
| 181 | + it('metrics include replay_id when mobile replay integration is active', async () => { |
| 182 | + const mockReplayId = 'test-replay-id-123'; |
| 183 | + (NATIVE.getCurrentReplayId as jest.Mock).mockReturnValue(mockReplayId); |
| 184 | + |
| 185 | + const beforeSendMetric = jest.fn(metric => metric); |
| 186 | + |
| 187 | + const client = new ReactNativeClient({ |
| 188 | + ...getDefaultTestClientOptions({ |
| 189 | + dsn: EXAMPLE_DSN, |
| 190 | + enableMetrics: true, |
| 191 | + beforeSendMetric, |
| 192 | + integrations: [mobileReplayIntegration()], |
| 193 | + replaysSessionSampleRate: 1.0, |
| 194 | + }), |
| 195 | + }); |
| 196 | + |
| 197 | + setCurrentClient(client); |
| 198 | + client.init(); |
| 199 | + |
| 200 | + // Send a metric |
| 201 | + metrics.count('test_metric', 1); |
| 202 | + |
| 203 | + jest.advanceTimersByTime(10000); |
| 204 | + expect(beforeSendMetric).toHaveBeenCalled(); |
| 205 | + const sentMetric = beforeSendMetric.mock.calls[0]?.[0]; |
| 206 | + expect(sentMetric).toBeDefined(); |
| 207 | + expect(sentMetric.attributes).toBeDefined(); |
| 208 | + expect(sentMetric.attributes?.replay_id).toBe(mockReplayId); |
| 209 | + }); |
| 210 | + |
| 211 | + it('metrics do not include replay_id when replay integration is not active', async () => { |
| 212 | + (NATIVE.getCurrentReplayId as jest.Mock).mockReturnValue(null); |
| 213 | + |
| 214 | + const beforeSendMetric = jest.fn(metric => metric); |
| 215 | + |
| 216 | + const client = new ReactNativeClient({ |
| 217 | + ...getDefaultTestClientOptions({ |
| 218 | + dsn: EXAMPLE_DSN, |
| 219 | + enableMetrics: true, |
| 220 | + beforeSendMetric, |
| 221 | + }), |
| 222 | + }); |
| 223 | + |
| 224 | + setCurrentClient(client); |
| 225 | + client.init(); |
| 226 | + |
| 227 | + // Send a metric |
| 228 | + metrics.count('test_metric', 1); |
| 229 | + |
| 230 | + jest.advanceTimersByTime(10000); |
| 231 | + expect(beforeSendMetric).toHaveBeenCalled(); |
| 232 | + const sentMetric = beforeSendMetric.mock.calls[0]?.[0]; |
| 233 | + expect(sentMetric).toBeDefined(); |
| 234 | + expect(sentMetric.attributes?.replay_id).toBeUndefined(); |
| 235 | + }); |
179 | 236 | }); |
180 | 237 | }); |
0 commit comments