-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ResetPage.test.tsx
185 lines (154 loc) · 5.72 KB
/
ResetPage.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import "@testing-library/jest-dom";
import { act, cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { ReactElement, ReactNode } from "react";
import { Provider } from "react-redux";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import configureMockStore from "redux-mock-store";
import "tests/reactI18nextMock";
import PasswordReset, {
PasswordResetTestIds,
} from "components/PasswordReset/ResetPage";
import { Path } from "types/path";
const mockPasswordReset = jest.fn();
const mockValidateResetToken = jest.fn();
jest.mock("backend", () => ({
resetPassword: (...args: any[]) => mockPasswordReset(...args),
validateResetToken: (...args: any[]) => mockValidateResetToken(...args),
}));
// This test relies on nothing in the store so mock an empty store
const mockStore = configureMockStore()();
const setupMocks = (): void => {
mockValidateResetToken.mockResolvedValue(true);
};
beforeEach(() => {
jest.clearAllMocks();
setupMocks();
});
afterEach(cleanup);
const ResetPageProviders = ({
children,
}: {
children: ReactNode;
}): ReactElement => {
return (
<Provider store={mockStore}>
<MemoryRouter initialEntries={[`${Path.PwReset}/testPasswordReset`]}>
<Routes>
<Route path={`${Path.PwReset}/:token`} element={children} />
</Routes>
</MemoryRouter>
</Provider>
);
};
const customRender = async (ui: ReactElement): Promise<void> => {
await act(async () => {
render(ui, { wrapper: ResetPageProviders });
});
};
describe("PasswordReset", () => {
it("renders with password length error", async () => {
const user = userEvent.setup();
await customRender(<PasswordReset />);
const shortPassword = "foo";
const passwdField = screen.getByTestId(PasswordResetTestIds.Password);
const passwdConfirm = screen.getByTestId(
PasswordResetTestIds.ConfirmPassword
);
await act(async () => {
await user.type(passwdField, shortPassword);
await user.type(passwdConfirm, shortPassword);
});
const reqErrors = screen.queryAllByTestId(
PasswordResetTestIds.PasswordReqError
);
const confirmErrors = screen.queryAllByTestId(
PasswordResetTestIds.PasswordMatchError
);
const submitButton = screen.getByTestId(PasswordResetTestIds.SubmitButton);
expect(reqErrors.length).toBeGreaterThan(0);
expect(confirmErrors.length).toBe(0);
expect(submitButton.closest("button")).toBeDisabled();
});
it("renders with password match error", async () => {
const user = userEvent.setup();
await customRender(<PasswordReset />);
const passwordEntry = "password";
const confirmEntry = "passward";
const passwdField = screen.getByTestId(PasswordResetTestIds.Password);
const passwdConfirm = screen.getByTestId(
PasswordResetTestIds.ConfirmPassword
);
await act(async () => {
await user.type(passwdField, passwordEntry);
await user.type(passwdConfirm, confirmEntry);
});
const reqErrors = screen.queryAllByTestId(
PasswordResetTestIds.PasswordReqError
);
const confirmErrors = screen.queryAllByTestId(
PasswordResetTestIds.PasswordMatchError
);
const submitButton = screen.getByTestId(PasswordResetTestIds.SubmitButton);
expect(reqErrors.length).toBe(0);
expect(confirmErrors.length).toBeGreaterThan(0);
expect(submitButton.closest("button")).toBeDisabled();
});
it("renders with no password errors", async () => {
const user = userEvent.setup();
await customRender(<PasswordReset />);
const passwordEntry = "password";
const confirmEntry = "password";
const passwdField = screen.getByTestId(PasswordResetTestIds.Password);
const passwdConfirm = screen.getByTestId(
PasswordResetTestIds.ConfirmPassword
);
await act(async () => {
await user.type(passwdField, passwordEntry);
await user.type(passwdConfirm, confirmEntry);
});
const reqErrors = screen.queryAllByTestId(
PasswordResetTestIds.PasswordReqError
);
const confirmErrors = screen.queryAllByTestId(
PasswordResetTestIds.PasswordMatchError
);
const submitButton = screen.getByTestId(PasswordResetTestIds.SubmitButton);
expect(reqErrors.length).toBe(0);
expect(confirmErrors.length).toBe(0);
expect(submitButton.closest("button")).toBeEnabled();
});
it("renders with expire error", async () => {
// rerender the component with the resetFailure prop set.
const user = userEvent.setup();
await customRender(<PasswordReset />);
const passwordEntry = "password";
const confirmEntry = "password";
const passwdField = screen.getByTestId(PasswordResetTestIds.Password);
const passwdConfirm = screen.getByTestId(
PasswordResetTestIds.ConfirmPassword
);
await act(async () => {
await user.type(passwdField, passwordEntry);
await user.type(passwdConfirm, confirmEntry);
});
const submitButton = screen.getByTestId(PasswordResetTestIds.SubmitButton);
mockPasswordReset.mockResolvedValueOnce(false);
await act(async () => {
await user.click(submitButton);
});
const resetErrors = screen.queryAllByTestId(
PasswordResetTestIds.PasswordResetFail
);
expect(resetErrors.length).toBeGreaterThan(0);
});
it("renders the InvalidLink component if token not valid", async () => {
mockValidateResetToken.mockResolvedValueOnce(false);
await customRender(<PasswordReset />);
for (const id of Object.values(PasswordResetTestIds)) {
expect(screen.queryAllByTestId(id)).toHaveLength(0);
}
// The textId will show up as text because t() is mocked to return its input.
expect(screen.queryAllByText("passwordReset.invalidURL")).toBeTruthy;
});
});