|
| 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 | +}); |
0 commit comments