|
| 1 | +import '../../../../tests/component-view/mocks'; |
| 2 | +import React from 'react'; |
| 3 | +import { FlatList, Text, View } from 'react-native'; |
| 4 | +import { useNavigation, useRoute } from '@react-navigation/native'; |
| 5 | +import { fireEvent, waitFor } from '@testing-library/react-native'; |
| 6 | + |
| 7 | +import { renderScreenWithRoutes } from '../../../../tests/component-view/render'; |
| 8 | +import { describeForPlatforms } from '../../../../tests/component-view/platform'; |
| 9 | +import { |
| 10 | + buildNotificationsState, |
| 11 | + MOCK_NOTIFICATIONS, |
| 12 | +} from '../../../../tests/component-view/presets/notifications'; |
| 13 | +import { |
| 14 | + MOCK_FEATURE_ANNOUNCEMENT_NOTIFICATIONS, |
| 15 | + MOCK_ON_CHAIN_NOTIFICATIONS, |
| 16 | +} from '../../../components/UI/Notification/__mocks__/mock_notifications'; |
| 17 | +import Routes from '../../../constants/navigation/Routes'; |
| 18 | +import { NotificationMenuViewSelectorsIDs } from './NotificationMenuView.testIds'; |
| 19 | +import { NotificationsViewSelectorsIDs } from './NotificationsView.testIds'; |
| 20 | +import NotificationsView from './'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Component-view coverage for smoke `enable-notifications-after-onboarding`. |
| 24 | + * |
| 25 | + * Smoke spec: tests/smoke/notifications/enable-notifications-after-onboarding.spec.ts |
| 26 | + * |
| 27 | + * Notifications are seeded into Redux via `buildNotificationsState` (controllers |
| 28 | + * + remote feature flag), mirroring what the smoke E2E gets via |
| 29 | + * `mockNotificationServices` mockttp responses — no nock needed at the view |
| 30 | + * layer. `IS_TEST=true` (set at config-load time in `jest.config.view.js`) |
| 31 | + * flips `isNotificationsFeatureEnabled` on without mocking the config module. |
| 32 | + */ |
| 33 | + |
| 34 | +const NOTIFICATIONS_DETAILS_PROBE_TEST_ID = 'notifications-details-probe'; |
| 35 | +const NOTIFICATIONS_DETAILS_PROBE_ID_TEST_ID = 'notifications-details-probe-id'; |
| 36 | +const NOTIFICATIONS_DETAILS_BACK_TEST_ID = |
| 37 | + 'notifications-details-probe-back-button'; |
| 38 | + |
| 39 | +/** |
| 40 | + * Lightweight `NotificationsDetails` stand-in. Avoids needing the real |
| 41 | + * `NotificationComponentState` machinery (block-explorer footers, asset rows…) |
| 42 | + * unrelated to this flow. Mirrors the back-navigation contract that |
| 43 | + * `NotificationDetailsView.tapOnBackButton()` exercises in the smoke spec. |
| 44 | + */ |
| 45 | +function NotificationsDetailsProbe() { |
| 46 | + const route = useRoute(); |
| 47 | + const navigation = useNavigation(); |
| 48 | + const params = route.params as { notification?: { id?: string } } | undefined; |
| 49 | + return ( |
| 50 | + <View testID={NOTIFICATIONS_DETAILS_PROBE_TEST_ID}> |
| 51 | + <Text>Notification Details</Text> |
| 52 | + <Text testID={NOTIFICATIONS_DETAILS_PROBE_ID_TEST_ID}> |
| 53 | + {params?.notification?.id ?? ''} |
| 54 | + </Text> |
| 55 | + <Text |
| 56 | + testID={NOTIFICATIONS_DETAILS_BACK_TEST_ID} |
| 57 | + onPress={() => navigation.goBack()} |
| 58 | + > |
| 59 | + Back |
| 60 | + </Text> |
| 61 | + </View> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +function renderNotificationsScreen( |
| 66 | + notifications: typeof MOCK_NOTIFICATIONS = MOCK_NOTIFICATIONS, |
| 67 | +) { |
| 68 | + return renderScreenWithRoutes( |
| 69 | + NotificationsView as unknown as React.ComponentType, |
| 70 | + { name: 'NotificationsView' }, |
| 71 | + [ |
| 72 | + { |
| 73 | + name: Routes.NOTIFICATIONS.DETAILS, |
| 74 | + Component: NotificationsDetailsProbe, |
| 75 | + }, |
| 76 | + ], |
| 77 | + { state: buildNotificationsState({ notifications }) }, |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +describeForPlatforms('Notifications view (list + details flow)', () => { |
| 82 | + /** |
| 83 | + * The smoke spec inspects the rendered list of notifications. In jest, |
| 84 | + * `FlatList` never receives layout metrics so it only renders the first row |
| 85 | + * — instead of fighting virtualization we assert on the FlatList `data` |
| 86 | + * prop, which is the same source the device-rendered list reads from. |
| 87 | + */ |
| 88 | + it('exposes the full seeded notifications list to the FlatList data source', () => { |
| 89 | + const result = renderNotificationsScreen(); |
| 90 | + const flatList = result.UNSAFE_getByType(FlatList); |
| 91 | + |
| 92 | + expect( |
| 93 | + result.getByTestId(NotificationsViewSelectorsIDs.NOTIFICATIONS_CONTAINER), |
| 94 | + ).toBeOnTheScreen(); |
| 95 | + |
| 96 | + const data = (flatList.props as { data?: typeof MOCK_NOTIFICATIONS }).data; |
| 97 | + expect(data).toHaveLength(MOCK_NOTIFICATIONS.length); |
| 98 | + |
| 99 | + const seededIds = new Set(MOCK_NOTIFICATIONS.map((n) => n.id)); |
| 100 | + data?.forEach((n) => { |
| 101 | + expect(seededIds.has(n.id)).toBe(true); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + /** |
| 106 | + * Seed only the feature announcement so it's the first (and only) row in |
| 107 | + * the FlatList's initial render window — proves the same tap → details → |
| 108 | + * back path the smoke spec asserts on, without depending on virtualization. |
| 109 | + */ |
| 110 | + it('opens details for a feature announcement and returns on back', async () => { |
| 111 | + const featureAnnouncement = MOCK_FEATURE_ANNOUNCEMENT_NOTIFICATIONS[0]; |
| 112 | + const result = renderNotificationsScreen([featureAnnouncement]); |
| 113 | + |
| 114 | + fireEvent.press( |
| 115 | + await result.findByTestId( |
| 116 | + NotificationMenuViewSelectorsIDs.ITEM(featureAnnouncement.id), |
| 117 | + ), |
| 118 | + ); |
| 119 | + |
| 120 | + await waitFor(() => { |
| 121 | + expect( |
| 122 | + result.getByTestId(NOTIFICATIONS_DETAILS_PROBE_TEST_ID), |
| 123 | + ).toBeOnTheScreen(); |
| 124 | + }); |
| 125 | + expect( |
| 126 | + result.getByTestId(NOTIFICATIONS_DETAILS_PROBE_ID_TEST_ID), |
| 127 | + ).toHaveTextContent(featureAnnouncement.id); |
| 128 | + |
| 129 | + fireEvent.press(result.getByTestId(NOTIFICATIONS_DETAILS_BACK_TEST_ID)); |
| 130 | + |
| 131 | + await waitFor(() => { |
| 132 | + expect( |
| 133 | + result.getByTestId( |
| 134 | + NotificationsViewSelectorsIDs.NOTIFICATIONS_CONTAINER, |
| 135 | + ), |
| 136 | + ).toBeOnTheScreen(); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('opens details for a wallet notification and returns on back', async () => { |
| 141 | + const walletNotification = MOCK_ON_CHAIN_NOTIFICATIONS[0]; |
| 142 | + const result = renderNotificationsScreen([walletNotification]); |
| 143 | + |
| 144 | + fireEvent.press( |
| 145 | + await result.findByTestId( |
| 146 | + NotificationMenuViewSelectorsIDs.ITEM(walletNotification.id), |
| 147 | + ), |
| 148 | + ); |
| 149 | + |
| 150 | + await waitFor(() => { |
| 151 | + expect( |
| 152 | + result.getByTestId(NOTIFICATIONS_DETAILS_PROBE_TEST_ID), |
| 153 | + ).toBeOnTheScreen(); |
| 154 | + }); |
| 155 | + expect( |
| 156 | + result.getByTestId(NOTIFICATIONS_DETAILS_PROBE_ID_TEST_ID), |
| 157 | + ).toHaveTextContent(walletNotification.id); |
| 158 | + |
| 159 | + fireEvent.press(result.getByTestId(NOTIFICATIONS_DETAILS_BACK_TEST_ID)); |
| 160 | + |
| 161 | + await waitFor(() => { |
| 162 | + expect( |
| 163 | + result.getByTestId( |
| 164 | + NotificationsViewSelectorsIDs.NOTIFICATIONS_CONTAINER, |
| 165 | + ), |
| 166 | + ).toBeOnTheScreen(); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments