2023-06-20 20:27:10 +02:00
|
|
|
import { fireEvent, queryByRole, screen } from '@testing-library/react';
|
2022-11-04 21:56:24 +01:00
|
|
|
import React from 'react';
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
2023-06-20 20:27:10 +02:00
|
|
|
import thunk from 'redux-thunk';
|
2022-11-04 21:56:24 +01:00
|
|
|
import mockState from '../../../../test/data/mock-state.json';
|
|
|
|
import { renderWithProvider } from '../../../../test/lib/render-helpers';
|
|
|
|
import SecurityTab from './security-tab.container';
|
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
jest.mock('../../../../app/scripts/lib/util', () => {
|
|
|
|
const originalModule = jest.requireActual('../../../../app/scripts/lib/util');
|
2022-11-04 21:56:24 +01:00
|
|
|
|
|
|
|
return {
|
2023-06-20 20:27:10 +02:00
|
|
|
...originalModule,
|
|
|
|
getEnvironmentType: jest.fn(),
|
2022-11-04 21:56:24 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Security Tab', () => {
|
2023-06-20 20:27:10 +02:00
|
|
|
delete mockState.metamask.featureFlags; // Unset featureFlags in order to test the default value
|
|
|
|
mockState.appState.warning = 'warning'; // This tests an otherwise untested render branch
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
const mockStore = configureMockStore([thunk])(mockState);
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
function toggleCheckbox(testId, initialState) {
|
|
|
|
renderWithProvider(<SecurityTab />, mockStore);
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
const container = screen.getByTestId(testId);
|
|
|
|
const checkbox = queryByRole(container, 'checkbox');
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
expect(checkbox).toHaveAttribute('value', initialState ? 'true' : 'false');
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
fireEvent.click(checkbox); // This fires the onToggle method of the ToggleButton, but it doesn't change the value of the checkbox
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
fireEvent.change(checkbox, {
|
|
|
|
target: { value: !initialState }, // This changes the value of the checkbox
|
2022-11-04 21:56:24 +01:00
|
|
|
});
|
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
expect(checkbox).toHaveAttribute('value', initialState ? 'false' : 'true');
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('should match snapshot', () => {
|
|
|
|
const { container } = renderWithProvider(<SecurityTab />, mockStore);
|
2022-11-04 21:56:24 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
expect(container).toMatchSnapshot();
|
2022-11-04 21:56:24 +01:00
|
|
|
});
|
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('toggles phishing detection', async () => {
|
|
|
|
expect(await toggleCheckbox('usePhishingDetection', true)).toBe(true);
|
2022-11-04 21:56:24 +01:00
|
|
|
});
|
2022-12-01 22:16:04 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('toggles balance and token price checker', async () => {
|
|
|
|
expect(await toggleCheckbox('currencyRateCheckToggle', true)).toBe(true);
|
|
|
|
});
|
2022-12-01 22:16:04 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('toggles incoming txs', async () => {
|
|
|
|
expect(await toggleCheckbox('showIncomingTransactions', false)).toBe(true);
|
|
|
|
});
|
2022-12-01 22:16:04 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('should toggle token detection', async () => {
|
|
|
|
expect(await toggleCheckbox('autoDetectTokens', true)).toBe(true);
|
|
|
|
});
|
2022-12-01 22:16:04 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('toggles batch balance checks', async () => {
|
|
|
|
expect(await toggleCheckbox('useMultiAccountBalanceChecker', false)).toBe(
|
|
|
|
true,
|
|
|
|
);
|
2022-12-01 22:16:04 +01:00
|
|
|
});
|
2022-12-19 18:46:36 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('toggles metaMetrics', async () => {
|
|
|
|
expect(await toggleCheckbox('participateInMetaMetrics', false)).toBe(true);
|
|
|
|
});
|
2022-12-19 18:46:36 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
it('toggles SRP Quiz', async () => {
|
|
|
|
renderWithProvider(<SecurityTab />, mockStore);
|
2022-12-19 18:46:36 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
expect(
|
|
|
|
screen.queryByTestId(`srp_stage_introduction`),
|
|
|
|
).not.toBeInTheDocument();
|
2022-12-19 18:46:36 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
fireEvent.click(screen.getByTestId('reveal-seed-words'));
|
2022-12-19 18:46:36 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
expect(screen.getByTestId(`srp_stage_introduction`)).toBeInTheDocument();
|
2022-12-19 18:46:36 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
const container = screen.getByTestId('srp-quiz-header');
|
|
|
|
const checkbox = queryByRole(container, 'button');
|
|
|
|
fireEvent.click(checkbox);
|
2022-12-19 18:46:36 +01:00
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
expect(
|
|
|
|
screen.queryByTestId(`srp_stage_introduction`),
|
|
|
|
).not.toBeInTheDocument();
|
2022-12-19 18:46:36 +01:00
|
|
|
});
|
2022-11-04 21:56:24 +01:00
|
|
|
});
|