-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance layout, add tests, and improve folder structure in Libr…
…aryPage
- Loading branch information
Showing
12 changed files
with
189 additions
and
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,38 @@ | ||
import { render, screen, fireEvent, act } from "@testing-library/react"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { LibraryPage } from "./LibraryPage"; | ||
import { BooksProps } from "./components"; | ||
import { useLibraryPage } from "./hooks"; | ||
import { vi } from "vitest"; | ||
|
||
const mockBooks = vi.fn(); | ||
vi.mock("./components/Books/Books", () => ({ | ||
Books: (props: BooksProps) => { | ||
mockBooks(props); | ||
return <div>Books</div>; | ||
}, | ||
vi.mock("./hooks", () => ({ | ||
useLibraryPage: vi.fn(), | ||
})); | ||
|
||
describe("LibraryPage", () => { | ||
it("renders correctly", () => { | ||
render(<LibraryPage />); | ||
|
||
expect(screen.getByText("Books")).toBeInTheDocument(); | ||
vi.mock("./components/Books", () => ({ | ||
Books: () => <div>Books</div>, | ||
})); | ||
|
||
expect(mockBooks).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
search: "", // Initial debouncedSearch value | ||
limit: 50, | ||
}) | ||
); | ||
}); | ||
vi.mock("./components/Search", () => ({ | ||
Search: () => <div>Search</div>, | ||
})); | ||
|
||
it("updates search state correctly", async () => { | ||
render(<LibraryPage />); | ||
vi.mock("./components/UserAuthentication", () => ({ | ||
UserAuthentication: () => <div>UserAuthentication</div>, | ||
})); | ||
|
||
// Simulate user input to the SearchInput component | ||
fireEvent.change(screen.getByLabelText("Search by title or author"), { | ||
target: { value: "New search value" }, | ||
describe("LibraryPage", () => { | ||
it("renders without crashing", () => { | ||
(useLibraryPage as jest.Mock).mockReturnValue({ | ||
search: "", | ||
setSearch: vi.fn(), | ||
debouncedSearch: "", | ||
error: null, | ||
handleLogout: vi.fn(), | ||
setError: vi.fn(), | ||
}); | ||
|
||
// Wait for the debounce delay | ||
await act(() => new Promise((resolve) => setTimeout(resolve, 500))); | ||
|
||
// Check that the Books component was called with the updated debouncedSearch value | ||
expect(mockBooks).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
search: "New search value", | ||
}) | ||
); | ||
render(<LibraryPage />); | ||
expect(screen.getByText("Books")).toBeInTheDocument(); | ||
expect(screen.getByText("Search")).toBeInTheDocument(); | ||
expect(screen.getByText("UserAuthentication")).toBeInTheDocument(); | ||
}); | ||
}); |
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,32 @@ | ||
import { ChangeEvent, Dispatch, SetStateAction } from "react"; | ||
import { Box } from "@mui/material"; | ||
import { StyledTextField } from "./SearchInput.styles"; | ||
|
||
interface SearchProps { | ||
search: string; | ||
setSearch: Dispatch<SetStateAction<string>>; | ||
} | ||
|
||
export const Search = ({ search, setSearch }: SearchProps) => { | ||
const handleSearchChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setSearch(event.target.value); | ||
}; | ||
|
||
return ( | ||
<Box | ||
py={2} | ||
display="flex" | ||
justifyContent="center" | ||
width="100%" | ||
alignSelf="center" | ||
> | ||
<StyledTextField | ||
label="Search by title or author" | ||
variant="outlined" | ||
onChange={handleSearchChange} | ||
fullWidth | ||
value={search} | ||
/> | ||
</Box> | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
src/components/LibraryPage/components/Search/SearchInput.styles.ts
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,22 @@ | ||
import { TextField } from "@mui/material"; | ||
import { styled } from "@mui/material/styles"; | ||
|
||
export const StyledTextField = styled(TextField)(({ theme }) => { | ||
return ` | ||
.MuiInputBase-input { | ||
background-color: ${ | ||
theme.palette?.mode === "dark" | ||
? "rgba(255, 255, 255, 0.15)" | ||
: undefined | ||
}; | ||
color: ${theme.palette?.mode === "dark" ? "#fff" : undefined}; | ||
&::placeholder { | ||
color: ${ | ||
theme.palette?.mode === "dark" | ||
? "rgba(255, 255, 255, 0.5)" | ||
: undefined | ||
}; | ||
} | ||
} | ||
`; | ||
}); |
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 @@ | ||
export * from "./Search"; |
40 changes: 40 additions & 0 deletions
40
src/components/LibraryPage/components/UserAuthentication/UserAuthentication.test.tsx
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,40 @@ | ||
import { render, fireEvent, waitFor, screen } from "@testing-library/react"; | ||
import { UserAuthentication } from "./UserAuthentication"; | ||
import { useAuth0 } from "@auth0/auth0-react"; | ||
import { vi } from "vitest"; | ||
|
||
vi.mock("@auth0/auth0-react", () => ({ | ||
useAuth0: vi.fn(), | ||
})); | ||
|
||
describe("UserAuthentication", () => { | ||
it("should render loading state", () => { | ||
(useAuth0 as jest.Mock).mockReturnValue({ | ||
isLoading: true, | ||
}); | ||
|
||
render(<UserAuthentication />); | ||
expect(screen.getByRole("progressbar")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render authenticated state", async () => { | ||
(useAuth0 as jest.Mock).mockReturnValue({ | ||
isLoading: false, | ||
user: { name: "Test User" }, | ||
}); | ||
|
||
render(<UserAuthentication />); | ||
fireEvent.click(screen.getByText("T")); | ||
await waitFor(() => expect(screen.getByRole("menu")).toBeInTheDocument()); | ||
}); | ||
|
||
it("should render unauthenticated state", () => { | ||
(useAuth0 as jest.Mock).mockReturnValue({ | ||
isLoading: false, | ||
user: null, | ||
}); | ||
|
||
render(<UserAuthentication />); | ||
expect(screen.getByRole("button", { name: /log in/i })).toBeInTheDocument(); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
src/components/LibraryPage/components/UserAuthentication/UserAuthentication.tsx
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,49 @@ | ||
import { Avatar, CircularProgress, Menu, MenuItem } from "@mui/material"; | ||
import { useAuth0 } from "@auth0/auth0-react"; | ||
import { useState, useCallback } from "react"; | ||
import { LoginButton } from "../LoginButton"; | ||
|
||
export const UserAuthentication = () => { | ||
const { user, isLoading, logout } = useAuth0(); | ||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); | ||
|
||
const handleMenuOpen = useCallback( | ||
(event: React.MouseEvent<HTMLDivElement>) => { | ||
setAnchorEl(event.currentTarget); | ||
}, | ||
[] | ||
); | ||
|
||
const handleMenuClose = useCallback(() => { | ||
setAnchorEl(null); | ||
}, []); | ||
|
||
const handleLogout = useCallback( | ||
(event: React.MouseEvent<HTMLLIElement>) => { | ||
event.preventDefault(); | ||
logout(); | ||
}, | ||
[logout] | ||
); | ||
|
||
if (isLoading) { | ||
return <CircularProgress />; | ||
} | ||
|
||
if (user?.name) { | ||
return ( | ||
<div> | ||
<Avatar onClick={handleMenuOpen}>{user.name[0]}</Avatar> | ||
<Menu | ||
anchorEl={anchorEl} | ||
open={Boolean(anchorEl)} | ||
onClose={handleMenuClose} | ||
> | ||
<MenuItem onClick={handleLogout}>Logout</MenuItem> | ||
</Menu> | ||
</div> | ||
); | ||
} | ||
|
||
return <LoginButton />; | ||
}; |
1 change: 1 addition & 0 deletions
1
src/components/LibraryPage/components/UserAuthentication/index.ts
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 @@ | ||
export * from "./UserAuthentication"; |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "./Books"; | ||
export * from "./Search"; | ||
export * from "./UserAuthentication"; |
Oops, something went wrong.