Skip to content

Commit b5cf966

Browse files
committed
Added unit tests to test the tags
1 parent 7b97a00 commit b5cf966

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
});

src/features/Search/TagList/TagList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export function TagList({ tags, selectedTagIds, onTagClick }: TagListProps) {
6969
return (
7070
<div className={styles.tagContainer}>
7171
<div className={styles.tagList}>
72-
{visibleTags.map((tag) => (
72+
{visibleTags.map((tag, i) => (
7373
<TagBadge
74-
key={tag.id}
74+
key={`${tag.id}-${i}`}
7575
label={tag.label}
7676
borderColor={tag.borderColor}
7777
backgroundColor={tag.backgroundColor}

0 commit comments

Comments
 (0)