Skip to content

Commit b12a6d3

Browse files
test: update UTs
1 parent 9269693 commit b12a6d3

File tree

3 files changed

+119
-114
lines changed

3 files changed

+119
-114
lines changed

app/components/Views/Settings/NotificationsSettings/CustomNotificationsRow/__snapshots__/index.test.tsx.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ exports[`CustomNotificationsRow should render correctly 1`] = `
66
{
77
"alignItems": "center",
88
"flexDirection": "row",
9-
"marginBottom": 24,
109
}
1110
}
1211
testID="notifications-switch--container"
Lines changed: 118 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,149 @@
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';
67
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';
129

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+
};
1537

16-
describe('useNotificationAccountListProps', () => {
1738
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,
2250
error: null,
23-
initialLoading: false,
24-
update: mockUpdate,
51+
switchNotifications,
2552
});
26-
const mockUseFetchAccountNotifications = jest
27-
.spyOn(UseSwitchNotificationsModule, 'useFetchAccountNotifications')
28-
.mockReturnValue(createMockUseFetchAccountNotificationsReturn());
53+
54+
mockUseMetrics.mockReturnValue({
55+
trackEvent,
56+
createEventBuilder: MetricsEventBuilder.createEventBuilder,
57+
} as never);
2958

3059
return {
31-
mockUpdate,
32-
mockUseFetchAccountNotifications,
33-
createMockUseFetchAccountNotificationsReturn,
60+
navigate,
61+
switchNotifications,
62+
trackEvent,
63+
...arrangeState(),
3464
};
3565
};
3666

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+
) => {
4070
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 },
4476
);
4577

46-
return { mocks, hook };
78+
return {
79+
mocks,
80+
result,
81+
};
4782
};
4883

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();
5386
});
5487

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+
);
65105
});
66106

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,
73114
});
74115
});
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-
});
87116

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+
);
91131
});
92-
});
93-
94-
jest.mock('../../../hooks/useAccounts', () => ({
95-
useAccounts: jest.fn(),
96-
}));
97132

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;
123136
});
124137

125-
return {
126-
...arrangeUseAccounts(),
127-
mockStore,
128-
};
129-
};
138+
result.current.onToggle();
130139

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+
},
135145
});
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();
140148
});
141149
});

app/components/Views/Settings/NotificationsSettings/__snapshots__/index.test.tsx.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ exports[`NotificationsSettings render matches snapshot 1`] = `
44
<RCTScrollView
55
style={
66
{
7-
"backgroundColor": "#ffffff",
8-
"flex": 1,
97
"padding": 24,
108
"paddingBottom": 48,
119
}
@@ -70,7 +68,7 @@ exports[`NotificationsSettings render matches snapshot 1`] = `
7068
<View
7169
style={
7270
{
73-
"marginVertical": 16,
71+
"marginTop": 16,
7472
}
7573
}
7674
>

0 commit comments

Comments
 (0)