-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterButtons.test.js
42 lines (34 loc) · 1.91 KB
/
FilterButtons.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import FilterButtons from '../components/news/FilterButtons';
describe("FilterButtons", () => {
const categories = ["Category1", "Category2", "Category3"];
const setSelectedCategory = jest.fn();
const selectedCategory = "Category1";
it("renders without crashing", () => {
render(<FilterButtons categories={categories} setSelectedCategory={setSelectedCategory} selectedCategory={selectedCategory} />);
expect(screen.getByText("Category1")).toBeTruthy();
});
it("renders all categories", () => {
render(<FilterButtons categories={categories} setSelectedCategory={setSelectedCategory} selectedCategory={selectedCategory} />);
categories.forEach(category => {
expect(screen.getByText(category)).toBeTruthy();
});
});
it("calls setSelectedCategory when a button is clicked", () => {
render(<FilterButtons categories={categories} setSelectedCategory={setSelectedCategory} selectedCategory={selectedCategory} />);
fireEvent.click(screen.getByText("Category2"));
expect(setSelectedCategory).toHaveBeenCalledWith("Category2");
});
it("applies selected class to the selected category", () => {
render(<FilterButtons categories={categories} setSelectedCategory={setSelectedCategory} selectedCategory={selectedCategory} />);
const selectedButton = screen.getByText("Category1");
expect(selectedButton).toHaveClass("selected");
});
it("does not apply selected class to non-selected categories", () => {
render(<FilterButtons categories={categories} setSelectedCategory={setSelectedCategory} selectedCategory={selectedCategory} />);
const nonSelectedButton = screen.getByText("Category2");
expect(nonSelectedButton).not.toHaveClass("selected");
});
});