Skip to content

Commit 272645f

Browse files
authored
Merge pull request #2955 from gauravsingh94/issue-#2923
Issue-#2923 added test for the Account Form.
2 parents 41d0303 + 0bea803 commit 272645f

File tree

3 files changed

+292
-0
lines changed

3 files changed

+292
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import React from 'react';
2+
import thunk from 'redux-thunk';
3+
import configureStore from 'redux-mock-store';
4+
import { fireEvent } from '@storybook/testing-library';
5+
import { reduxRender, screen, act, waitFor } from '../../../test-utils';
6+
import { initialTestState } from '../../../testData/testReduxStore';
7+
import NewPasswordForm from './NewPasswordForm';
8+
9+
const mockStore = configureStore([thunk]);
10+
const store = mockStore(initialTestState);
11+
12+
const mockResetPasswordToken = 'mockResetToken';
13+
const subject = () => {
14+
reduxRender(<NewPasswordForm resetPasswordToken={mockResetPasswordToken} />, {
15+
store
16+
});
17+
};
18+
const mockDispatch = jest.fn();
19+
20+
jest.mock('react-redux', () => ({
21+
...jest.requireActual('react-redux'),
22+
useDispatch: () => mockDispatch
23+
}));
24+
25+
jest.mock('../../../utils/reduxFormUtils', () => ({
26+
validateNewPassword: jest.fn()
27+
}));
28+
29+
jest.mock('../actions', () => ({
30+
updatePassword: jest.fn().mockReturnValue(
31+
new Promise((resolve) => {
32+
resolve();
33+
})
34+
)
35+
}));
36+
37+
describe('<NewPasswordForm/>', () => {
38+
beforeEach(() => {
39+
mockDispatch.mockClear();
40+
jest.clearAllMocks();
41+
});
42+
test('renders form fields correctly', () => {
43+
subject();
44+
45+
const passwordInputElements = screen.getAllByLabelText(/Password/i);
46+
expect(passwordInputElements).toHaveLength(2);
47+
48+
const passwordInputElement = passwordInputElements[0];
49+
expect(passwordInputElement).toBeInTheDocument();
50+
51+
const confirmPasswordInputElement = passwordInputElements[1];
52+
expect(confirmPasswordInputElement).toBeInTheDocument();
53+
54+
const submitElemement = screen.getByRole('button', {
55+
name: /set new password/i
56+
});
57+
expect(submitElemement).toBeInTheDocument();
58+
});
59+
60+
test('submits form with valid data', async () => {
61+
subject();
62+
const passwordInputElements = screen.getAllByLabelText(/Password/i);
63+
64+
const passwordInputElement = passwordInputElements[0];
65+
fireEvent.change(passwordInputElement, {
66+
target: { value: 'password123' }
67+
});
68+
69+
const confirmPasswordInputElement = passwordInputElements[1];
70+
fireEvent.change(confirmPasswordInputElement, {
71+
target: { value: 'password123' }
72+
});
73+
74+
const submitElemement = screen.getByRole('button', {
75+
name: /set new password/i
76+
});
77+
78+
await act(async () => {
79+
fireEvent.click(submitElemement);
80+
});
81+
82+
await waitFor(() => expect(mockDispatch).toHaveBeenCalled());
83+
});
84+
});

0 commit comments

Comments
 (0)