2023-05-24 00:54:30 +02:00
|
|
|
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();
|
2023-06-13 17:07:01 +02:00
|
|
|
const mockOnActionComplete = jest.fn();
|
|
|
|
|
2023-05-24 00:54:30 +02:00
|
|
|
describe('Json', () => {
|
|
|
|
const mockStore = configureMockStore()(mockState);
|
|
|
|
it('should match snapshot', () => {
|
|
|
|
const { asFragment } = renderWithProvider(
|
2023-06-13 17:07:01 +02:00
|
|
|
<Json
|
|
|
|
importAccountFunc={mockImportFunc}
|
|
|
|
onActionComplete={mockOnActionComplete}
|
|
|
|
/>,
|
2023-05-24 00:54:30 +02:00
|
|
|
mockStore,
|
|
|
|
);
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render', () => {
|
|
|
|
const { getByText } = renderWithProvider(
|
2023-06-13 17:07:01 +02:00
|
|
|
<Json
|
|
|
|
importAccountFunc={mockImportFunc}
|
|
|
|
onActionComplete={mockOnActionComplete}
|
|
|
|
/>,
|
2023-05-24 00:54:30 +02:00
|
|
|
mockStore,
|
|
|
|
);
|
|
|
|
|
|
|
|
const fileImportLink = getByText('File import not working? Click here!');
|
|
|
|
expect(fileImportLink).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should import file without password', async () => {
|
|
|
|
const { getByText, getByTestId } = renderWithProvider(
|
2023-06-13 17:07:01 +02:00
|
|
|
<Json
|
|
|
|
importAccountFunc={mockImportFunc}
|
|
|
|
onActionComplete={mockOnActionComplete}
|
|
|
|
/>,
|
2023-05-24 00:54:30 +02:00
|
|
|
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(
|
2023-06-13 17:07:01 +02:00
|
|
|
<Json
|
|
|
|
importAccountFunc={mockImportFunc}
|
|
|
|
onActionComplete={mockOnActionComplete}
|
|
|
|
/>,
|
2023-05-24 00:54:30 +02:00
|
|
|
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',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|