|
1 | | -/* eslint-disable import/no-unresolved, @typescript-eslint/no-unsafe-member-access */ |
2 | 1 | import * as Sentry from '@sentry/react-native'; |
3 | 2 | import * as React from 'react'; |
4 | 3 | import { Text, View } from 'react-native'; |
| 4 | +import { LaunchArguments } from "react-native-launch-arguments"; |
5 | 5 |
|
6 | 6 | import { getTestProps } from './utils/getTestProps'; |
| 7 | +import { fetchEvent } from './utils/fetchEvent'; |
| 8 | + |
| 9 | +const getSentryAuthToken = (): |
| 10 | + | { token: string } |
| 11 | + | { error: string } => { |
| 12 | + const { sentryAuthToken } = LaunchArguments.value<{ |
| 13 | + sentryAuthToken: unknown; |
| 14 | + }>(); |
| 15 | + |
| 16 | + if (typeof sentryAuthToken !== 'string') { |
| 17 | + return { error: 'Sentry Auth Token is required' }; |
| 18 | + } |
| 19 | + |
| 20 | + if (sentryAuthToken.length === 0) { |
| 21 | + return { error: 'Sentry Auth Token must not be empty' }; |
| 22 | + } |
| 23 | + |
| 24 | + return { token: sentryAuthToken }; |
| 25 | +}; |
7 | 26 |
|
8 | | -export { getTestProps }; |
9 | | -/** |
10 | | - * This screen is for internal end-to-end testing purposes only. Do not use. |
11 | | - * Not visible through the UI (no button to load it). |
12 | | - */ |
13 | | -// Deprecated in https://github.com/DefinitelyTyped/DefinitelyTyped/commit/f1b25591890978a92c610ce575ea2ba2bbde6a89 |
14 | | -// eslint-disable-next-line deprecation/deprecation |
15 | 27 | const EndToEndTestsScreen = (): JSX.Element => { |
16 | 28 | const [eventId, setEventId] = React.useState<string | null | undefined>(); |
| 29 | + const [error, setError] = React.useState<string>('No error'); |
| 30 | + |
| 31 | + async function assertEventReceived(eventId: string | undefined) { |
| 32 | + if (!eventId) { |
| 33 | + setError('Event ID is required'); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const value = getSentryAuthToken(); |
| 38 | + if ('error' in value) { |
| 39 | + setError(value.error); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + await fetchEvent(eventId, value.token); |
| 44 | + |
| 45 | + setEventId(eventId); |
| 46 | + } |
17 | 47 |
|
18 | | - // !!! WARNING: This is only for testing purposes. |
19 | | - // We only do this to render the eventId onto the UI for end to end tests. |
20 | 48 | React.useEffect(() => { |
21 | 49 | const client: Sentry.ReactNativeClient | undefined = Sentry.getClient(); |
| 50 | + |
| 51 | + if (!client) { |
| 52 | + setError('Client is not initialized'); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // WARNING: This is only for testing purposes. |
| 57 | + // We only do this to render the eventId onto the UI for end to end tests. |
22 | 58 | client.getOptions().beforeSend = (e) => { |
23 | | - setEventId(e.event_id || null); |
| 59 | + assertEventReceived(e.event_id); |
24 | 60 | return e; |
25 | 61 | }; |
26 | 62 | }, []); |
27 | 63 |
|
| 64 | + const testCases = [ |
| 65 | + { |
| 66 | + id: 'captureMessage', |
| 67 | + name: 'Capture Message', |
| 68 | + action: () => Sentry.captureMessage('React Native Test Message'), |
| 69 | + }, |
| 70 | + { |
| 71 | + id: 'captureException', |
| 72 | + name: 'Capture Exception', |
| 73 | + action: () => Sentry.captureException(new Error('captureException test')), |
| 74 | + }, |
| 75 | + { |
| 76 | + id: 'unhandledPromiseRejection', |
| 77 | + name: 'Unhandled Promise Rejection', |
| 78 | + action: async () => await Promise.reject(new Error('Unhandled Promise Rejection')), |
| 79 | + }, |
| 80 | + { |
| 81 | + id: 'close', |
| 82 | + name: 'Close', |
| 83 | + action: async () => await Sentry.close(), |
| 84 | + }, |
| 85 | + ]; |
| 86 | + |
28 | 87 | return ( |
29 | 88 | <View> |
| 89 | + <Text>{error}</Text> |
30 | 90 | <Text {...getTestProps('eventId')}>{eventId}</Text> |
31 | 91 | <Text {...getTestProps('clearEventId')} onPress={() => setEventId('')}> |
32 | 92 | Clear Event Id |
33 | 93 | </Text> |
34 | | - <Text |
35 | | - {...getTestProps('captureMessage')} |
36 | | - onPress={() => { |
37 | | - Sentry.captureMessage('React Native Test Message'); |
38 | | - }}> |
39 | | - captureMessage |
40 | | - </Text> |
41 | | - <Text |
42 | | - {...getTestProps('captureException')} |
43 | | - onPress={() => { |
44 | | - Sentry.captureException(new Error('captureException test')); |
45 | | - }}> |
46 | | - captureException |
47 | | - </Text> |
48 | | - <Text |
49 | | - onPress={async () => { |
50 | | - await Promise.reject(new Error('Unhandled Promise Rejection')); |
51 | | - }} |
52 | | - {...getTestProps('unhandledPromiseRejection')}> |
53 | | - Unhandled Promise Rejection |
54 | | - </Text> |
55 | | - <Text |
56 | | - {...getTestProps('close')} |
57 | | - onPress={async () => { |
58 | | - await Sentry.close(); |
59 | | - }}> |
60 | | - close |
61 | | - </Text> |
| 94 | + {testCases.map((testCase) => ( |
| 95 | + <Text key={testCase.id} {...getTestProps(testCase.id)} onPress={testCase.action}> |
| 96 | + {testCase.name} |
| 97 | + </Text> |
| 98 | + ))} |
62 | 99 | </View> |
63 | 100 | ); |
64 | 101 | }; |
|
0 commit comments