2023-07-26 21:37:33 +02:00
|
|
|
import { toChecksumHexAddress } from '@metamask/controller-utils';
|
2023-08-24 23:35:26 +02:00
|
|
|
import { fireEvent, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import React from 'react';
|
2023-05-03 19:09:13 +02:00
|
|
|
import { showPrivateKey } from '../../../../app/_locales/en/messages.json';
|
2023-08-24 23:35:26 +02:00
|
|
|
import mockState from '../../../../test/data/mock-state.json';
|
|
|
|
import { renderWithProvider } from '../../../../test/jest';
|
|
|
|
import { shortenAddress } from '../../../helpers/utils/util';
|
2023-05-03 19:09:13 +02:00
|
|
|
import {
|
2023-08-24 23:35:26 +02:00
|
|
|
clearAccountDetails,
|
2023-05-03 19:09:13 +02:00
|
|
|
exportAccount,
|
|
|
|
hideWarning,
|
2023-08-24 23:35:26 +02:00
|
|
|
setAccountDetailsAddress,
|
2023-05-03 19:09:13 +02:00
|
|
|
} from '../../../store/actions';
|
2023-08-24 23:35:26 +02:00
|
|
|
import configureStore from '../../../store/store';
|
|
|
|
import { AccountDetailsKey } from './account-details-key';
|
2023-05-03 19:09:13 +02:00
|
|
|
import { AccountDetails } from '.';
|
|
|
|
|
|
|
|
jest.mock('../../../store/actions.ts');
|
|
|
|
|
|
|
|
describe('AccountDetails', () => {
|
|
|
|
const address = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc';
|
2023-08-24 23:35:26 +02:00
|
|
|
const mockClearAccountDetails = jest.fn();
|
2023-05-03 19:09:13 +02:00
|
|
|
const mockExportAccount = jest.fn().mockResolvedValue(true);
|
|
|
|
const mockHideWarning = jest.fn();
|
2023-08-24 23:35:26 +02:00
|
|
|
const mockSetAccountDetailsAddress = jest.fn();
|
2023-05-03 19:09:13 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-08-24 23:35:26 +02:00
|
|
|
clearAccountDetails.mockReturnValue(mockClearAccountDetails);
|
2023-05-03 19:09:13 +02:00
|
|
|
exportAccount.mockReturnValue(mockExportAccount);
|
|
|
|
hideWarning.mockReturnValue(mockHideWarning);
|
2023-08-24 23:35:26 +02:00
|
|
|
setAccountDetailsAddress.mockReturnValue(mockSetAccountDetailsAddress);
|
2023-05-03 19:09:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => jest.clearAllMocks());
|
|
|
|
|
|
|
|
function render(props = {}, storeModifications = {}) {
|
|
|
|
const store = configureStore({
|
|
|
|
metamask: {
|
|
|
|
...mockState.metamask,
|
|
|
|
},
|
|
|
|
...storeModifications,
|
|
|
|
});
|
|
|
|
const allProps = { address, ...props };
|
|
|
|
return renderWithProvider(<AccountDetails {...allProps} />, store);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should set account label when changing default account label', () => {
|
|
|
|
render();
|
|
|
|
|
|
|
|
const editButton = screen.getByTestId('editable-label-button');
|
|
|
|
fireEvent.click(editButton);
|
|
|
|
|
2023-05-08 16:03:34 +02:00
|
|
|
const editableInput = screen.getByPlaceholderText('Account name');
|
2023-05-03 19:09:13 +02:00
|
|
|
const newAccountLabel = 'New Label';
|
|
|
|
|
|
|
|
fireEvent.change(editableInput, { target: { value: newAccountLabel } });
|
|
|
|
|
|
|
|
expect(editableInput).toHaveAttribute('value', newAccountLabel);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows export private key contents and password field when clicked', () => {
|
|
|
|
const { queryByText, queryByPlaceholderText } = render();
|
|
|
|
const exportPrivateKeyButton = queryByText(showPrivateKey.message);
|
|
|
|
fireEvent.click(exportPrivateKeyButton);
|
|
|
|
|
2023-07-26 21:37:33 +02:00
|
|
|
expect(
|
|
|
|
queryByText(shortenAddress(toChecksumHexAddress(address))),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
2023-05-03 19:09:13 +02:00
|
|
|
expect(queryByText('Show private key')).toBeInTheDocument();
|
|
|
|
expect(queryByPlaceholderText('Password')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('attempts to validate password when submitted', async () => {
|
|
|
|
const password = 'password';
|
|
|
|
|
|
|
|
const { queryByPlaceholderText, queryByText } = render();
|
|
|
|
const exportPrivateKeyButton = queryByText(showPrivateKey.message);
|
|
|
|
fireEvent.click(exportPrivateKeyButton);
|
|
|
|
|
|
|
|
queryByPlaceholderText('Password').focus();
|
|
|
|
await userEvent.keyboard(password);
|
|
|
|
fireEvent.click(queryByText('Confirm'));
|
|
|
|
|
2023-08-24 23:35:26 +02:00
|
|
|
expect(exportAccount).toHaveBeenCalledWith(
|
|
|
|
password,
|
|
|
|
address,
|
|
|
|
expect.any(Function),
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2023-05-03 19:09:13 +02:00
|
|
|
});
|
|
|
|
|
2023-08-24 23:35:26 +02:00
|
|
|
it('displays the private key when sent in props', () => {
|
2023-05-03 19:09:13 +02:00
|
|
|
const samplePrivateKey = '8675309';
|
2023-08-24 23:35:26 +02:00
|
|
|
|
|
|
|
const { queryByText } = renderWithProvider(
|
|
|
|
<AccountDetailsKey
|
|
|
|
accountName="Account 1"
|
|
|
|
onClose={jest.fn()}
|
|
|
|
privateKey={samplePrivateKey}
|
|
|
|
/>,
|
2023-05-03 19:09:13 +02:00
|
|
|
);
|
|
|
|
|
2023-08-24 23:35:26 +02:00
|
|
|
expect(queryByText(samplePrivateKey)).toBeInTheDocument();
|
|
|
|
});
|
2023-05-03 19:09:13 +02:00
|
|
|
|
2023-08-24 23:35:26 +02:00
|
|
|
it('should call AccountDetails.onClose()', () => {
|
|
|
|
render();
|
2023-07-26 21:37:33 +02:00
|
|
|
|
2023-08-24 23:35:26 +02:00
|
|
|
fireEvent.click(screen.getByLabelText('Close'));
|
|
|
|
|
|
|
|
expect(screen.queryByText('Account 1')).toBeNull();
|
2023-05-03 19:09:13 +02:00
|
|
|
});
|
|
|
|
});
|