|
| 1 | +import React from 'react'; |
| 2 | +import thunk from 'redux-thunk'; |
| 3 | +import configureStore from 'redux-mock-store'; |
| 4 | +import { |
| 5 | + reduxRender, |
| 6 | + screen, |
| 7 | + fireEvent, |
| 8 | + act, |
| 9 | + waitFor |
| 10 | +} from '../../../test-utils'; |
| 11 | +import AccountForm from './AccountForm'; |
| 12 | +import { initialTestState } from '../../../testData/testReduxStore'; |
| 13 | +import * as actions from '../actions'; |
| 14 | + |
| 15 | +const mockStore = configureStore([thunk]); |
| 16 | +const store = mockStore(initialTestState); |
| 17 | + |
| 18 | +jest.mock('../actions', () => ({ |
| 19 | + ...jest.requireActual('../actions'), |
| 20 | + updateSettings: jest.fn().mockReturnValue( |
| 21 | + (dispatch) => |
| 22 | + new Promise((resolve) => { |
| 23 | + setTimeout(() => { |
| 24 | + dispatch({ type: 'UPDATE_SETTINGS', payload: {} }); |
| 25 | + resolve(); |
| 26 | + }, 100); |
| 27 | + }) |
| 28 | + ) |
| 29 | +})); |
| 30 | + |
| 31 | +const subject = () => { |
| 32 | + reduxRender(<AccountForm />, { |
| 33 | + store |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +describe('<AccountForm />', () => { |
| 38 | + it('renders form fields with initial values', () => { |
| 39 | + subject(); |
| 40 | + const emailElement = screen.getByRole('textbox', { |
| 41 | + name: /email/i |
| 42 | + }); |
| 43 | + expect(emailElement).toBeInTheDocument(); |
| 44 | + expect(emailElement).toHaveValue('[email protected]'); |
| 45 | + |
| 46 | + const userNameElement = screen.getByRole('textbox', { |
| 47 | + name: /username/i |
| 48 | + }); |
| 49 | + expect(userNameElement).toBeInTheDocument(); |
| 50 | + expect(userNameElement).toHaveValue('happydog'); |
| 51 | + |
| 52 | + const currentPasswordElement = screen.getByLabelText(/current password/i); |
| 53 | + expect(currentPasswordElement).toBeInTheDocument(); |
| 54 | + expect(currentPasswordElement).toHaveValue(''); |
| 55 | + |
| 56 | + const newPasswordElement = screen.getByLabelText(/new password/i); |
| 57 | + expect(newPasswordElement).toBeInTheDocument(); |
| 58 | + expect(newPasswordElement).toHaveValue(''); |
| 59 | + }); |
| 60 | + |
| 61 | + it('handles form submission and calls updateSettings', async () => { |
| 62 | + subject(); |
| 63 | + |
| 64 | + const saveAllSettingsButton = screen.getByRole('button', { |
| 65 | + name: /save all settings/i |
| 66 | + }); |
| 67 | + |
| 68 | + const currentPasswordElement = screen.getByLabelText(/current password/i); |
| 69 | + const newPasswordElement = screen.getByLabelText(/new password/i); |
| 70 | + |
| 71 | + fireEvent.change(currentPasswordElement, { |
| 72 | + target: { |
| 73 | + value: 'currentPassword' |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + fireEvent.change(newPasswordElement, { |
| 78 | + target: { |
| 79 | + value: 'newPassword' |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + await act(async () => { |
| 84 | + fireEvent.click(saveAllSettingsButton); |
| 85 | + }); |
| 86 | + |
| 87 | + await waitFor(() => { |
| 88 | + expect(actions.updateSettings).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('Save all setting button should get disabled while submitting and enable when not submitting', async () => { |
| 93 | + subject(); |
| 94 | + |
| 95 | + const saveAllSettingsButton = screen.getByRole('button', { |
| 96 | + name: /save all settings/i |
| 97 | + }); |
| 98 | + |
| 99 | + const currentPasswordElement = screen.getByLabelText(/current password/i); |
| 100 | + const newPasswordElement = screen.getByLabelText(/new password/i); |
| 101 | + |
| 102 | + fireEvent.change(currentPasswordElement, { |
| 103 | + target: { |
| 104 | + value: 'currentPassword' |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + fireEvent.change(newPasswordElement, { |
| 109 | + target: { |
| 110 | + value: 'newPassword' |
| 111 | + } |
| 112 | + }); |
| 113 | + expect(saveAllSettingsButton).not.toHaveAttribute('disabled'); |
| 114 | + |
| 115 | + await act(async () => { |
| 116 | + fireEvent.click(saveAllSettingsButton); |
| 117 | + await waitFor(() => { |
| 118 | + expect(saveAllSettingsButton).toHaveAttribute('disabled'); |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments