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/customize-nonce/customize-nonce.test.js
Thomas Huang e65b955b19
Unit tests for various modals (#18115)
* Unit tests for Hide Token Confirmation modal

* Unit test for Export Private Key modal

* Unit test for Customize Nonce modal

* Bump coverage targets

---------

Co-authored-by: Brad Decker <bhdecker84@gmail.com>
2023-04-24 09:55:32 -07:00

133 lines
3.1 KiB
JavaScript

import React from 'react';
import { fireEvent } from '@testing-library/react';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { renderWithProvider } from '../../../../../test/lib/render-helpers';
import CustomizeNonce from '.';
const mockHideModal = jest.fn();
jest.mock('../../../../store/actions.ts', () => ({
...jest.requireActual('../../../../store/actions.ts'),
hideModal: () => mockHideModal,
}));
describe('Customize Nonce', () => {
const mockState = {
appState: {
modal: {
modalState: {
props: {},
},
},
},
};
const mockStore = configureMockStore([thunk])(mockState);
const props = {
nextNonce: 1,
customNonceValue: '',
updateCustomNonce: jest.fn(),
getNextNonce: jest.fn(),
};
afterEach(() => {
jest.clearAllMocks();
});
it('should match snapshot', () => {
const { container } = renderWithProvider(
<CustomizeNonce {...props} />,
mockStore,
);
expect(container).toMatchSnapshot();
});
it('should change and submit custom nonce', async () => {
const { queryByTestId, queryByText } = renderWithProvider(
<CustomizeNonce {...props} />,
mockStore,
);
const nonceInputEvent = {
target: {
value: 101,
},
};
const nonceInput = queryByTestId('custom-nonce-input');
fireEvent.change(nonceInput, nonceInputEvent);
expect(nonceInput).toHaveValue(101);
const saveButton = queryByText('Save');
fireEvent.click(saveButton);
expect(props.updateCustomNonce).toHaveBeenCalledWith('101');
expect(props.getNextNonce).toHaveBeenCalled();
expect(mockHideModal).toHaveBeenCalled();
});
it('should handle emptry string custom nonce', () => {
const { queryByTestId, queryByText } = renderWithProvider(
<CustomizeNonce {...props} />,
mockStore,
);
const nonceInputEvent = {
target: {
value: '',
},
};
const nonceInput = queryByTestId('custom-nonce-input');
fireEvent.change(nonceInput, nonceInputEvent);
const saveButton = queryByText('Save');
fireEvent.click(saveButton);
expect(props.updateCustomNonce).toHaveBeenCalledWith(
props.customNonceValue,
);
});
it('should handle cancel', async () => {
const { queryByText } = renderWithProvider(
<CustomizeNonce {...props} />,
mockStore,
);
const modalClose = queryByText('Cancel');
fireEvent.click(modalClose);
expect(mockHideModal).toHaveBeenCalled();
});
it('should handle reset of nonce', async () => {
const { queryByTestId } = renderWithProvider(
<CustomizeNonce {...props} />,
mockStore,
);
const resetNonce = queryByTestId('customize-nonce-reset');
const nonceInputEvent = {
target: {
value: 101,
},
};
const nonceInput = queryByTestId('custom-nonce-input');
fireEvent.change(nonceInput, nonceInputEvent);
expect(nonceInput).toHaveValue(101);
fireEvent.click(resetNonce);
expect(nonceInput).toHaveValue(props.nextNonce);
});
});