|
| 1 | +import React, { act } from "react"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { ChooseTeam } from "./ChooseTeam"; |
| 4 | + |
| 5 | +describe("ChooseTeam Component tests", () => { |
| 6 | + beforeEach(() => { |
| 7 | + render(<ChooseTeam />); |
| 8 | + }); |
| 9 | + test("(2 pts) The initial team is empty", () => { |
| 10 | + const currentTeam = screen.queryAllByRole("listitem"); |
| 11 | + expect(currentTeam).toHaveLength(0); |
| 12 | + }); |
| 13 | + test("(2 pts) There are 7 buttons.", () => { |
| 14 | + const adders = screen.queryAllByRole("button"); |
| 15 | + expect(adders).toHaveLength(7); |
| 16 | + }); |
| 17 | + test("(2 pts) Clicking first team member works", async () => { |
| 18 | + const first = screen.queryAllByRole("button")[0]; |
| 19 | + await act(async () => { |
| 20 | + first.click(); |
| 21 | + }); |
| 22 | + const currentTeam = screen.queryAllByRole("listitem"); |
| 23 | + expect(currentTeam).toHaveLength(1); |
| 24 | + expect(currentTeam[0].textContent).toEqual(first.textContent); |
| 25 | + }); |
| 26 | + test("(2 pts) Clicking the third team member works", async () => { |
| 27 | + const third = screen.queryAllByRole("button")[2]; |
| 28 | + await act(async () => { |
| 29 | + third.click(); |
| 30 | + }); |
| 31 | + const currentTeam = screen.queryAllByRole("listitem"); |
| 32 | + expect(currentTeam).toHaveLength(1); |
| 33 | + expect(currentTeam[0].textContent).toEqual(third.textContent); |
| 34 | + }); |
| 35 | + test("(2 pts) Clicking three team members works", async () => { |
| 36 | + const [, second, third, , fifth] = screen.queryAllByRole("button"); |
| 37 | + await act(async () => { |
| 38 | + third.click(); |
| 39 | + }); |
| 40 | + await act(async () => { |
| 41 | + second.click(); |
| 42 | + }); |
| 43 | + await act(async () => { |
| 44 | + fifth.click(); |
| 45 | + }); |
| 46 | + const currentTeam = screen.queryAllByRole("listitem"); |
| 47 | + expect(currentTeam).toHaveLength(3); |
| 48 | + expect(currentTeam[0].textContent).toEqual(third.textContent); |
| 49 | + expect(currentTeam[1].textContent).toEqual(second.textContent); |
| 50 | + expect(currentTeam[2].textContent).toEqual(fifth.textContent); |
| 51 | + }); |
| 52 | + test("(2 pts) Clearing the team works", async () => { |
| 53 | + const [, second, third, fourth, fifth, , clear] = |
| 54 | + screen.queryAllByRole("button"); |
| 55 | + await act(async () => { |
| 56 | + third.click(); |
| 57 | + }); |
| 58 | + await act(async () => { |
| 59 | + second.click(); |
| 60 | + }); |
| 61 | + await act(async () => { |
| 62 | + fifth.click(); |
| 63 | + }); |
| 64 | + let currentTeam = screen.queryAllByRole("listitem"); |
| 65 | + expect(currentTeam).toHaveLength(3); |
| 66 | + expect(currentTeam[0].textContent).toEqual(third.textContent); |
| 67 | + expect(currentTeam[1].textContent).toEqual(second.textContent); |
| 68 | + expect(currentTeam[2].textContent).toEqual(fifth.textContent); |
| 69 | + await act(async () => { |
| 70 | + clear.click(); |
| 71 | + }); |
| 72 | + currentTeam = screen.queryAllByRole("listitem"); |
| 73 | + expect(currentTeam).toHaveLength(0); |
| 74 | + await act(async () => { |
| 75 | + fourth.click(); |
| 76 | + }); |
| 77 | + currentTeam = screen.queryAllByRole("listitem"); |
| 78 | + expect(currentTeam).toHaveLength(1); |
| 79 | + expect(currentTeam[0].textContent).toEqual(fourth.textContent); |
| 80 | + }); |
| 81 | +}); |
0 commit comments