|
| 1 | +import { screen, within, waitFor } from "@testing-library/react"; |
| 2 | +import AuditLogs from "./page"; |
| 3 | +import { renderWithUser, RootProviderMock } from "@/app/tests/unit/setup"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | + |
| 6 | +const TEST_NAME = "Rocky Balboa"; |
| 7 | +const TEST_REPORT = "Created Report"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Creates an enhanced user event with custom helper methods. |
| 11 | + * @param user - The user event from renderWithUser |
| 12 | + * @returns - The enhanced user event object with custom helpers |
| 13 | + */ |
| 14 | +const createUserWithHelpers = (user: ReturnType<typeof userEvent.setup>) => ({ |
| 15 | + ...user, |
| 16 | + |
| 17 | + /** |
| 18 | + * Selects an option in a dropdown. |
| 19 | + * @param label - The label of the dropdown |
| 20 | + * @param value - The value of the option to select |
| 21 | + */ |
| 22 | + async selectDropdownOption(label: string, value: string) { |
| 23 | + const select = screen.getByLabelText(label); |
| 24 | + await user.selectOptions(select, value); |
| 25 | + }, |
| 26 | + |
| 27 | + /** |
| 28 | + * Inputs text in a field. |
| 29 | + * @param placeholder - The placeholder of the input field |
| 30 | + * @param text - The text to input |
| 31 | + */ |
| 32 | + async typeInField(placeholder: string, text: string) { |
| 33 | + const input = screen.getByPlaceholderText(placeholder); |
| 34 | + await user.type(input, text); |
| 35 | + }, |
| 36 | +}); |
| 37 | + |
| 38 | +describe("AuditLogs Component", () => { |
| 39 | + let user: ReturnType<typeof createUserWithHelpers>; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + const renderResult = renderWithUser( |
| 43 | + <RootProviderMock currentPage="/auditLogs"> |
| 44 | + <AuditLogs /> |
| 45 | + </RootProviderMock>, |
| 46 | + ); |
| 47 | + user = createUserWithHelpers(renderResult.user); |
| 48 | + }); |
| 49 | + |
| 50 | + test("renders the audit logs table", async () => { |
| 51 | + await waitFor(() => expect(screen.getByRole("table")).toBeInTheDocument()); |
| 52 | + }); |
| 53 | + |
| 54 | + test("filters by name and clears filter", async () => { |
| 55 | + await user.selectDropdownOption("Name(s)", TEST_NAME); |
| 56 | + |
| 57 | + const rows = await screen.findAllByRole("row"); |
| 58 | + expect(rows.length).toBeGreaterThan(1); |
| 59 | + rows.slice(1).forEach((row) => { |
| 60 | + expect(within(row).getByText(TEST_NAME)).toBeInTheDocument(); |
| 61 | + }); |
| 62 | + |
| 63 | + await user.selectDropdownOption("Name(s)", ""); |
| 64 | + }); |
| 65 | + |
| 66 | + test("filters by action and clears filter", async () => { |
| 67 | + await user.selectDropdownOption("Action(s)", TEST_REPORT); |
| 68 | + |
| 69 | + const rows = await screen.findAllByRole("row"); |
| 70 | + expect(rows.length).toBeGreaterThan(1); |
| 71 | + rows.slice(1).forEach((row) => { |
| 72 | + expect(within(row).getByText(TEST_REPORT)).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + await user.selectDropdownOption("Action(s)", ""); |
| 76 | + }); |
| 77 | + |
| 78 | + test("filters by partial search", async () => { |
| 79 | + await user.typeInField("Search name or action", "Apollo"); |
| 80 | + |
| 81 | + const rows = await screen.findAllByRole("row"); |
| 82 | + expect(rows.length).toBeGreaterThan(1); |
| 83 | + rows.slice(1).forEach((row) => { |
| 84 | + expect(within(row).getByText(/Apollo/i)).toBeInTheDocument(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + test("pagination updates displayed logs", async () => { |
| 89 | + const nextButton = screen.getByLabelText("Next page"); |
| 90 | + expect(nextButton).toBeInTheDocument(); |
| 91 | + await user.click(nextButton); |
| 92 | + |
| 93 | + await waitFor(() => { |
| 94 | + const pageNumbers = screen.getAllByRole("button", { name: /Page/ }); |
| 95 | + expect(pageNumbers[1]).toHaveAttribute("aria-current", "page"); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + test("pagination allows clicking previous only if it exists", async () => { |
| 100 | + const nextButton = screen.getByLabelText("Next page"); |
| 101 | + expect(nextButton).toBeInTheDocument(); |
| 102 | + await user.click(nextButton); |
| 103 | + |
| 104 | + // Ensure previous button appears after going to the second page |
| 105 | + await waitFor(async () => { |
| 106 | + const prevButton = screen.getByLabelText("Previous page"); |
| 107 | + expect(prevButton).toBeInTheDocument(); |
| 108 | + await user.click(prevButton); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + test("pagination allows clicking a specific page number", async () => { |
| 113 | + const pageTwoButton = screen.getByRole("button", { name: "Page 2" }); |
| 114 | + |
| 115 | + await user.click(pageTwoButton); |
| 116 | + |
| 117 | + await waitFor(() => { |
| 118 | + expect(pageTwoButton).toHaveAttribute("aria-current", "page"); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + test("changing actions per page updates table", async () => { |
| 123 | + await user.selectDropdownOption("Actions per page", "25"); |
| 124 | + |
| 125 | + await waitFor(() => { |
| 126 | + const rows = screen.getAllByRole("row"); |
| 127 | + expect(rows.length).toBe(26); // 25 logs + 1 header row |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + test("clear filters resets empty state", async () => { |
| 132 | + await user.selectDropdownOption("Name(s)", "Apollo Creed"); |
| 133 | + await user.selectDropdownOption("Action(s)", "Created Report"); |
| 134 | + |
| 135 | + await waitFor(() => { |
| 136 | + expect( |
| 137 | + screen.queryByRole("button", { name: "Clear filters" }), |
| 138 | + ).toBeInTheDocument(); |
| 139 | + }); |
| 140 | + |
| 141 | + const clearFiltersButton = screen.getByRole("button", { |
| 142 | + name: "Clear filters", |
| 143 | + }); |
| 144 | + await user.click(clearFiltersButton); |
| 145 | + |
| 146 | + await waitFor(() => { |
| 147 | + expect(screen.getByRole("table")).toBeInTheDocument(); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments