forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue-processing#2923 added test for the Login Form.
- Loading branch information
1 parent
bc20b47
commit 37727bf
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from 'react'; | ||
import thunk from 'redux-thunk'; | ||
import configureStore from 'redux-mock-store'; | ||
import LoginForm from './LoginForm'; | ||
import * as actions from '../actions'; | ||
import { initialTestState } from '../../../testData/testReduxStore'; | ||
import { reduxRender, screen, fireEvent, act } from '../../../test-utils'; | ||
|
||
const mockStore = configureStore([thunk]); | ||
const store = mockStore(initialTestState); | ||
|
||
jest.mock('../actions', () => ({ | ||
...jest.requireActual('../actions'), | ||
validateAndLoginUser: jest.fn().mockReturnValue( | ||
(dispatch) => | ||
new Promise((resolve) => { | ||
setTimeout(() => { | ||
dispatch({ type: 'AUTH_USER', payload: {} }); | ||
dispatch({ type: 'SET_PREFERENCES', payload: {} }); | ||
resolve(); | ||
}, 100); | ||
}) | ||
) | ||
})); | ||
|
||
const subject = () => { | ||
reduxRender(<LoginForm />, { | ||
store | ||
}); | ||
}; | ||
|
||
describe('<LoginForm/>', () => { | ||
test('Renders form with the correct fields.', () => { | ||
subject(); | ||
const emailTextElement = screen.getByText(/email or username/i); | ||
expect(emailTextElement).toBeInTheDocument(); | ||
|
||
const emailInputElement = screen.getByRole('textbox', { | ||
name: /email or username/i | ||
}); | ||
expect(emailInputElement).toBeInTheDocument(); | ||
|
||
const passwordTextElement = screen.getByText(/password/i); | ||
expect(passwordTextElement).toBeInTheDocument(); | ||
|
||
const passwordInputElement = screen.getByLabelText(/password/i); | ||
expect(passwordInputElement).toBeInTheDocument(); | ||
|
||
const loginButtonElement = screen.getByRole('button', { | ||
name: /log in/i | ||
}); | ||
expect(loginButtonElement).toBeInTheDocument(); | ||
}); | ||
test('Validate and login user is called with the correct values.', async () => { | ||
subject(); | ||
|
||
const emailElement = screen.getByRole('textbox', { | ||
name: /email or username/i | ||
}); | ||
fireEvent.change(emailElement, { | ||
target: { | ||
value: '[email protected]' | ||
} | ||
}); | ||
|
||
const passwordElement = screen.getByLabelText(/password/i); | ||
|
||
fireEvent.change(passwordElement, { | ||
target: { | ||
value: 'correctpassword' | ||
} | ||
}); | ||
|
||
const loginButton = screen.getByRole('button', { | ||
name: /log in/i | ||
}); | ||
await act(async () => { | ||
fireEvent.click(loginButton); | ||
}); | ||
|
||
expect(actions.validateAndLoginUser).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'correctpassword' | ||
}); | ||
}); | ||
}); |