preloadGridPopups and /testing
Preloading popover parts, and testing helpers for drawing the grid in jsdom
One reference for both the React and Solid versions
Lazy loading of popover parts (preloadGridPopups)
The parts of the column filter popover and the tool panel (filter manager / column visibility / sorting) are split into a chunk separate from the core and aren't in the initial load (the initial JS is about 10% smaller; props / handle are the same).
- After mounting, when the browser is idle (
requestIdleCallback; about 200ms later in environments without it), the grid preloads the parts only when that feature can be used (column filters =enableColumnFilterand there are columns withfilterType/ tool panel = any of filters, the column menu or sorting can be used). - When opened before the preload, it loads on the spot, and the parts don't appear until loading finishes (focus goes into the input once they appear. Escape closes it even while loading). The grid doesn't re-render just because a preload finished.
- When loading fails, it logs to
console.errorand closes (it tries to load again the next time it opens, but some browsers remember a failed dynamic import until the page reloads). - Assumes a bundler that supports dynamic import (
import()) (Vite / webpack / Next.js / Rollup / esbuild and so on). The CJS build loads withrequire.
import { preloadGridPopups } from '@ishibashi0112/spreadsheet-grid';| Function | Description |
|---|---|
preloadGridPopups(): Promise<void> | Loads the parts ahead of time. Usually not needed (the grid preloads automatically). Use it when a test checks the contents synchronously right after opening a popover (await it in beforeAll), or when you want them loaded before a page transition. |
Testing helpers (the /testing subpath)
jsdom doesn't compute layout, so with plain jsdom not a single row or column of a real grid is drawn (vertical: the scroll element's clientHeight / clientWidth are 0. Horizontal: column virtualization (@tanstack/virtual-core) gets its rectangle from ResizeObserver notifications, so with a no-op stub the width stays 0). installJsdomLayoutStubs() from @ishibashi0112/spreadsheet-grid/testing solves all of this at once.
// @vitest-environment jsdom
import { installJsdomLayoutStubs } from '@ishibashi0112/spreadsheet-grid/testing';
beforeAll(() => {
installJsdomLayoutStubs(); // default 1200×600. Change with { width, height }
});- What it installs:
HTMLElement.prototype.clientHeight/clientWidth(fixed getters),Element.prototype.getBoundingClientRect(a fixed rectangle),globalThis.ResizeObserver(a stub that calls back immediately on observe),Element.prototype.scrollTo(a no-op if missing). - The return value is a restore function that goes back to before installing (for
afterAll(restore); optional). - A small module independent of React, separate from the main bundle (
dist/testing.js). - When the stubs are installed twice, restore them in the reverse order of installing (restoring in the same order leaves the first stub's values).
- With this you can verify the drawing of
.ssg-body-row/.ssg-body-cell, the classes added and cell text in the DOM (the library's ownSpreadsheetGrid.jsdomLayoutStubs.integration.test.tsxguarantees it works). Virtualization still applies, so only the rows and columns that fit in the stub's size (plus overscan) are drawn (with the default 1200×600, a row height of 36px and 120px-wide columns, about 38 rows and about 18 columns; the grid'sheightdoesn't change it). To draw everything, make the stub bigger ({ width: 3000, height: 4000 }and so on), or writescrollTop/scrollLeftof.ssg-scroll-containerand firefireEvent.scroll. The handle'sscrollToRow/scrollToCelldo nothing in jsdom (the stub'sscrollTois a no-op). Rows are also drawn in pinned panes, and the row number "#" is in the rows of the left pane (since 0.63.0, even in tables without left-pinned columns), so to count rows / get cells of the n-th row, use the rows of the center pane ([data-pane="center"].ssg-body-row). - Tests that check the contents synchronously right after opening the column filter popover / tool panel find nothing there until lazy loading of the parts has finished. Load them first with
beforeAll(() => preloadGridPopups())(imported from the main entry; from/litefor/litegrids), or wait withfindBy*/waitFor(Lazy loading of popover parts). - Common operations: pasting reads the paste event's
clipboardData, so use user-event'suser.paste('a\tb')orfireEvent.paste(shell, { clipboardData: { getData: () => 'a\tb' } })(theCtrl+Vkey alone doesn't paste). Copying writes withnavigator.clipboard.writeText(afteruserEvent.setup()it can be read withnavigator.clipboard.readText()). jsdom has no coordinates, so select ranges withShift+arrows/Shift+click/selectRange. Moving the active cell after commit / cancel and accepting the next edit happen in a later microtask, so when continuing synchronously with fireEvent, putawait act(async () => {})in between. The date editor is a native<input type="date">, so usefireEvent.change.selectCell/setActiveCelldon't move focus, so callfocus()before sending keys. Focus moves into the column filter popover's input 2 frames after it opens (after its position is decided), so wait withawait waitFor(() => expect(input).toHaveFocus())(on close it returns to the grid right away; 1.0.2). - Tests that start typing a number column with digits: in jsdom / happy-dom + user-event,
user.keyboard('42')becomes "24" (jsdom doesn't keep the caret position of the<input type="number">opened by the first character, so the second character goes in at the start; "42" in real browsers). Open it with the first character and continue into the input withawait user.type(input, '2'), or open withEnter/F2and writeawait user.clear(input)+await user.type(input, '42'). - Jest: there is a CommonJS entry, so it loads as is. Map CSS imports with
moduleNameMapper: { '\\.css$': 'identity-obj-proxy' }or similar. Jest 30's jsdom (26) has noPointerEvent, andfireEventalone doesn't select cells, so write cell presses with user-event, or with the handle'sselectCell+focus().