Skip to content

Commit 4a85b37

Browse files
authored
Fix some bugs and produce test file. (#15)
1 parent 1f385f7 commit 4a85b37

File tree

2 files changed

+102
-6
lines changed

2 files changed

+102
-6
lines changed

src/components/User.test.tsx

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import "@testing-library/jest-dom";
2+
3+
import { fireEvent, render } from "@testing-library/react";
4+
import { Avatar } from "@mui/material";
5+
import { User } from "./User";
6+
7+
describe("User", () => {
8+
it("should render", () => {
9+
render(<User onLogin={() => {}} onLogout={() => {}} user={null} />);
10+
render(<User onLogout={() => {}} user={null} />);
11+
render(<User onLogin={() => {}} user={null} />);
12+
render(<User user={null} />);
13+
});
14+
15+
it("should display login button when not authenticated", () => {
16+
const {getByText} = render(<User onLogin={() => {}} onLogout={() => {}} user={null} />);
17+
const loginButton = getByText("Login");
18+
19+
expect(loginButton).toBeInTheDocument()
20+
});
21+
22+
it("should display logout menuitem when authenticated", () => {
23+
const {getByRole, getByText} = render(<User onLogin={() => {}} onLogout={() => {}} user={{ name: "Name", fedid: "FedID" }} />);
24+
25+
const userMenu = getByRole("button");
26+
fireEvent.click(userMenu);
27+
28+
const logoutMenuItem = getByText("Logout");
29+
expect(logoutMenuItem).toBeInTheDocument();
30+
});
31+
32+
it("should fire login callback when button is clicked", () => {
33+
const loginCallback = jest.fn();
34+
const {getByText} = render(<User onLogin={loginCallback} user={null} />);
35+
36+
const loginButton = getByText("Login");
37+
fireEvent.click(loginButton);
38+
39+
expect(loginCallback).toHaveBeenCalled();
40+
});
41+
42+
it("should fire logout callback when button is clicked", () => {
43+
const logoutCallback = jest.fn();
44+
const {getByRole, getByText} = render(
45+
<User onLogout={logoutCallback} user={{ name: "Name", fedid: "FedID" }} />,
46+
);
47+
const userMenu = getByRole("button");
48+
fireEvent.click(userMenu);
49+
50+
const logoutMenuItem = getByText("Logout");
51+
fireEvent.click(logoutMenuItem);
52+
53+
expect(logoutCallback).toHaveBeenCalled();
54+
});
55+
56+
it("should display name and FedID", () => {
57+
const name = "A Name",
58+
fedId = "FED14000";
59+
const {getByText} = render(<User user={{ name: name, fedid: fedId }} />);
60+
61+
expect(getByText(name)).toBeInTheDocument();
62+
expect(getByText(fedId)).toBeInTheDocument();
63+
});
64+
65+
it("should not have logout when no onLogout", () => {
66+
const {getByRole, queryByText} = render(
67+
<User user={{ name: "Name", fedid: "FedID" }} />,
68+
);
69+
const userMenu = getByRole("button");
70+
fireEvent.click(userMenu);
71+
72+
const logoutMenuItem = queryByText("Logout");
73+
expect(logoutMenuItem).not.toBeInTheDocument()
74+
});
75+
76+
it("should render a new avatar", () => {
77+
const avatarInitials = "MW"
78+
const {queryByText} = render(
79+
<User user={{ name: "Name", fedid: "FedID" }}
80+
avatar={<Avatar>{avatarInitials}</Avatar>}
81+
/>,
82+
);
83+
84+
const avatar = queryByText(avatarInitials);
85+
expect(avatar).toBeInTheDocument()
86+
});
87+
88+
});

src/components/User.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ interface UserProps {
2222

2323
const User = ({ user, onLogin, onLogout, avatar, color }: UserProps) => {
2424
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
25-
2625
const open = Boolean(anchorEl);
26+
2727
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
2828
setAnchorEl(event.currentTarget);
2929
};
3030
const handleClose = () => {
31-
3231
setAnchorEl(null);
3332
};
34-
33+
34+
const handleLogin = () => {
35+
if(onLogin) onLogin()
36+
}
37+
const handleLogout = () => {
38+
handleClose()
39+
if(onLogout) onLogout()
40+
}
41+
3542
const theme = useTheme();
3643

3744
return (
@@ -82,20 +89,21 @@ const User = ({ user, onLogin, onLogout, avatar, color }: UserProps) => {
8289
</div>
8390
</Stack>
8491
</Button>
85-
<Menu
92+
{onLogout && <Menu
8693
id="menu-list"
8794
anchorEl={anchorEl}
8895
open={open}
8996
onClose={handleClose}
9097
>
91-
<MenuItem onClick={onLogout} aria-label="Logout">
98+
<MenuItem onClick={handleLogout} aria-label="Logout">
9299
<Link sx={{ textDecoration: "none" }}>Logout</Link>
93100
</MenuItem>
94101
</Menu>
102+
}
95103
</>
96104
) : (
97105
<Button
98-
onClick={onLogin}
106+
onClick={handleLogin}
99107
startIcon={<MdLogin />}
100108
sx={{
101109
backgroundColor: theme.palette.primary.light,

0 commit comments

Comments
 (0)