1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 15:50:28 +01:00
metamask-extension/ui/components/multichain/import-account/json.test.tsx
David Walsh 28137798b6
UX: Multichain: Move Add Account and Import Account into Account Menu Popover (#19346)
* UX: Multichain: Move Add Account and Import Account into Account Menu Popover

* Create a new CreateAccount component for the Account Menu

* Add actions for import form

* Use separate actions for cancel vs. submit

* Fix jest tests

* Remove commented route navigation

* Accommodate for failing import

* Fix tests

* Remove routes for new account and import

* Remove old create account page

* Move import-account files to multichain directory

* Fix paths on the import files

* Remove deprecated component library variables

* Fix error property of add form

* Fix user-actions-benchmark
2023-06-13 10:07:01 -05:00

112 lines
2.9 KiB
TypeScript

import React from 'react';
import configureMockStore from 'redux-mock-store';
import { fireEvent, waitFor } from '@testing-library/react';
import { renderWithProvider } from '../../../../test/lib/render-helpers';
import mockState from '../../../../test/data/mock-state.json';
import messages from '../../../../app/_locales/en/messages.json';
import Json from './json';
const mockImportFunc = jest.fn();
const mockOnActionComplete = jest.fn();
describe('Json', () => {
const mockStore = configureMockStore()(mockState);
it('should match snapshot', () => {
const { asFragment } = renderWithProvider(
<Json
importAccountFunc={mockImportFunc}
onActionComplete={mockOnActionComplete}
/>,
mockStore,
);
expect(asFragment()).toMatchSnapshot();
});
it('should render', () => {
const { getByText } = renderWithProvider(
<Json
importAccountFunc={mockImportFunc}
onActionComplete={mockOnActionComplete}
/>,
mockStore,
);
const fileImportLink = getByText('File import not working? Click here!');
expect(fileImportLink).toBeInTheDocument();
});
it('should import file without password', async () => {
const { getByText, getByTestId } = renderWithProvider(
<Json
importAccountFunc={mockImportFunc}
onActionComplete={mockOnActionComplete}
/>,
mockStore,
);
const importButton = getByText('Import');
const fileInput = getByTestId('file-input');
const mockFile = new File(['0'], 'test.json');
expect(importButton).toBeInTheDocument();
expect(importButton).toBeDisabled();
fireEvent.change(fileInput, {
target: { files: [mockFile] },
});
await waitFor(() => {
expect(importButton).not.toBeDisabled();
});
fireEvent.click(importButton);
await waitFor(() => {
expect(mockImportFunc).toHaveBeenCalledWith('JSON File', ['0', '']);
});
});
it('should import file with password', async () => {
const { getByText, getByTestId, getByPlaceholderText } = renderWithProvider(
<Json
importAccountFunc={mockImportFunc}
onActionComplete={mockOnActionComplete}
/>,
mockStore,
);
const importButton = getByText('Import');
const fileInput = getByTestId('file-input');
const mockFile = new File(['0'], 'test.json');
expect(importButton).toBeInTheDocument();
expect(importButton).toBeDisabled();
const passwordInput = getByPlaceholderText(
messages.enterOptionalPassword.message,
);
fireEvent.change(passwordInput, {
target: { value: 'password' },
});
fireEvent.change(fileInput, {
target: { files: [mockFile] },
});
await waitFor(() => {
expect(importButton).not.toBeDisabled();
});
fireEvent.click(importButton);
await waitFor(() => {
expect(mockImportFunc).toHaveBeenCalledWith('JSON File', [
'0',
'password',
]);
});
});
});