|
1 | | -import { describe, test } from "vitest"; |
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { useLayoutEffect } from "react"; |
| 3 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 4 | +import { EMPTY_OBJECT } from "../../../src/constants"; |
| 5 | +import { updateMockResizeObserver } from "../../utils/test/mockResizeObserver"; |
| 6 | +import { Grid } from "./Grid"; |
| 7 | +import type { CellComponentProps } from "./types"; |
2 | 8 |
|
3 | 9 | describe("Grid", () => { |
4 | | - test("have tests", () => { |
5 | | - // TODO Write tests |
| 10 | + let mountedCells: Map<string, CellComponentProps<object>> = new Map(); |
| 11 | + |
| 12 | + const CellComponent = vi.fn(function Cell(props: CellComponentProps<object>) { |
| 13 | + const { columnIndex, rowIndex, style } = props; |
| 14 | + |
| 15 | + const key = `${rowIndex},${columnIndex}`; |
| 16 | + |
| 17 | + useLayoutEffect(() => { |
| 18 | + mountedCells.set(key, props); |
| 19 | + return () => { |
| 20 | + mountedCells.delete(key); |
| 21 | + }; |
| 22 | + }); |
| 23 | + |
| 24 | + return ( |
| 25 | + <div role="listitem" style={style}> |
| 26 | + Cell {key} |
| 27 | + </div> |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + CellComponent.mockReset(); |
| 33 | + |
| 34 | + updateMockResizeObserver(new DOMRect(0, 0, 100, 40)); |
| 35 | + |
| 36 | + mountedCells = new Map(); |
| 37 | + }); |
| 38 | + |
| 39 | + test("should render an empty Grid", () => { |
| 40 | + render( |
| 41 | + <Grid |
| 42 | + cellComponent={CellComponent} |
| 43 | + cellProps={EMPTY_OBJECT} |
| 44 | + columnCount={0} |
| 45 | + columnWidth={25} |
| 46 | + overscanCount={0} |
| 47 | + rowCount={0} |
| 48 | + rowHeight={20} |
| 49 | + /> |
| 50 | + ); |
| 51 | + |
| 52 | + const items = screen.queryAllByRole("listitem"); |
| 53 | + expect(items).toHaveLength(0); |
| 54 | + }); |
| 55 | + |
| 56 | + test("should render extra cells for overscan", () => { |
| 57 | + render( |
| 58 | + <Grid |
| 59 | + cellComponent={CellComponent} |
| 60 | + cellProps={EMPTY_OBJECT} |
| 61 | + columnCount={100} |
| 62 | + columnWidth={25} |
| 63 | + overscanCount={2} |
| 64 | + rowCount={100} |
| 65 | + rowHeight={20} |
| 66 | + /> |
| 67 | + ); |
| 68 | + |
| 69 | + // 4 columns (+2) by 2 rows (+2) |
| 70 | + const items = screen.queryAllByRole("listitem"); |
| 71 | + expect(items).toHaveLength(24); |
| 72 | + }); |
| 73 | + |
| 74 | + describe("cell sizes", () => { |
| 75 | + test("type: number (px)", () => { |
| 76 | + const { container } = render( |
| 77 | + <Grid |
| 78 | + cellComponent={CellComponent} |
| 79 | + cellProps={EMPTY_OBJECT} |
| 80 | + columnCount={100} |
| 81 | + columnWidth={25} |
| 82 | + overscanCount={0} |
| 83 | + rowCount={100} |
| 84 | + rowHeight={20} |
| 85 | + /> |
| 86 | + ); |
| 87 | + |
| 88 | + // 4 columns by 2 rows |
| 89 | + expect(container.querySelectorAll('[role="listitem"]')).toHaveLength(8); |
| 90 | + }); |
| 91 | + |
| 92 | + test("type: function (px)", () => { |
| 93 | + const columnWidth = () => 50; |
| 94 | + const rowHeight = () => 20; |
| 95 | + |
| 96 | + const { container } = render( |
| 97 | + <Grid |
| 98 | + cellComponent={CellComponent} |
| 99 | + cellProps={EMPTY_OBJECT} |
| 100 | + columnCount={100} |
| 101 | + columnWidth={columnWidth} |
| 102 | + overscanCount={0} |
| 103 | + rowCount={100} |
| 104 | + rowHeight={rowHeight} |
| 105 | + /> |
| 106 | + ); |
| 107 | + |
| 108 | + // 2 columns by 2 rows |
| 109 | + expect(container.querySelectorAll('[role="listitem"]')).toHaveLength(4); |
| 110 | + }); |
| 111 | + |
| 112 | + test("type: string (%)", () => { |
| 113 | + const { container } = render( |
| 114 | + <Grid |
| 115 | + cellComponent={CellComponent} |
| 116 | + cellProps={EMPTY_OBJECT} |
| 117 | + columnCount={100} |
| 118 | + columnWidth="25%" |
| 119 | + overscanCount={0} |
| 120 | + rowCount={100} |
| 121 | + rowHeight="25%" |
| 122 | + /> |
| 123 | + ); |
| 124 | + |
| 125 | + // 4 columns by 4 rows |
| 126 | + expect(container.querySelectorAll('[role="listitem"]')).toHaveLength(16); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + test.skip("should pass cellProps to the cellComponent", () => { |
| 131 | + // TODO |
| 132 | + }); |
| 133 | + |
| 134 | + test.skip("should re-render items if cellComponent changes", () => { |
| 135 | + // TODO |
| 136 | + }); |
| 137 | + |
| 138 | + test.skip("should re-render items if cell size changes", () => { |
| 139 | + // TODO |
| 140 | + }); |
| 141 | + |
| 142 | + test.skip("should re-render items if cellProps change", () => { |
| 143 | + // TODO |
| 144 | + }); |
| 145 | + |
| 146 | + test.skip("should use default sizes for initial mount", () => { |
| 147 | + // TODO |
| 148 | + }); |
| 149 | + |
| 150 | + test.skip("should call onCellsRendered", () => { |
| 151 | + // TODO |
| 152 | + }); |
| 153 | + |
| 154 | + test.skip("should support custom className and style props", () => { |
| 155 | + // TODO |
| 156 | + }); |
| 157 | + |
| 158 | + test.skip("should spread HTML rest attributes", () => { |
| 159 | + // TODO |
| 160 | + }); |
| 161 | + |
| 162 | + describe("imperative API", () => { |
| 163 | + test.skip("should return the root element", () => { |
| 164 | + // TODO |
| 165 | + }); |
| 166 | + |
| 167 | + test.skip("should scroll to cells", () => { |
| 168 | + // TODO |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + test.skip("should auto-memoize cellProps object using shallow equality", () => { |
| 173 | + // TODO |
| 174 | + }); |
| 175 | + |
| 176 | + describe("edge cases", () => { |
| 177 | + test.skip("should not cause a cycle of Grid callback ref is passed in cellProps", () => { |
| 178 | + // TODO |
| 179 | + }); |
6 | 180 | }); |
7 | 181 | }); |
0 commit comments