|
| 1 | +import { MutableRefObject } from "react"; |
| 2 | +import { computed, configure, observable } from "mobx"; |
| 3 | +import { SetPageAction } from "@mendix/widget-plugin-grid/pagination/main"; |
| 4 | +import { PaginationConfig } from "../../../features/pagination/pagination.config"; |
| 5 | +import { GridSizeStore, VIRTUAL_SCROLLING_OFFSET } from "../GridSize.store"; |
| 6 | + |
| 7 | +configure({ enforceActions: "never" }); |
| 8 | + |
| 9 | +function makePaginationConfig(pagination: "virtualScrolling" | "buttons" = "virtualScrolling"): PaginationConfig { |
| 10 | + return { |
| 11 | + pagination, |
| 12 | + pagingPosition: "bottom", |
| 13 | + paginationKind: "virtualScrolling.always", |
| 14 | + showPagingButtons: "always", |
| 15 | + showNumberOfRows: false, |
| 16 | + constPageSize: 10, |
| 17 | + initPageSize: 10, |
| 18 | + isLimitBased: false, |
| 19 | + dynamicPageSizeEnabled: false, |
| 20 | + dynamicPageEnabled: false, |
| 21 | + customPaginationEnabled: false, |
| 22 | + requestTotalCount: false |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +interface StoreOptions { |
| 27 | + hasMoreItems?: boolean; |
| 28 | + pageSize?: number; |
| 29 | + columnCount?: number; |
| 30 | + pagination?: "virtualScrolling" | "buttons"; |
| 31 | +} |
| 32 | + |
| 33 | +function buildStore(options: StoreOptions = {}): { |
| 34 | + store: GridSizeStore; |
| 35 | + hasMoreItemsBox: ReturnType<typeof observable.box<boolean | undefined>>; |
| 36 | + pageSizeBox: ReturnType<typeof observable.box<number>>; |
| 37 | + columnCountBox: ReturnType<typeof observable.box<number>>; |
| 38 | + setPageAction: jest.Mock; |
| 39 | +} { |
| 40 | + const { hasMoreItems = true, pageSize = 10, columnCount = 3, pagination = "virtualScrolling" } = options; |
| 41 | + |
| 42 | + const hasMoreItemsBox = observable.box<boolean | undefined>(hasMoreItems); |
| 43 | + const pageSizeBox = observable.box(pageSize); |
| 44 | + const columnCountBox = observable.box(columnCount); |
| 45 | + const setPageAction = jest.fn() as unknown as SetPageAction; |
| 46 | + |
| 47 | + const store = new GridSizeStore( |
| 48 | + computed(() => hasMoreItemsBox.get()), |
| 49 | + makePaginationConfig(pagination), |
| 50 | + setPageAction, |
| 51 | + computed(() => pageSizeBox.get()), |
| 52 | + computed(() => columnCountBox.get()) |
| 53 | + ); |
| 54 | + |
| 55 | + return { |
| 56 | + store, |
| 57 | + hasMoreItemsBox, |
| 58 | + pageSizeBox, |
| 59 | + columnCountBox, |
| 60 | + setPageAction: setPageAction as unknown as jest.Mock |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +function createMockRows(count: number, rowHeight: number): HTMLElement[] { |
| 65 | + return Array.from({ length: count }, () => { |
| 66 | + const row = document.createElement("div"); |
| 67 | + const cell = document.createElement("div"); |
| 68 | + Object.defineProperty(cell, "clientHeight", { value: rowHeight, configurable: true }); |
| 69 | + row.appendChild(cell); |
| 70 | + return row; |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +interface RefsOptions { |
| 75 | + rowHeight?: number; |
| 76 | + rowCount?: number; |
| 77 | + headerHeight?: number; |
| 78 | + containerClientHeight?: number; |
| 79 | + bodyScrollHeight?: number; |
| 80 | + bodyClientHeight?: number; |
| 81 | +} |
| 82 | + |
| 83 | +function setupRefs(store: GridSizeStore, options: RefsOptions = {}): void { |
| 84 | + const { |
| 85 | + rowHeight = 40, |
| 86 | + rowCount = 10, |
| 87 | + headerHeight = 50, |
| 88 | + containerClientHeight = 1000, |
| 89 | + bodyScrollHeight, |
| 90 | + bodyClientHeight |
| 91 | + } = options; |
| 92 | + |
| 93 | + const container = document.createElement("div"); |
| 94 | + Object.defineProperty(container, "clientHeight", { value: containerClientHeight, configurable: true }); |
| 95 | + (store.gridContainerRef as MutableRefObject<HTMLDivElement>).current = container; |
| 96 | + |
| 97 | + const body = document.createElement("div"); |
| 98 | + createMockRows(rowCount, rowHeight).forEach(r => body.appendChild(r)); |
| 99 | + Object.defineProperty(body, "scrollHeight", { |
| 100 | + value: bodyScrollHeight ?? rowHeight * rowCount, |
| 101 | + configurable: true |
| 102 | + }); |
| 103 | + Object.defineProperty(body, "clientHeight", { |
| 104 | + value: bodyClientHeight ?? containerClientHeight - headerHeight, |
| 105 | + configurable: true |
| 106 | + }); |
| 107 | + (store.gridBodyRef as MutableRefObject<HTMLDivElement>).current = body; |
| 108 | + |
| 109 | + const header = document.createElement("div"); |
| 110 | + const th = document.createElement("div"); |
| 111 | + th.className = "th"; |
| 112 | + Object.defineProperty(th, "offsetHeight", { value: headerHeight, configurable: true }); |
| 113 | + header.appendChild(th); |
| 114 | + (store.gridHeaderRef as MutableRefObject<HTMLDivElement>).current = header; |
| 115 | +} |
| 116 | + |
| 117 | +describe("GridSizeStore.lockGridContainerHeight()", () => { |
| 118 | + it("locks container height on first call when virtual scrolling is active and hasMoreItems", () => { |
| 119 | + const { store } = buildStore({ pageSize: 3, columnCount: 2 }); |
| 120 | + // bodyViewport = 3 rows × 40px = 120px, header = 50px → fullHeight = 170px |
| 121 | + // containerClientHeight (1000) >= fullHeight (170) → no overflow → subtract VIRTUAL_SCROLLING_OFFSET |
| 122 | + setupRefs(store, { rowHeight: 40, rowCount: 3, headerHeight: 50, containerClientHeight: 1000 }); |
| 123 | + |
| 124 | + store.lockGridContainerHeight(); |
| 125 | + |
| 126 | + expect(store.gridContainerHeight).toBe(170 - VIRTUAL_SCROLLING_OFFSET); |
| 127 | + }); |
| 128 | + |
| 129 | + it("is a no-op when hasVirtualScrolling is false", () => { |
| 130 | + const { store } = buildStore({ pagination: "buttons" }); |
| 131 | + setupRefs(store); |
| 132 | + |
| 133 | + store.lockGridContainerHeight(); |
| 134 | + |
| 135 | + expect(store.gridContainerHeight).toBeUndefined(); |
| 136 | + }); |
| 137 | + |
| 138 | + it("is a no-op when hasMoreItems is false", () => { |
| 139 | + const { store } = buildStore({ hasMoreItems: false }); |
| 140 | + setupRefs(store); |
| 141 | + |
| 142 | + store.lockGridContainerHeight(); |
| 143 | + |
| 144 | + expect(store.gridContainerHeight).toBeUndefined(); |
| 145 | + }); |
| 146 | + |
| 147 | + it("does not double-lock on repeated calls with same layout inputs", () => { |
| 148 | + const { store } = buildStore({ pageSize: 3, columnCount: 2 }); |
| 149 | + setupRefs(store, { rowHeight: 40, rowCount: 3, headerHeight: 50, containerClientHeight: 1000 }); |
| 150 | + |
| 151 | + store.lockGridContainerHeight(); |
| 152 | + const heightAfterFirst = store.gridContainerHeight; |
| 153 | + |
| 154 | + // Mutate refs to simulate different measurements — if re-locking, height would change |
| 155 | + setupRefs(store, { rowHeight: 99, rowCount: 3, headerHeight: 50, containerClientHeight: 1000 }); |
| 156 | + store.lockGridContainerHeight(); |
| 157 | + |
| 158 | + expect(store.gridContainerHeight).toBe(heightAfterFirst); |
| 159 | + }); |
| 160 | + |
| 161 | + it("clears and recomputes height when page size changes", () => { |
| 162 | + const { store, pageSizeBox } = buildStore({ pageSize: 3, columnCount: 2 }); |
| 163 | + setupRefs(store, { rowHeight: 40, rowCount: 3, headerHeight: 50, containerClientHeight: 1000 }); |
| 164 | + store.lockGridContainerHeight(); |
| 165 | + const heightWithPageSize3 = store.gridContainerHeight; |
| 166 | + |
| 167 | + pageSizeBox.set(5); |
| 168 | + setupRefs(store, { rowHeight: 40, rowCount: 5, headerHeight: 50, containerClientHeight: 1000 }); |
| 169 | + store.lockGridContainerHeight(); |
| 170 | + |
| 171 | + // bodyViewport = 5 × 40 = 200, header = 50 → fullHeight = 250, no overflow → subtract offset |
| 172 | + expect(store.gridContainerHeight).toBe(250 - VIRTUAL_SCROLLING_OFFSET); |
| 173 | + expect(store.gridContainerHeight).not.toBe(heightWithPageSize3); |
| 174 | + }); |
| 175 | + |
| 176 | + it("clears and recomputes height when visible column count changes", () => { |
| 177 | + const { store, columnCountBox } = buildStore({ pageSize: 3, columnCount: 2 }); |
| 178 | + setupRefs(store, { rowHeight: 40, rowCount: 3, headerHeight: 50, containerClientHeight: 1000 }); |
| 179 | + store.lockGridContainerHeight(); |
| 180 | + const heightWith2Columns = store.gridContainerHeight; |
| 181 | + |
| 182 | + columnCountBox.set(1); |
| 183 | + // Simulate rows getting shorter after hiding a column |
| 184 | + setupRefs(store, { rowHeight: 20, rowCount: 3, headerHeight: 50, containerClientHeight: 1000 }); |
| 185 | + store.lockGridContainerHeight(); |
| 186 | + |
| 187 | + // bodyViewport = 3 × 20 = 60, header = 50 → fullHeight = 110, no overflow → subtract offset |
| 188 | + expect(store.gridContainerHeight).toBe(110 - VIRTUAL_SCROLLING_OFFSET); |
| 189 | + expect(store.gridContainerHeight).not.toBe(heightWith2Columns); |
| 190 | + }); |
| 191 | +}); |
0 commit comments