|
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 3 | +import { TagList } from "./TagList"; |
| 4 | + |
| 5 | +export interface Tag { |
| 6 | + id: string; |
| 7 | + label: string; |
| 8 | + lucideIcon: string; |
| 9 | + borderColor: string; |
| 10 | + backgroundColor: string; |
| 11 | +} |
| 12 | + |
| 13 | +const mockTagLabel = "Atom"; |
| 14 | +const secondTagLabel = "Beta"; |
| 15 | +const mockTag = { |
| 16 | + id: "atom", |
| 17 | + label: mockTagLabel, |
| 18 | + lucideIcon: "", |
| 19 | + borderColor: "red", |
| 20 | + backgroundColor: "white", |
| 21 | +}; |
| 22 | +const secondTag = { |
| 23 | + id: "beta", |
| 24 | + label: secondTagLabel, |
| 25 | + lucideIcon: "", |
| 26 | + borderColor: "red", |
| 27 | + backgroundColor: "white", |
| 28 | +}; |
| 29 | + |
| 30 | +describe("TagList", () => { |
| 31 | + it("hides tags", () => { |
| 32 | + render( |
| 33 | + <TagList tags={[mockTag]} onTagClick={() => {}} selectedTagIds={[]} />, |
| 34 | + ); |
| 35 | + expect(screen.getByText(mockTagLabel)).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + it("test click event", () => { |
| 38 | + const mockClick = jest.fn(); |
| 39 | + render( |
| 40 | + <TagList tags={[mockTag]} onTagClick={mockClick} selectedTagIds={[]} />, |
| 41 | + ); |
| 42 | + const tag = screen.getByText("Atom"); |
| 43 | + fireEvent.click(tag); |
| 44 | + expect(mockClick).toHaveBeenCalledWith(mockTag); |
| 45 | + expect(mockClick).toHaveBeenCalledTimes(1); |
| 46 | + }); |
| 47 | + it("will hide 13th element after clicking show less", () => { |
| 48 | + const mockClick = jest.fn(); |
| 49 | + const tags = Array(13).fill(mockTag); |
| 50 | + tags.push(secondTag); |
| 51 | + render(<TagList tags={tags} onTagClick={mockClick} selectedTagIds={[]} />); |
| 52 | + const showLessButton = screen.getByText("Show Less"); |
| 53 | + fireEvent.click(showLessButton); |
| 54 | + const tag = screen.queryByText(secondTagLabel); |
| 55 | + expect(tag).toBeNull(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments