-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
54 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
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 |
---|---|---|
|
@@ -6,63 +6,67 @@ import axios from "axios"; | |
import MockAdapter from "axios-mock-adapter"; | ||
import { act } from "react-dom/test-utils"; | ||
import { beforeEach, beforeAll, vi, describe, it, expect } from "vitest"; | ||
import { debug } from "vitest-preview"; | ||
const user = userEvent.setup(); | ||
|
||
beforeEach(async () => { | ||
await act(async () => { | ||
render(<PasswordReset />); | ||
}); | ||
await act(async () => { | ||
render(<PasswordReset />); | ||
}); | ||
}); | ||
|
||
beforeAll(() => { | ||
vi.mock("next/navigation", async (importOriginal) => { | ||
const actual = (await importOriginal()) as Object; | ||
return { | ||
...actual, | ||
useRouter: vi.fn(() => ({ | ||
push: vi.fn(), | ||
})), | ||
// giberish token | ||
usePathname: vi.fn().mockImplementation(() => "/account/login/"), | ||
}; | ||
}); | ||
vi.mock("next/navigation", async (importOriginal) => { | ||
const actual = (await importOriginal()) as Object; | ||
return { | ||
...actual, | ||
useRouter: vi.fn(() => ({ | ||
push: vi.fn(), | ||
})), | ||
// giberish token | ||
usePathname: vi.fn().mockImplementation(() => "/account/login/"), | ||
}; | ||
}); | ||
}); | ||
|
||
describe('User attempts reset password:', () => { | ||
it('User enters invalid email', async () => { | ||
const email = screen.getByPlaceholderText(/email/i); | ||
await act(async () => { | ||
await user.type(email, "ajacobs") | ||
await clickAway(user) | ||
}) | ||
expect(screen.getByText("Invalid email")).toBeInTheDocument() | ||
await act(async () => { | ||
await user.clear(email) | ||
}) | ||
expect(screen.getByText("Required")).toBeInTheDocument() | ||
it('User enters invalid email', async () => { | ||
const email = screen.getByPlaceholderText(/email/i); | ||
await act(async () => { | ||
await user.type(email, "ajacobs") | ||
await clickAway(user) | ||
}) | ||
expect(screen.getByText("Invalid email")).toBeInTheDocument() | ||
await act(async () => { | ||
await user.clear(email) | ||
}) | ||
it('User enters an email that does not exist.', async () => { | ||
const email = screen.getByPlaceholderText(/email/i); | ||
expect(screen.getByText("Required")).toBeInTheDocument() | ||
}) | ||
it('User enters an email that does not exist.', async () => { | ||
const email = screen.getByPlaceholderText(/email/i); | ||
|
||
// This sets the mock adapter on the default instance | ||
let mock = new MockAdapter(axios); | ||
mock.onPost().reply(400, "User does not exist") | ||
await act(async () => { | ||
await user.type(email, "[email protected]") | ||
await user.click(screen.getByText(/submit/i)) | ||
}) | ||
await waitFor(() => { | ||
expect(screen.getByText("User does not exist")).toBeInTheDocument(); | ||
}); | ||
// This sets the mock adapter on the default instance | ||
let mock = new MockAdapter(axios); | ||
mock.onPost().reply(400, { | ||
error: 'User does not exist' | ||
}) | ||
await act(async () => { | ||
await user.type(email, "[email protected]") | ||
await user.click(screen.getByText(/submit/i)) | ||
}) | ||
it('User requests valid password reset for email.', async () => { | ||
const email = screen.getByPlaceholderText(/email/i); | ||
// This sets the mock adapter on the default instance | ||
let mock = new MockAdapter(axios); | ||
mock.onPost().reply(200, "Password reset sent") | ||
await act(async () => { | ||
await user.type(email, "[email protected]") | ||
await user.click(screen.getByText(/submit/i)) | ||
}) | ||
debug(); | ||
await waitFor(() => { | ||
expect(screen.getByText("User does not exist")).toBeInTheDocument(); | ||
}); | ||
}) | ||
it('User requests valid password reset for email.', async () => { | ||
const email = screen.getByPlaceholderText(/email/i); | ||
// This sets the mock adapter on the default instance | ||
let mock = new MockAdapter(axios); | ||
mock.onPost().reply(200, "Password reset sent") | ||
await act(async () => { | ||
await user.type(email, "[email protected]") | ||
await user.click(screen.getByText(/submit/i)) | ||
}) | ||
}) | ||
}) | ||
}) |
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