Skip to content

Commit 557c96f

Browse files
committed
feat: add UTs and more robust logic handling
1 parent 583af04 commit 557c96f

File tree

2 files changed

+227
-3
lines changed

2 files changed

+227
-3
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
import * as React from 'react';
2+
import { fireEvent, waitFor } from '@testing-library/react';
3+
import { useLocation } from 'react-router-dom';
4+
import configureStore from '../../../store/store';
5+
import { renderWithProvider } from '../../../../test/lib/render-helpers';
6+
import * as Actions from '../../../store/actions';
7+
import {
8+
hideBasicFunctionalityModal,
9+
onboardingToggleBasicFunctionalityOff,
10+
} from '../../../ducks/app/app';
11+
import { ONBOARDING_PRIVACY_SETTINGS_ROUTE } from '../../../helpers/constants/routes';
12+
import { BasicConfigurationModal } from './basic-configuration-modal';
13+
14+
jest.mock('../../../store/actions', () => ({
15+
setDataCollectionForMarketing: jest.fn(),
16+
setParticipateInMetaMetrics: jest.fn(),
17+
toggleExternalServices: jest.fn(),
18+
}));
19+
20+
jest.mock('../../../ducks/app/app', () => ({
21+
hideBasicFunctionalityModal: jest.fn(),
22+
onboardingToggleBasicFunctionalityOff: jest.fn(),
23+
}));
24+
25+
const mockDispatch = jest.fn();
26+
27+
jest.mock('react-redux', () => {
28+
const actual = jest.requireActual('react-redux');
29+
return {
30+
...actual,
31+
useDispatch: () => mockDispatch,
32+
};
33+
});
34+
35+
jest.mock('react-router-dom', () => ({
36+
...jest.requireActual('react-router-dom'),
37+
useLocation: jest.fn(),
38+
}));
39+
40+
type StateOverrides<T extends boolean> = {
41+
metamask: {
42+
useExternalServices: T;
43+
};
44+
};
45+
46+
type ArrangeMocksParams<T extends boolean> = {
47+
isOnboarding?: boolean;
48+
stateOverrides?: StateOverrides<T>;
49+
};
50+
51+
type ArrangeMocksReturn<T extends boolean> = {
52+
toggleBasicFunctionalityButton: HTMLElement;
53+
cancelButton: HTMLElement;
54+
agreementCheckbox: T extends true ? HTMLElement : null;
55+
};
56+
57+
const arrangeMocks = <T extends boolean>({
58+
isOnboarding = false,
59+
stateOverrides,
60+
}: ArrangeMocksParams<T> = {}): ArrangeMocksReturn<T> => {
61+
jest.clearAllMocks();
62+
63+
(useLocation as jest.Mock).mockReturnValue({
64+
pathname: isOnboarding
65+
? ONBOARDING_PRIVACY_SETTINGS_ROUTE
66+
: '/any-other-path',
67+
});
68+
69+
const store = configureStore({
70+
...stateOverrides,
71+
});
72+
const { getByTestId, getByTitle } = renderWithProvider(
73+
<BasicConfigurationModal />,
74+
store,
75+
);
76+
77+
const agreementCheckbox = stateOverrides?.metamask.useExternalServices
78+
? getByTitle('basic-configuration-checkbox')
79+
: null;
80+
const toggleBasicFunctionalityButton = getByTestId(
81+
'basic-configuration-modal-toggle-button',
82+
);
83+
const cancelButton = getByTestId('basic-configuration-modal-cancel-button');
84+
85+
return {
86+
toggleBasicFunctionalityButton,
87+
cancelButton,
88+
agreementCheckbox,
89+
} as ArrangeMocksReturn<T>;
90+
};
91+
92+
describe('BasicConfigurationModal', () => {
93+
it('should call hideBasicFunctionalityModal when the cancel button is clicked', () => {
94+
const { cancelButton } = arrangeMocks();
95+
96+
expect(cancelButton).toBeEnabled();
97+
98+
fireEvent.click(cancelButton);
99+
100+
expect(hideBasicFunctionalityModal).toHaveBeenCalledTimes(1);
101+
});
102+
103+
describe('during onboarding', () => {
104+
it('should render the basic configuration modal', async () => {
105+
const {
106+
agreementCheckbox,
107+
cancelButton,
108+
toggleBasicFunctionalityButton,
109+
} = arrangeMocks({
110+
isOnboarding: true,
111+
stateOverrides: {
112+
metamask: {
113+
useExternalServices: true,
114+
},
115+
},
116+
});
117+
118+
expect(agreementCheckbox).toBeInTheDocument();
119+
expect(cancelButton).toBeInTheDocument();
120+
expect(toggleBasicFunctionalityButton).toBeInTheDocument();
121+
});
122+
123+
it('should call appropriate actions when the turn off button is clicked', () => {
124+
const { agreementCheckbox, toggleBasicFunctionalityButton } =
125+
arrangeMocks({
126+
isOnboarding: true,
127+
stateOverrides: {
128+
metamask: {
129+
useExternalServices: true,
130+
},
131+
},
132+
});
133+
134+
fireEvent.click(agreementCheckbox, {
135+
target: { checked: true },
136+
});
137+
138+
expect(toggleBasicFunctionalityButton).toBeEnabled();
139+
140+
fireEvent.click(toggleBasicFunctionalityButton);
141+
142+
expect(hideBasicFunctionalityModal).toHaveBeenCalledTimes(1);
143+
expect(onboardingToggleBasicFunctionalityOff).toHaveBeenCalledTimes(1);
144+
expect(Actions.setParticipateInMetaMetrics).toHaveBeenCalledTimes(1);
145+
expect(Actions.setParticipateInMetaMetrics).toHaveBeenCalledWith(false);
146+
expect(Actions.setDataCollectionForMarketing).toHaveBeenCalledTimes(1);
147+
expect(Actions.setDataCollectionForMarketing).toHaveBeenCalledWith(false);
148+
});
149+
});
150+
151+
describe('outside onboarding', () => {
152+
it('should render the basic configuration modal', async () => {
153+
const {
154+
agreementCheckbox,
155+
cancelButton,
156+
toggleBasicFunctionalityButton,
157+
} = arrangeMocks({
158+
isOnboarding: false,
159+
stateOverrides: {
160+
metamask: {
161+
useExternalServices: true,
162+
},
163+
},
164+
});
165+
166+
expect(agreementCheckbox).toBeInTheDocument();
167+
expect(cancelButton).toBeInTheDocument();
168+
expect(toggleBasicFunctionalityButton).toBeInTheDocument();
169+
});
170+
171+
it('should call appropriate actions when the turn off button is clicked', () => {
172+
const { agreementCheckbox, toggleBasicFunctionalityButton } =
173+
arrangeMocks({
174+
stateOverrides: {
175+
metamask: {
176+
useExternalServices: true,
177+
},
178+
},
179+
});
180+
181+
fireEvent.click(agreementCheckbox, {
182+
target: { checked: true },
183+
});
184+
185+
waitFor(() => {
186+
expect(toggleBasicFunctionalityButton).toBeEnabled();
187+
});
188+
189+
fireEvent.click(toggleBasicFunctionalityButton);
190+
191+
expect(hideBasicFunctionalityModal).toHaveBeenCalledTimes(1);
192+
expect(Actions.setParticipateInMetaMetrics).toHaveBeenCalledTimes(1);
193+
expect(Actions.setParticipateInMetaMetrics).toHaveBeenCalledWith(false);
194+
expect(Actions.setDataCollectionForMarketing).toHaveBeenCalledTimes(1);
195+
expect(Actions.setDataCollectionForMarketing).toHaveBeenCalledWith(false);
196+
expect(Actions.toggleExternalServices).toHaveBeenCalledTimes(1);
197+
expect(Actions.toggleExternalServices).toHaveBeenCalledWith(false);
198+
});
199+
200+
it('should call the appropriate actions when the turn on button is clicked', () => {
201+
const { toggleBasicFunctionalityButton } = arrangeMocks({
202+
stateOverrides: {
203+
metamask: {
204+
useExternalServices: false,
205+
},
206+
},
207+
});
208+
209+
expect(toggleBasicFunctionalityButton).toBeEnabled();
210+
211+
fireEvent.click(toggleBasicFunctionalityButton);
212+
213+
expect(hideBasicFunctionalityModal).toHaveBeenCalledTimes(1);
214+
expect(Actions.setParticipateInMetaMetrics).toHaveBeenCalledTimes(0);
215+
expect(Actions.setDataCollectionForMarketing).toHaveBeenCalledTimes(0);
216+
expect(Actions.toggleExternalServices).toHaveBeenCalledTimes(1);
217+
expect(Actions.toggleExternalServices).toHaveBeenCalledWith(true);
218+
});
219+
});
220+
});

ui/components/app/basic-configuration-modal/basic-configuration-modal.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export function BasicConfigurationModal() {
139139
size={ButtonSize.Lg}
140140
width={BlockSize.Half}
141141
variant={ButtonVariant.Secondary}
142+
data-testid="basic-configuration-modal-cancel-button"
142143
onClick={closeModal}
143144
>
144145
{t('cancel')}
@@ -148,6 +149,7 @@ export function BasicConfigurationModal() {
148149
disabled={!hasAgreed && isExternalServicesEnabled}
149150
width={BlockSize.Half}
150151
variant={ButtonVariant.Primary}
152+
data-testid="basic-configuration-modal-toggle-button"
151153
onClick={() => {
152154
const event = onboardingFlow
153155
? {
@@ -176,16 +178,18 @@ export function BasicConfigurationModal() {
176178

177179
trackEvent(event);
178180

181+
if (isExternalServicesEnabled || onboardingFlow) {
182+
dispatch(setParticipateInMetaMetrics(false));
183+
dispatch(setDataCollectionForMarketing(false));
184+
}
185+
179186
if (onboardingFlow) {
180187
dispatch(hideBasicFunctionalityModal());
181188
dispatch(onboardingToggleBasicFunctionalityOff());
182189
} else {
183190
closeModal();
184191
dispatch(toggleExternalServices(!isExternalServicesEnabled));
185192
}
186-
187-
dispatch(setParticipateInMetaMetrics(false));
188-
dispatch(setDataCollectionForMarketing(false));
189193
}}
190194
danger={isExternalServicesEnabled}
191195
>

0 commit comments

Comments
 (0)