Skip to content

Commit

Permalink
test: trim whitespace in text user inputs; allow custom IDs for ONSPa…
Browse files Browse the repository at this point in the history
…sswordInputs
  • Loading branch information
kristian4res committed Nov 4, 2024
1 parent f2791cc commit 54e8685
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
71 changes: 59 additions & 12 deletions src/pages/users/UserProfileEdits/ChangePassword.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jest.mock("react-router-dom", () => ({
jest.mock("blaise-login-react/blaise-login-react-client", () => ({
AuthManager: jest.fn().mockImplementation(() => ({
authHeader: () => ({
Authorization: "Bearer " + process.env.MOCK_AUTH_TOKEN
Authorization: process.env.MOCK_AUTH_TOKEN
})
}))
}));
Expand All @@ -42,7 +42,10 @@ beforeEach(() => {
(useParams as jest.Mock).mockReturnValue({ user: mockUserDetails.name });
});

afterEach(() => cleanup());
afterEach(() => {
jest.clearAllMocks();
cleanup();
});

describe("ChangePassword Component", () => {
it("matches the snapshot", async () => {
Expand All @@ -55,6 +58,26 @@ describe("ChangePassword Component", () => {
expect(asFragment()).toMatchSnapshot();
});

it("displays error message when passwords are empty", async () => {
const { findByText, getByLabelText, getByText } = render(
<MemoryRouter initialEntries={[mockState]}>
<ChangePassword />
</MemoryRouter>
);

const newPasswordInput = getByLabelText("New password");
const confirmPasswordInput = getByLabelText("Confirm password");
const saveButton = getByText("Save");

act(() => {
userEvent.type(newPasswordInput, "");
userEvent.type(confirmPasswordInput, "");
userEvent.click(saveButton);
});

expect(await findByText(/Passwords cannot be blank/i)).toBeVisible();
});

it("displays error message when passwords do not match", async () => {
const { findByText, getByLabelText, getByText } = render(
<MemoryRouter initialEntries={[mockState]}>
Expand All @@ -68,13 +91,39 @@ describe("ChangePassword Component", () => {

act(() => {
userEvent.type(newPasswordInput, "password123");
userEvent.type(confirmPasswordInput, "password321");
userEvent.type(confirmPasswordInput, "password321333");
userEvent.click(saveButton);
});

expect(await findByText(/Passwords do not match/i)).toBeVisible();
});

it("calls fetch with correct parameters upon form submission with matching passwords that remove any trailing whitespaces", async () => {
const { getByLabelText, getByText, findByText } = render(

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable findByText.
<MemoryRouter initialEntries={[mockState]}>
<ChangePassword />
</MemoryRouter>
);

const newPasswordInput = getByLabelText("New password");
const confirmPasswordInput = getByLabelText("Confirm password");
const saveButton = getByText("Save");

// Wait for state update
act(() => {
userEvent.type(newPasswordInput, "password123 ");
userEvent.type(confirmPasswordInput, "password123 ");
userEvent.click(saveButton);
});

expect(fetch).toHaveBeenCalledWith("/api/change-password/testUser", {
headers: {
Authorization: process.env.MOCK_AUTH_TOKEN,
password: "password123"
}
});
});

it("calls fetch with correct parameters upon form submission with matching passwords", async () => {
const { getByLabelText, getByText, findByText } = render(
<MemoryRouter initialEntries={[mockState]}>
Expand All @@ -87,19 +136,17 @@ describe("ChangePassword Component", () => {
const saveButton = getByText("Save");

// Wait for state update
act(async () => {
act(() => {
userEvent.type(newPasswordInput, "password123");
userEvent.type(confirmPasswordInput, "password123");
userEvent.click(saveButton);
});

// Improvement: Figure out why the fetch function is not being called
// expect(fetch).toHaveBeenCalledTimes(1);
// expect(fetch).toHaveBeenCalledWith("/api/change-password/testUser", {
// "headers": {
// "Authorization": "Bearer " + process.env.MOCK_AUTH_TOKEN,
// "password": "password123"
// }
// });
expect(fetch).toHaveBeenCalledWith("/api/change-password/testUser", {
headers: {
Authorization: process.env.MOCK_AUTH_TOKEN,
password: "password123"
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ exports[`ChangePassword Component matches the snapshot 1`] = `
>
<label
class="ons-label"
for="password"
for="new-password"
>
New password
</label>
Expand All @@ -128,7 +128,7 @@ exports[`ChangePassword Component matches the snapshot 1`] = `
<input
class="ons-input ons-input--text ons-input-type__input ons-u-mt-xs"
data-testid="login-password-input"
id="password"
id="new-password"
type="password"
value=""
/>
Expand All @@ -138,7 +138,7 @@ exports[`ChangePassword Component matches the snapshot 1`] = `
>
<label
class="ons-label"
for="password"
for="confirm-password"
>
Confirm password
</label>
Expand All @@ -163,7 +163,7 @@ exports[`ChangePassword Component matches the snapshot 1`] = `
<input
class="ons-input ons-input--text ons-input-type__input ons-u-mt-xs"
data-testid="login-password-input"
id="password"
id="confirm-password"
type="password"
value=""
/>
Expand Down

0 comments on commit 54e8685

Please sign in to comment.