|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import React, { act } from "react"; |
9 | | -import { render, screen, waitFor } from "jest-matrix-react"; |
| 9 | +import { render, screen } from "jest-matrix-react"; |
10 | 10 | import userEvent from "@testing-library/user-event"; |
11 | 11 |
|
12 | 12 | import { type RoomListViewState } from "../../../../../../src/components/viewmodels/roomlist/RoomListViewModel"; |
13 | 13 | import { RoomListPrimaryFilters } from "../../../../../../src/components/views/rooms/RoomListPanel/RoomListPrimaryFilters"; |
14 | 14 | import { FilterKey } from "../../../../../../src/stores/room-list-v3/skip-list/filters"; |
15 | 15 |
|
16 | | -// Mock the useIsNodeVisible hook |
17 | | -jest.mock("../../../../../../src/hooks/useIsNodeVisible", () => ({ |
18 | | - useIsNodeVisible: jest.fn().mockImplementation(() => ({ |
19 | | - isVisible: true, |
20 | | - nodeRef: jest.fn(), |
21 | | - rootRef: jest.fn(), |
22 | | - })), |
23 | | -})); |
24 | | - |
25 | | -jest.mock("../../../../../../src/hooks/left-panel/room-list/useFilterHeight", () => ({ |
26 | | - useFilterHeight: jest.fn().mockImplementation(() => ({ |
27 | | - filterHeight: 30, |
28 | | - })), |
29 | | -})); |
30 | | - |
31 | 16 | describe("<RoomListPrimaryFilters />", () => { |
32 | 17 | let vm: RoomListViewState; |
33 | 18 | const filterToggleMocks = [jest.fn(), jest.fn(), jest.fn()]; |
@@ -84,89 +69,57 @@ describe("<RoomListPrimaryFilters />", () => { |
84 | 69 | expect(filterToggleMocks[0]).toHaveBeenCalledTimes(1); |
85 | 70 | }); |
86 | 71 |
|
87 | | - it("should show chevron and expands filters when there's overflow", async () => { |
88 | | - const user = userEvent.setup(); |
89 | | - render(<RoomListPrimaryFilters vm={vm} />); |
90 | | - |
91 | | - expect(screen.getByTestId("filter-container")).toHaveAttribute("data-expanded", "false"); |
92 | | - |
93 | | - // Force the overflow state by simulating ResizeObserver callback |
94 | | - // Mock that the scrollHeight is greater than FILTER_HEIGHT (30) |
95 | | - jest.spyOn(screen.getByRole("listbox", { name: "Room list filters" }), "scrollHeight", "get").mockReturnValue( |
96 | | - 50, |
97 | | - ); |
98 | | - // @ts-ignore |
99 | | - act(() => resizeCallback()); |
100 | | - |
101 | | - await waitFor(async () => { |
102 | | - const chevronButton = screen.getByRole("button", { name: "Expand filter list" }); |
103 | | - await user.click(chevronButton); |
104 | | - }); |
| 72 | + function mockFiltersOffsetLeft() { |
| 73 | + jest.spyOn(screen.getByRole("option", { name: "People" }), "offsetLeft", "get").mockReturnValue(0); |
| 74 | + jest.spyOn(screen.getByRole("option", { name: "Rooms" }), "offsetLeft", "get").mockReturnValue(30); |
| 75 | + // Unreads is wrapping |
| 76 | + jest.spyOn(screen.getByRole("option", { name: "Unreads" }), "offsetLeft", "get").mockReturnValue(0); |
| 77 | + } |
105 | 78 |
|
106 | | - // Check that the container has the expanded attribute |
107 | | - await waitFor(() => { |
108 | | - expect(screen.getByTestId("filter-container")).toHaveAttribute("data-expanded", "true"); |
109 | | - expect(screen.getByRole("button", { name: "Collapse filter list" })).toBeInTheDocument(); |
110 | | - }); |
111 | | - }); |
112 | | - |
113 | | - it("should not show the chevron when the list is equal at FILTER_HEIGHT", async () => { |
| 79 | + it("should hide or display filters if they are wrapping", async () => { |
| 80 | + const user = userEvent.setup(); |
114 | 81 | render(<RoomListPrimaryFilters vm={vm} />); |
115 | 82 |
|
116 | | - // Force the overflow state by simulating ResizeObserver callback |
117 | | - // Mock that the scrollHeight is greater than FILTER_HEIGHT (30) |
118 | | - jest.spyOn(screen.getByRole("listbox", { name: "Room list filters" }), "scrollHeight", "get").mockReturnValue( |
119 | | - 30, |
120 | | - ); |
121 | | - |
122 | | - // @ts-ignore |
123 | | - act(() => resizeCallback()); |
124 | | - |
| 83 | + // No filter is wrapping, so chevron shouldn't be visible |
125 | 84 | expect(screen.queryByRole("button", { name: "Expand filter list" })).toBeNull(); |
126 | | - }); |
127 | | - |
128 | | - it("handles filter reordering when active filter is not visible", async () => { |
129 | | - // Mock useIsNodeVisible to return false, indicating active filter is not visible |
130 | | - jest.requireMock("../../../../../../src/hooks/useIsNodeVisible").useIsNodeVisible.mockImplementation(() => ({ |
131 | | - isVisible: false, |
132 | | - nodeRef: jest.fn(), |
133 | | - rootRef: jest.fn(), |
134 | | - })); |
135 | | - |
136 | | - render(<RoomListPrimaryFilters vm={vm} />); |
| 85 | + expect(screen.queryByRole("option", { name: "Unreads" })).toBeVisible(); |
137 | 86 |
|
138 | | - // Check that filters are reordered with the active one first |
139 | | - const options = screen.getAllByRole("option"); |
140 | | - expect(options[0]).toHaveAttribute("aria-selected", "true"); |
141 | | - expect(options[0].textContent).toBe("Rooms"); |
| 87 | + mockFiltersOffsetLeft(); |
| 88 | + // @ts-ignore |
| 89 | + act(() => resizeCallback([{ target: screen.getByRole("listbox", { name: "Room list filters" }) }])); |
| 90 | + |
| 91 | + // The Unreads filter is wrapping, it should not be visible |
| 92 | + expect(screen.queryByRole("option", { name: "Unreads" })).toBeNull(); |
| 93 | + // Now filters are wrapping, so chevron should be visible |
| 94 | + await user.click(screen.getByRole("button", { name: "Expand filter list" })); |
| 95 | + // The list is expanded, so Unreads should be visible |
| 96 | + expect(screen.getByRole("option", { name: "Unreads" })).toBeVisible(); |
142 | 97 | }); |
143 | 98 |
|
144 | | - it("resets filter ordering when a filter is selected", async () => { |
145 | | - // Setup: First mock it as not visible so it reorders |
146 | | - jest.requireMock("../../../../../../src/hooks/useIsNodeVisible").useIsNodeVisible.mockImplementation(() => ({ |
147 | | - isVisible: false, |
148 | | - nodeRef: jest.fn(), |
149 | | - rootRef: jest.fn(), |
150 | | - })); |
| 99 | + it("should move the active filter if the list is collapsed and the filter is wrapping", async () => { |
| 100 | + vm = { |
| 101 | + primaryFilters: [ |
| 102 | + { name: "People", active: false, toggle: filterToggleMocks[0], key: FilterKey.PeopleFilter }, |
| 103 | + { name: "Rooms", active: false, toggle: filterToggleMocks[1], key: FilterKey.RoomsFilter }, |
| 104 | + { name: "Unreads", active: true, toggle: filterToggleMocks[2], key: FilterKey.UnreadFilter }, |
| 105 | + ], |
| 106 | + } as unknown as RoomListViewState; |
151 | 107 |
|
152 | 108 | const user = userEvent.setup(); |
153 | 109 | render(<RoomListPrimaryFilters vm={vm} />); |
| 110 | + mockFiltersOffsetLeft(); |
| 111 | + // @ts-ignore |
| 112 | + act(() => resizeCallback([{ target: screen.getByRole("listbox", { name: "Room list filters" }) }])); |
154 | 113 |
|
155 | | - // Check initial order (reordered with active first) |
156 | | - let options = screen.getAllByRole("option"); |
157 | | - expect(options[0].textContent).toBe("Rooms"); |
158 | | - |
159 | | - // Now change the hook to return true and click a filter |
160 | | - jest.requireMock("../../../../../../src/hooks/useIsNodeVisible").useIsNodeVisible.mockImplementation(() => ({ |
161 | | - isVisible: true, |
162 | | - nodeRef: jest.fn(), |
163 | | - rootRef: jest.fn(), |
164 | | - })); |
165 | | - |
166 | | - await user.click(screen.getByRole("button", { name: "People" })); |
| 114 | + // Unread filter should be moved to the first position |
| 115 | + expect(screen.getByRole("listbox", { name: "Room list filters" }).children[0]).toBe( |
| 116 | + screen.getByRole("option", { name: "Unreads" }), |
| 117 | + ); |
167 | 118 |
|
168 | | - // The order should be reset to original order |
169 | | - options = screen.getAllByRole("option"); |
170 | | - expect(options[0].textContent).toBe("People"); |
| 119 | + // When the list is expanded, the Unreads filter should move to its original position |
| 120 | + await user.click(screen.getByRole("button", { name: "Expand filter list" })); |
| 121 | + expect(screen.getByRole("listbox", { name: "Room list filters" }).children[0]).not.toEqual( |
| 122 | + screen.getByRole("option", { name: "Unreads" }), |
| 123 | + ); |
171 | 124 | }); |
172 | 125 | }); |
0 commit comments