Skip to content

Commit e01339e

Browse files
committed
test edit hours component
1 parent 866a675 commit e01339e

File tree

2 files changed

+96
-18
lines changed

2 files changed

+96
-18
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { useState } from "react";
2+
import { render, fireEvent } from "@testing-library/react";
3+
import { configureStore } from "@reduxjs/toolkit";
4+
import { Provider } from "react-redux";
5+
import { rootReducer } from "@/store/store";
6+
import { useUser } from "@/store/hooks";
7+
import { teamMemberData } from "@/app/(main)/my-voyage/[teamId]/directory/components/fixtures/TeamMember";
8+
import TeamMember from "@/app/(main)/my-voyage/[teamId]/directory/components/TeamMember";
9+
import TextInput from "@/components/inputs/TextInput";
10+
import Button from "@/components/Button";
11+
12+
jest.mock("@/store/hooks", () => ({
13+
useUser: jest.fn(),
14+
}));
15+
16+
jest.mock(
17+
"@/app/(main)/my-voyage/[teamId]/directory/components/EditHours",
18+
() =>
19+
function EditHoursMock() {
20+
const [isEditing, setIsEditing] = useState(false);
21+
22+
return isEditing ? (
23+
<TextInput
24+
placeholder={`${teamMemberData.hrPerSprint}`}
25+
defaultValue={`${teamMemberData.hrPerSprint}`}
26+
id="avgHours"
27+
/>
28+
) : (
29+
<Button
30+
onClick={() => setIsEditing(true)}
31+
aria-label="average hour per sprint"
32+
>
33+
{`${teamMemberData.hrPerSprint}`}
34+
</Button>
35+
);
36+
},
37+
);
38+
39+
const renderWithStore = (userId: number) => {
40+
const store = configureStore({
41+
reducer: rootReducer,
42+
});
43+
44+
(useUser as jest.Mock).mockReturnValue({
45+
voyageTeamMembers: [{ id: userId }],
46+
});
47+
48+
return render(
49+
<Provider store={store}>
50+
<TeamMember teamMember={teamMemberData} />
51+
</Provider>,
52+
);
53+
};
54+
55+
describe("Edit Hours Component", () => {
56+
beforeEach(() => {
57+
jest.clearAllMocks();
58+
});
59+
60+
it("current user has a button to edit their hours", () => {
61+
const screen = renderWithStore(20);
62+
63+
screen.debug();
64+
65+
const buttonAction = screen.queryByRole("button", {
66+
name: "average hour per sprint",
67+
});
68+
69+
expect(buttonAction).toBeInTheDocument();
70+
});
71+
72+
it("current user can edit their hours", () => {
73+
const screen = renderWithStore(20);
74+
75+
const buttonAction = screen.getByRole("button", {
76+
name: "average hour per sprint",
77+
});
78+
79+
fireEvent.click(buttonAction);
80+
81+
const textInputAction = screen.getByPlaceholderText("10");
82+
83+
// assert that the button is a textInput
84+
expect(textInputAction).toBeInTheDocument();
85+
86+
fireEvent.change(textInputAction, { target: { value: "15" } });
87+
88+
// Assert that the input is edited to a new value
89+
expect((textInputAction as HTMLInputElement).value).toBe("15");
90+
});
91+
});

src/app/(main)/my-voyage/[teamId]/directory/components/__tests__/TeamMember.test.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { configureStore } from "@reduxjs/toolkit";
33
import { Provider } from "react-redux";
44
import { rootReducer } from "@/store/store";
55
import { useUser } from "@/store/hooks";
6-
import { teamMemberData } from "../fixtures/TeamMember";
7-
import { type VoyageTeam } from "@/store/features/directory/directorySlice";
6+
import { teamMemberData } from "@/app/(main)/my-voyage/[teamId]/directory/components/fixtures/TeamMember";
87
import TeamMember from "@/app/(main)/my-voyage/[teamId]/directory/components/TeamMember";
98

109
jest.mock("@/store/hooks", () => ({
@@ -23,7 +22,7 @@ jest.mock(
2322
},
2423
);
2524

26-
const renderWithStore = (teamMember: VoyageTeam, userId: number) => {
25+
const renderWithStore = (userId: number) => {
2726
const store = configureStore({
2827
reducer: rootReducer,
2928
});
@@ -34,7 +33,7 @@ const renderWithStore = (teamMember: VoyageTeam, userId: number) => {
3433

3534
return render(
3635
<Provider store={store}>
37-
<TeamMember teamMember={teamMember} />
36+
<TeamMember teamMember={teamMemberData} />
3837
</Provider>,
3938
);
4039
};
@@ -45,7 +44,7 @@ describe("Team Member Component", () => {
4544
});
4645

4746
it("renders individual team data", () => {
48-
const teamMember = renderWithStore(teamMemberData, 20);
47+
const teamMember = renderWithStore(20);
4948

5049
const name = teamMember.getByLabelText("Name");
5150
const discordID = teamMember.getByLabelText("Discord ID");
@@ -61,7 +60,7 @@ describe("Team Member Component", () => {
6160
});
6261

6362
it("current user CANNOT input other id's average hour per sprint", () => {
64-
const screen = renderWithStore(teamMemberData, 30);
63+
const screen = renderWithStore(30);
6564

6665
screen.debug();
6766

@@ -71,16 +70,4 @@ describe("Team Member Component", () => {
7170

7271
expect(buttonAction).not.toBeInTheDocument();
7372
});
74-
75-
it("current user can input their own id's average hour per sprint", () => {
76-
const screen = renderWithStore(teamMemberData, 20);
77-
78-
screen.debug();
79-
80-
const buttonAction = screen.queryByRole("button", {
81-
name: "average hour per sprint",
82-
});
83-
84-
expect(buttonAction).toBeInTheDocument();
85-
});
8673
});

0 commit comments

Comments
 (0)