1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 21:57:06 +01:00
metamask-extension/ui/components/app/modals/export-private-key-modal/export-private-key-modal.component.test.js
Monte Lai 0306422bbf
Add reveal to export private key (#18170)
Co-authored-by: George Marshall <george.marshall@consensys.net>
Co-authored-by: Brad Decker <bhdecker84@gmail.com>
Co-authored-by: David Walsh <davidwalsh83@gmail.com>
Co-authored-by: Howard Braham <howrad@gmail.com>

* change js to tsx

* update to typescript

* add labels to circle animation

* add willHide prop to hold to reveal modal

* add test

* convert to design system

* fix lint

* fix type

* bump coverage

* rename

* remove comments

* remove ts comment and add fix exhuastive dep check

* update coverage

* add hide modal test

* use banneralert

* update label

* remove unused

* fix text

* update aria label messages

* change exportAccountAndGetPrivateKey to be async

* fix lint

* update coverage target

* update coverage

* update input component

* update coverage

* update coverage

* fix blank line

* use &&

* move plainKey to under !privateKeyInput

* update hold modal to display srp and private key message

* fix styling

* fix lint and test

* fix unused locales

* remove redundent check

* update storybook

* fix text alignment

* fix lint

* update snapshot

* fix test

* update coverage

* fix merge conflict

* refactor

* fix variant

* update snapshot

* fix test after merge

* fix test after merge conflict

* fix label text

* update to use label component
2023-05-06 17:04:20 -04:00

131 lines
3.8 KiB
JavaScript

import { fireEvent, waitFor } from '@testing-library/react';
import configureMockStore from 'redux-mock-store';
import React from 'react';
import thunk from 'redux-thunk';
import { renderWithProvider } from '../../../../../test/lib/render-helpers';
import ExportPrivateKeyModal from '.';
const mockAddress = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc';
const mockPrivateKey = 'mock private key';
const mockExportAccount = jest.fn().mockResolvedValue(mockPrivateKey);
const mockClearAccountDetail = jest.fn();
const mockHideWarning = jest.fn();
jest.mock('../../../../store/actions', () => ({
exportAccount: () => mockExportAccount,
clearAccountDetails: () => mockClearAccountDetail,
hideWarning: () => mockHideWarning,
}));
describe('Export Private Key Modal', () => {
const state = {
metamask: {
selectedAddress: mockAddress,
identities: {
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': {
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
name: 'Test Account',
},
},
providerConfig: {
type: 'rpc',
chainId: '0x5',
ticker: 'ETH',
id: 'testNetworkConfigurationId',
},
},
appState: {
warning: null,
previousModalState: {
name: null,
},
isLoading: false,
accountDetail: {
privateKey: null,
},
modal: {
modalState: {},
previousModalState: {
name: null,
},
},
},
};
const mockStore = configureMockStore([thunk])(state);
it('renders export private key modal', () => {
const { queryByText } = renderWithProvider(
<ExportPrivateKeyModal />,
mockStore,
);
const title = queryByText('Show Private Keys');
expect(title).toBeInTheDocument();
const warning = queryByText(
'Warning: Never disclose this key. Anyone with your private keys can steal any assets held in your account.',
);
expect(warning).toBeInTheDocument();
expect(queryByText(mockPrivateKey)).not.toBeInTheDocument();
});
it('renders hold to reveal after entering password', async () => {
const { queryByText, getByPlaceholderText } = renderWithProvider(
<ExportPrivateKeyModal />,
mockStore,
);
const nextButton = queryByText('Confirm');
expect(nextButton).toBeInTheDocument();
const input = getByPlaceholderText('Enter password');
fireEvent.change(input, {
target: { value: 'password' },
});
fireEvent.click(nextButton);
await waitFor(() => {
expect(mockExportAccount).toHaveBeenCalled();
expect(queryByText('Keep your private key safe')).toBeInTheDocument();
});
});
it('provides password after passing hold to reveal', async () => {
const { queryByText, getByLabelText, getByText, getByPlaceholderText } =
renderWithProvider(<ExportPrivateKeyModal />, mockStore);
const nextButton = queryByText('Confirm');
expect(nextButton).toBeInTheDocument();
const input = getByPlaceholderText('Enter password');
fireEvent.change(input, {
target: { value: 'password' },
});
fireEvent.click(nextButton);
await waitFor(() => {
expect(mockExportAccount).toHaveBeenCalled();
expect(queryByText('Keep your private key safe')).toBeInTheDocument();
});
const holdButton = getByText('Hold to reveal Private Key');
expect(holdButton).toBeInTheDocument();
fireEvent.mouseDown(holdButton);
const circle = getByLabelText('hold to reveal circle locked');
fireEvent.transitionEnd(circle);
const circleUnlocked = getByLabelText('hold to reveal circle unlocked');
fireEvent.animationEnd(circleUnlocked);
await waitFor(() => {
expect(queryByText('Show Private Keys')).toBeInTheDocument();
expect(queryByText('Done')).toBeInTheDocument();
expect(queryByText(mockPrivateKey)).toBeInTheDocument();
});
});
});