-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IssueId #222311 feat: Writing Test Cases for LoginPage Component in ALL #104
Open
ajinkyapandetekdi
wants to merge
6
commits into
Sunbird-ALL:all-automation
Choose a base branch
from
ajinkyapandetekdi:all-automation
base: all-automation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
787d8e8
IssueId #222311 Writing Test Cases for LoginPage Component in ALL
ajinkyapandetekdi 56f2c4e
IssueId #222311 Writing Test Cases for LoginPage Component in ALL
ajinkyapandetekdi 28813cd
IssueId #222311 Writing Test Cases for LoginPage Component in ALL
ajinkyapandetekdi 481822d
IssueId #222311 Writing Test Cases for LoginPage Component in ALL
ajinkyapandetekdi e729a19
IssueId #222311 Writing Test Cases for LoginPage Component in ALL
ajinkyapandetekdi 3fa4325
IssueId #222311 Writing Test Cases for LoginPage Component in ALL
ajinkyapandetekdi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import React from "react"; | ||
import { render, fireEvent, screen, waitFor } from "@testing-library/react"; | ||
import axios from "axios"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import LoginPage from "./LoginPage"; | ||
import config from "../../utils/urlConstants.json"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
jest.spyOn(window, "alert").mockImplementation(() => {}); | ||
|
||
jest.mock("react-router-dom", () => ({ | ||
...jest.requireActual("react-router-dom"), | ||
useNavigate: jest.fn(), | ||
})); | ||
|
||
describe("LoginPage", () => { | ||
let mockNavigate; | ||
let mockAxios; | ||
|
||
beforeEach(() => { | ||
mockNavigate = jest.fn(); | ||
useNavigate.mockReturnValue(mockNavigate); | ||
mockAxios = new MockAdapter(axios); | ||
}); | ||
|
||
afterEach(() => { | ||
mockAxios.reset(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("shows alert when fields are empty", () => { | ||
ajinkyapandetekdi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Given the LoginPage is rendered | ||
render( | ||
<BrowserRouter> | ||
<LoginPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
// When the login button is clicked | ||
fireEvent.click(screen.getByRole("button", { name: "Login" })); | ||
|
||
// Then an alert should be shown indicating fields are empty | ||
expect(window.alert).toHaveBeenCalledWith("Please fill in all fields"); | ||
}); | ||
|
||
test("navigates to /discover-start on successful login", async () => { | ||
// Given the LoginPage is rendered | ||
mockAxios | ||
.onPost( | ||
`${process.env.REACT_APP_VIRTUAL_ID_HOST}/${config.URLS.GET_VIRTUAL_ID}?username=testuser` | ||
) | ||
.reply(200, { result: { virtualID: "12345" } }); | ||
|
||
render( | ||
<BrowserRouter> | ||
<LoginPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
// When the user enters a valid username and password and clicks login | ||
fireEvent.change(screen.getByLabelText("Username"), { | ||
target: { value: "testuser" }, | ||
}); | ||
fireEvent.change(screen.getByLabelText("Password"), { | ||
target: { value: "testpassword" }, | ||
}); | ||
fireEvent.click(screen.getByRole("button", { name: "Login" })); | ||
|
||
// Then localStorage should be set with the username and virtualId | ||
await waitFor(() => | ||
expect(localStorage.getItem("profileName")).toBe("testuser") | ||
); | ||
await waitFor(() => | ||
expect(localStorage.getItem("virtualId")).toBe("12345") | ||
); | ||
|
||
// And the user should be navigated to /discover-start | ||
expect(mockNavigate).toHaveBeenCalledWith("/discover-start"); | ||
}); | ||
|
||
test("displays entered username", () => { | ||
// Given the LoginPage is rendered | ||
render( | ||
<BrowserRouter> | ||
<LoginPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
// When the user enters a username | ||
const usernameInput = screen.getByLabelText("Username"); | ||
fireEvent.change(usernameInput, { target: { value: "testuser" } }); | ||
|
||
// Then the username input should display the entered value | ||
expect(usernameInput.value).toBe("testuser"); | ||
}); | ||
|
||
test("displays entered password", () => { | ||
// Given the LoginPage is rendered | ||
render( | ||
<BrowserRouter> | ||
<LoginPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
// When the user enters a password | ||
const passwordInput = screen.getByLabelText("Password"); | ||
fireEvent.change(passwordInput, { target: { value: "testpassword" } }); | ||
|
||
// Then the password input should display the entered value | ||
expect(passwordInput.value).toBe("testpassword"); | ||
}); | ||
|
||
test("shows alert on API error", async () => { | ||
// Given the LoginPage is rendered | ||
mockAxios | ||
.onPost( | ||
`${process.env.REACT_APP_VIRTUAL_ID_HOST}/${config.URLS.GET_VIRTUAL_ID}?username=testuser` | ||
) | ||
.networkError(); | ||
|
||
render( | ||
<BrowserRouter> | ||
<LoginPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
// When the user enters a valid username and password and clicks login | ||
fireEvent.change(screen.getByLabelText("Username"), { | ||
target: { value: "testuser" }, | ||
}); | ||
fireEvent.change(screen.getByLabelText("Password"), { | ||
target: { value: "testpassword" }, | ||
}); | ||
fireEvent.click(screen.getByRole("button", { name: "Login" })); | ||
|
||
// Then an alert should be shown indicating an API error | ||
await waitFor(() => | ||
expect(window.alert).toHaveBeenCalledWith( | ||
"An error occurred. Please try again later." | ||
) | ||
); | ||
}); | ||
|
||
test("Set localStorage on form submission", async () => { | ||
// Given the LoginPage is rendered and localStorage has initial values | ||
mockAxios | ||
.onPost( | ||
`${process.env.REACT_APP_VIRTUAL_ID_HOST}/${config.URLS.GET_VIRTUAL_ID}?username=testuser` | ||
) | ||
.reply(200, { result: { virtualID: "12345" } }); | ||
|
||
localStorage.setItem("profileName", "oldProfileName"); | ||
localStorage.setItem("virtualId", "oldVirtualId"); | ||
|
||
render( | ||
<BrowserRouter> | ||
<LoginPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
// When the user enters a valid username and password and clicks login | ||
fireEvent.change(screen.getByLabelText("Username"), { | ||
target: { value: "testuser" }, | ||
}); | ||
fireEvent.change(screen.getByLabelText("Password"), { | ||
target: { value: "testpassword" }, | ||
}); | ||
fireEvent.click(screen.getByRole("button", { name: "Login" })); | ||
|
||
// Then localStorage should be updated with the new values | ||
await waitFor(() => | ||
expect(localStorage.getItem("profileName")).toBe("testuser") | ||
); | ||
await waitFor(() => | ||
expect(localStorage.getItem("virtualId")).toBe("12345") | ||
); | ||
}); | ||
|
||
test("renders form fields correctly", () => { | ||
// Given the LoginPage is rendered | ||
render( | ||
<BrowserRouter> | ||
<LoginPage /> | ||
</BrowserRouter> | ||
); | ||
|
||
// Then the username, password fields and login button should be in the document | ||
expect(screen.getByLabelText("Username")).toBeInTheDocument(); | ||
expect(screen.getByLabelText("Password")).toBeInTheDocument(); | ||
expect(screen.getByRole("button", { name: "Login" })).toBeInTheDocument(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this is removed from dev dependencies?