|
1 | | -import { act } from '@testing-library/react-hooks'; |
2 | | -import { waitFor } from '@testing-library/react-native'; |
3 | | -import { AvatarAccountType } from '../../../../component-library/components/Avatars/Avatar'; |
4 | | -// eslint-disable-next-line import/no-namespace |
5 | | -import * as UseSwitchNotificationsModule from '../../../../util/notifications/hooks/useSwitchNotifications'; |
| 1 | +import { useNavigation } from '@react-navigation/native'; |
| 2 | +import { useMainNotificationToggle } from './MainNotificationToggle.hooks'; |
| 3 | +import Routes from '../../../../constants/navigation/Routes'; |
| 4 | +import { useNotificationsToggle } from '../../../../util/notifications/hooks/useSwitchNotifications'; |
| 5 | +import { useMetrics } from '../../../hooks/useMetrics'; |
| 6 | +import { MetricsEventBuilder } from '../../../../core/Analytics/MetricsEventBuilder'; |
6 | 7 | import { renderHookWithProvider } from '../../../../util/test/renderWithProvider'; |
7 | | -import { useAccounts } from '../../../hooks/useAccounts'; |
8 | | -import { |
9 | | - useAccountProps, |
10 | | - useNotificationAccountListProps, |
11 | | -} from './AccountsList.hooks'; |
| 8 | +import { EVENT_NAME } from '../../../../core/Analytics'; |
12 | 9 |
|
13 | | -// eslint-disable-next-line @typescript-eslint/no-explicit-any |
14 | | -type MockVar = any; |
| 10 | +// Mock dependencies |
| 11 | +jest.mock('@react-navigation/native'); |
| 12 | +jest.mock('../../../../util/notifications/hooks/useSwitchNotifications'); |
| 13 | +jest.mock('../../../hooks/useMetrics'); |
| 14 | + |
| 15 | +const mockUseNavigation = jest.mocked(useNavigation); |
| 16 | +const mockUseNotificationsToggle = jest.mocked(useNotificationsToggle); |
| 17 | +const mockUseMetrics = jest.mocked(useMetrics); |
| 18 | + |
| 19 | +describe('useMainNotificationToggle', () => { |
| 20 | + beforeEach(() => jest.clearAllMocks()); |
| 21 | + |
| 22 | + const arrangeState = () => { |
| 23 | + const mockState = { |
| 24 | + settings: { |
| 25 | + basicFunctionalityEnabled: true, |
| 26 | + }, |
| 27 | + engine: { |
| 28 | + backgroundState: { |
| 29 | + UserStorageController: { |
| 30 | + isBackupAndSyncEnabled: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }; |
| 35 | + return { mockState }; |
| 36 | + }; |
15 | 37 |
|
16 | | -describe('useNotificationAccountListProps', () => { |
17 | 38 | const arrangeMocks = () => { |
18 | | - const mockUpdate = jest.fn(); |
19 | | - const createMockUseFetchAccountNotificationsReturn = () => ({ |
20 | | - accountsBeingUpdated: [], |
21 | | - data: {}, |
| 39 | + const navigate = jest.fn(); |
| 40 | + const switchNotifications = jest.fn(); |
| 41 | + const trackEvent = jest.fn(); |
| 42 | + |
| 43 | + mockUseNavigation.mockReturnValue({ |
| 44 | + navigate, |
| 45 | + } as never); |
| 46 | + |
| 47 | + mockUseNotificationsToggle.mockReturnValue({ |
| 48 | + data: true, |
| 49 | + loading: false, |
22 | 50 | error: null, |
23 | | - initialLoading: false, |
24 | | - update: mockUpdate, |
| 51 | + switchNotifications, |
25 | 52 | }); |
26 | | - const mockUseFetchAccountNotifications = jest |
27 | | - .spyOn(UseSwitchNotificationsModule, 'useFetchAccountNotifications') |
28 | | - .mockReturnValue(createMockUseFetchAccountNotificationsReturn()); |
| 53 | + |
| 54 | + mockUseMetrics.mockReturnValue({ |
| 55 | + trackEvent, |
| 56 | + createEventBuilder: MetricsEventBuilder.createEventBuilder, |
| 57 | + } as never); |
29 | 58 |
|
30 | 59 | return { |
31 | | - mockUpdate, |
32 | | - mockUseFetchAccountNotifications, |
33 | | - createMockUseFetchAccountNotificationsReturn, |
| 60 | + navigate, |
| 61 | + switchNotifications, |
| 62 | + trackEvent, |
| 63 | + ...arrangeState(), |
34 | 64 | }; |
35 | 65 | }; |
36 | 66 |
|
37 | | - type Mocks = ReturnType<typeof arrangeMocks>; |
38 | | - const arrange = (addresses: string[], mutateMocks?: (m: Mocks) => void) => { |
39 | | - // Arrange |
| 67 | + const arrangeTest = ( |
| 68 | + overridesMocks?: (m: ReturnType<typeof arrangeMocks>) => void, |
| 69 | + ) => { |
40 | 70 | const mocks = arrangeMocks(); |
41 | | - mutateMocks?.(mocks); |
42 | | - const hook = renderHookWithProvider(() => |
43 | | - useNotificationAccountListProps(addresses), |
| 71 | + overridesMocks?.(mocks); |
| 72 | + |
| 73 | + const { result } = renderHookWithProvider( |
| 74 | + () => useMainNotificationToggle(), |
| 75 | + { state: mocks.mockState }, |
44 | 76 | ); |
45 | 77 |
|
46 | | - return { mocks, hook }; |
| 78 | + return { |
| 79 | + mocks, |
| 80 | + result, |
| 81 | + }; |
47 | 82 | }; |
48 | 83 |
|
49 | | - it('returns correct loading state', async () => { |
50 | | - const addresses = ['0x123', '0x456']; |
51 | | - const { hook } = arrange(addresses); |
52 | | - expect(hook.result.current.isAnyAccountLoading).toBe(false); |
| 84 | + beforeEach(() => { |
| 85 | + jest.clearAllMocks(); |
53 | 86 | }); |
54 | 87 |
|
55 | | - it('returns correct account loading state', async () => { |
56 | | - const addresses = ['0x123', '0x456']; |
57 | | - const { hook } = arrange(addresses, (m) => { |
58 | | - m.mockUseFetchAccountNotifications.mockReturnValue({ |
59 | | - ...m.createMockUseFetchAccountNotificationsReturn(), |
60 | | - accountsBeingUpdated: ['0x123'], |
61 | | - }); |
62 | | - }); |
63 | | - expect(hook.result.current.isAccountLoading('0x123')).toBe(true); |
64 | | - expect(hook.result.current.isAccountLoading('0x456')).toBe(false); |
| 88 | + it('toggles notifications from false to true', () => { |
| 89 | + const { mocks, result } = arrangeTest(); |
| 90 | + |
| 91 | + result.current.onToggle(); |
| 92 | + |
| 93 | + expect(mocks.switchNotifications).toHaveBeenCalledWith(false); |
| 94 | + expect(mocks.trackEvent).toHaveBeenCalledWith( |
| 95 | + expect.objectContaining({ |
| 96 | + name: EVENT_NAME.NOTIFICATIONS_SETTINGS_UPDATED, |
| 97 | + properties: expect.objectContaining({ |
| 98 | + settings_type: 'notifications', |
| 99 | + old_value: true, |
| 100 | + new_value: false, |
| 101 | + was_profile_syncing_on: true, |
| 102 | + }), |
| 103 | + }), |
| 104 | + ); |
65 | 105 | }); |
66 | 106 |
|
67 | | - it('returns correct account enabled state', async () => { |
68 | | - const addresses = ['0x123', '0x456']; |
69 | | - const { hook } = arrange(addresses, (m) => { |
70 | | - m.mockUseFetchAccountNotifications.mockReturnValue({ |
71 | | - ...m.createMockUseFetchAccountNotificationsReturn(), |
72 | | - data: { '0x123': true, '0x456': false }, |
| 107 | + it('toggles from false to true', () => { |
| 108 | + const { mocks, result } = arrangeTest((m) => { |
| 109 | + mockUseNotificationsToggle.mockReturnValue({ |
| 110 | + data: false, |
| 111 | + loading: false, |
| 112 | + error: null, |
| 113 | + switchNotifications: m.switchNotifications, |
73 | 114 | }); |
74 | 115 | }); |
75 | | - expect(hook.result.current.isAccountEnabled('0x123')).toBe(true); |
76 | | - expect(hook.result.current.isAccountEnabled('0x456')).toBe(false); |
77 | | - }); |
78 | | - |
79 | | - it('refetches account settings', async () => { |
80 | | - const addresses = ['0x123', '0x456']; |
81 | | - const { mocks, hook } = await arrange(addresses); |
82 | | - |
83 | | - // Act |
84 | | - await act(async () => { |
85 | | - await hook.result.current.refetchAccountSettings(); |
86 | | - }); |
87 | 116 |
|
88 | | - await waitFor(() => { |
89 | | - expect(mocks.mockUpdate).toHaveBeenCalledWith(addresses); |
90 | | - }); |
| 117 | + result.current.onToggle(); |
| 118 | + |
| 119 | + expect(mocks.switchNotifications).toHaveBeenCalledWith(true); |
| 120 | + expect(mocks.trackEvent).toHaveBeenCalledWith( |
| 121 | + expect.objectContaining({ |
| 122 | + name: EVENT_NAME.NOTIFICATIONS_SETTINGS_UPDATED, |
| 123 | + properties: expect.objectContaining({ |
| 124 | + settings_type: 'notifications', |
| 125 | + old_value: false, |
| 126 | + new_value: true, |
| 127 | + was_profile_syncing_on: true, |
| 128 | + }), |
| 129 | + }), |
| 130 | + ); |
91 | 131 | }); |
92 | | -}); |
93 | | - |
94 | | -jest.mock('../../../hooks/useAccounts', () => ({ |
95 | | - useAccounts: jest.fn(), |
96 | | -})); |
97 | 132 |
|
98 | | -const arrangeUseAccounts = () => { |
99 | | - const createMockAccounts = (addresses: string[]) => |
100 | | - addresses.map((address) => ({ address })); |
101 | | - |
102 | | - const mockUseAccounts = jest.mocked(useAccounts).mockReturnValue({ |
103 | | - accounts: createMockAccounts(['0x123', '0x456']), |
104 | | - } as MockVar); |
105 | | - |
106 | | - return { |
107 | | - createMockAccounts, |
108 | | - mockUseAccounts, |
109 | | - }; |
110 | | -}; |
111 | | - |
112 | | -describe('useAccountProps', () => { |
113 | | - const arrangeMocks = () => { |
114 | | - const mockStore = jest.fn().mockReturnValue({ |
115 | | - engine: { |
116 | | - backgroundState: { |
117 | | - NotificationServicesController: { |
118 | | - subscriptionAccountsSeen: ['0x123', '0x456'], |
119 | | - }, |
120 | | - }, |
121 | | - }, |
122 | | - settings: { avatarAccountType: AvatarAccountType.Maskicon }, |
| 133 | + it('navigate to basic functionality screen when basicFunctionalityEnabled is false', () => { |
| 134 | + const { mocks, result } = arrangeTest((m) => { |
| 135 | + m.mockState.settings.basicFunctionalityEnabled = false; |
123 | 136 | }); |
124 | 137 |
|
125 | | - return { |
126 | | - ...arrangeUseAccounts(), |
127 | | - mockStore, |
128 | | - }; |
129 | | - }; |
| 138 | + result.current.onToggle(); |
130 | 139 |
|
131 | | - it('returns correct account props', () => { |
132 | | - const mocks = arrangeMocks(); |
133 | | - const { result } = renderHookWithProvider(() => useAccountProps(), { |
134 | | - state: mocks.mockStore(), |
| 140 | + expect(mocks.navigate).toHaveBeenCalledWith(Routes.MODAL.ROOT_MODAL_FLOW, { |
| 141 | + screen: Routes.SHEET.BASIC_FUNCTIONALITY, |
| 142 | + params: { |
| 143 | + caller: Routes.SETTINGS.NOTIFICATIONS, |
| 144 | + }, |
135 | 145 | }); |
136 | | - |
137 | | - expect(result.current.accounts).toHaveLength(2); |
138 | | - expect(result.current.accountAvatarType).toBe(AvatarAccountType.Maskicon); |
139 | | - expect(result.current.accountAddresses).toEqual(['0x123', '0x456']); |
| 146 | + expect(mocks.switchNotifications).not.toHaveBeenCalled(); |
| 147 | + expect(mocks.trackEvent).not.toHaveBeenCalled(); |
140 | 148 | }); |
141 | 149 | }); |
0 commit comments