|
| 1 | +import React from 'react'; |
| 2 | +import thunk from 'redux-thunk'; |
| 3 | +import configureStore from 'redux-mock-store'; |
| 4 | +import LoginForm from './LoginForm'; |
| 5 | +import * as actions from '../actions'; |
| 6 | +import { initialTestState } from '../../../testData/testReduxStore'; |
| 7 | +import { reduxRender, screen, fireEvent, act } from '../../../test-utils'; |
| 8 | + |
| 9 | +const mockStore = configureStore([thunk]); |
| 10 | +const store = mockStore(initialTestState); |
| 11 | + |
| 12 | +jest.mock('../actions', () => ({ |
| 13 | + ...jest.requireActual('../actions'), |
| 14 | + validateAndLoginUser: jest.fn().mockReturnValue( |
| 15 | + (dispatch) => |
| 16 | + new Promise((resolve) => { |
| 17 | + setTimeout(() => { |
| 18 | + dispatch({ type: 'AUTH_USER', payload: {} }); |
| 19 | + dispatch({ type: 'SET_PREFERENCES', payload: {} }); |
| 20 | + resolve(); |
| 21 | + }, 100); |
| 22 | + }) |
| 23 | + ) |
| 24 | +})); |
| 25 | + |
| 26 | +const subject = () => { |
| 27 | + reduxRender(<LoginForm />, { |
| 28 | + store |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +describe('<LoginForm/>', () => { |
| 33 | + test('Renders form with the correct fields.', () => { |
| 34 | + subject(); |
| 35 | + const emailTextElement = screen.getByText(/email or username/i); |
| 36 | + expect(emailTextElement).toBeInTheDocument(); |
| 37 | + |
| 38 | + const emailInputElement = screen.getByRole('textbox', { |
| 39 | + name: /email or username/i |
| 40 | + }); |
| 41 | + expect(emailInputElement).toBeInTheDocument(); |
| 42 | + |
| 43 | + const passwordTextElement = screen.getByText(/password/i); |
| 44 | + expect(passwordTextElement).toBeInTheDocument(); |
| 45 | + |
| 46 | + const passwordInputElement = screen.getByLabelText(/password/i); |
| 47 | + expect(passwordInputElement).toBeInTheDocument(); |
| 48 | + |
| 49 | + const loginButtonElement = screen.getByRole('button', { |
| 50 | + name: /log in/i |
| 51 | + }); |
| 52 | + expect(loginButtonElement).toBeInTheDocument(); |
| 53 | + }); |
| 54 | + test('Validate and login user is called with the correct values.', async () => { |
| 55 | + subject(); |
| 56 | + |
| 57 | + const emailElement = screen.getByRole('textbox', { |
| 58 | + name: /email or username/i |
| 59 | + }); |
| 60 | + fireEvent.change(emailElement, { |
| 61 | + target: { |
| 62 | + |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + const passwordElement = screen.getByLabelText(/password/i); |
| 67 | + |
| 68 | + fireEvent.change(passwordElement, { |
| 69 | + target: { |
| 70 | + value: 'correctpassword' |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + const loginButton = screen.getByRole('button', { |
| 75 | + name: /log in/i |
| 76 | + }); |
| 77 | + await act(async () => { |
| 78 | + fireEvent.click(loginButton); |
| 79 | + }); |
| 80 | + |
| 81 | + expect(actions.validateAndLoginUser).toHaveBeenCalledWith({ |
| 82 | + |
| 83 | + password: 'correctpassword' |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments