Debian Codegraph @

install/freeipa-webui/src/components/layouts/SettingsTableLayout/SettingsTableLayout.test.tsx

raw
1 import React from "react";
2 import { render, screen, cleanup, fireEvent } from "@testing-library/react";
3 import { expect, it, describe, vi, afterEach } from "vitest";
4 import { Tr, Th, Td } from "@patternfly/react-table";
5 // Component
6 import SettingsTableLayout from "./SettingsTableLayout";
7
8 describe("SettingsTableLayout Component", () => {
9 const mockOnAddModal = vi.fn();
10 const mockOnDeleteModal = vi.fn();
11 const mockUpdatePage = vi.fn();
12 const mockUpdatePerPage = vi.fn();
13 const mockOnSearchChange = vi.fn();
14
15 const paginationData = {
16 page: 1,
17 perPage: 10,
18 updatePage: mockUpdatePage,
19 updatePerPage: mockUpdatePerPage,
20 updateSelectedPerPage: vi.fn(),
21 updateShownElementsList: vi.fn(),
22 totalCount: 25,
23 };
24
25 const mockList = ["item1", "item2", "item3"];
26
27 const testTableHeader = (
28 <Tr>
29 <Th>Test Header</Th>
30 </Tr>
31 );
32
33 const testTableBody = [
34 <Tr key="1">
35 <Td>Test Table Content</Td>
36 </Tr>,
37 ];
38
39 const defaultProps = {
40 ariaLabel: "Settings Table",
41 variant: "compact" as const,
42 hasBorders: true,
43 tableId: "settings-table",
44 isStickyHeader: true,
45 tableBody: testTableBody,
46 onAddModal: mockOnAddModal,
47 onDeleteModal: mockOnDeleteModal,
48 onSearchChange: mockOnSearchChange,
49 tableHeader: testTableHeader,
50 paginationData: paginationData,
51 tableClasses: "test-table-class",
52 list: mockList,
53 entryType: "User",
54 };
55
56 afterEach(() => {
57 vi.clearAllMocks();
58 cleanup();
59 });
60
61 it("should render SettingsTableLayout with table when length of list > 0", () => {
62 render(<SettingsTableLayout {...defaultProps} />);
63
64 // Check table is rendered
65 expect(screen.getByLabelText("Settings Table")).toBeInTheDocument();
66 expect(screen.getByText("Test Table Content")).toBeInTheDocument();
67 expect(screen.getByText("Test Header")).toBeInTheDocument();
68
69 // Check if buttons are rendered
70 expect(screen.getByRole("button", { name: /add/i })).toBeInTheDocument();
71 expect(screen.getByRole("button", { name: /add/i })).toHaveAttribute(
72 "id",
73 "add-user"
74 );
75 expect(screen.getByRole("button", { name: /delete/i })).toBeInTheDocument();
76
77 // Check pagination is rendered
78 expect(
79 screen.getByRole("navigation", { name: /pagination/i })
80 ).toBeInTheDocument();
81 });
82
83 it("should render EmptyState when length of list == 0", () => {
84 const emptyProps = {
85 ...defaultProps,
86 list: [],
87 paginationData: { ...paginationData, totalCount: 0 },
88 };
89
90 render(<SettingsTableLayout {...emptyProps} />);
91
92 // Check EmptyState is rendered
93 expect(screen.getByText("No users")).toBeInTheDocument();
94 expect(
95 screen.getByRole("button", { name: /Add users/i })
96 ).toBeInTheDocument();
97 });
98
99 it("should disable Add button in EmptyState when isAddDisabled is true", () => {
100 const emptyProps = {
101 ...defaultProps,
102 list: [],
103 isAddDisabled: true,
104 paginationData: { ...paginationData, totalCount: 0 },
105 };
106
107 render(<SettingsTableLayout {...emptyProps} />);
108
109 const addButton = screen.getByRole("button", { name: /Add users/i });
110 expect(addButton).toBeDisabled();
111 });
112
113 it("should call onAddModal when Add button is clicked", () => {
114 render(<SettingsTableLayout {...defaultProps} />);
115
116 const addButton = screen.getByRole("button", { name: /add/i });
117 fireEvent.click(addButton);
118 expect(mockOnAddModal).toHaveBeenCalled();
119 });
120
121 it("should call onDeleteModal when Delete button is clicked", () => {
122 render(<SettingsTableLayout {...defaultProps} />);
123
124 const deleteButton = screen.getByRole("button", { name: /delete/i });
125 fireEvent.click(deleteButton);
126 expect(mockOnDeleteModal).toHaveBeenCalled();
127 });
128
129 it("should search for an item in the list", () => {
130 render(<SettingsTableLayout {...defaultProps} />);
131
132 const searchInput = screen.getByPlaceholderText(/Filter by .../i);
133 fireEvent.change(searchInput, { target: { value: "item1" } });
134 expect(mockOnSearchChange).toHaveBeenCalledWith("item1");
135 });
136
137 it("should clear the search input when clear button is clicked", () => {
138 render(<SettingsTableLayout {...defaultProps} />);
139
140 const searchInput = screen.getByPlaceholderText(/Filter by .../i);
141 fireEvent.change(searchInput, { target: { value: "item1" } });
142 fireEvent.change(searchInput, { target: { value: "" } });
143 expect(mockOnSearchChange).toHaveBeenCalledWith("");
144 });
145
146 it("should generate correct button ID when extraID is provided", () => {
147 const propsWithExtraId = {
148 ...defaultProps,
149 extraID: "keytab",
150 entryType: "User",
151 };
152
153 render(<SettingsTableLayout {...propsWithExtraId} />);
154
155 // Check that the Add button has the correct ID with extraID
156 const addButton = screen.getByRole("button", { name: /add/i });
157 expect(addButton).toHaveAttribute("id", "add-keytab-user");
158 });
159
160 it("should handle entryType with spaces in extraID", () => {
161 const propsWithSpaces = {
162 ...defaultProps,
163 extraID: "host-group",
164 entryType: "Host Group",
165 };
166
167 render(<SettingsTableLayout {...propsWithSpaces} />);
168
169 const addButton = screen.getByRole("button", { name: /add/i });
170 expect(addButton).toHaveAttribute("id", "add-host-group-host-group");
171 });
172 });