// @vitest-environment jsdom import { cleanup, fireEvent, render, screen } from '@testing-library/preact' import { useState } from 'preact/hooks' import { act } from 'preact/test-utils' import { stockFeatures } from '@tanstack/table-core' import { afterEach, describe, expect, test, vi } from 'vitest' import { FlexRender, useTable } from '../../src' import type { ColumnDef } from '@tanstack/table-core' afterEach(() => { cleanup() vi.restoreAllMocks() }) function outputText(name: string) { return screen.getByRole('status', { name }).textContent } describe('FlexRender', () => { type Data = { id: string; name: string } type CellMode = 'aggregate' | 'normal' | 'placeholder' const columns: Array> = [ { id: 'name', accessorKey: 'name', header: ({ column }) => {`header:${column.id}`}, cell: ({ getValue }) => {`cell:${String(getValue())}`}, aggregatedCell: ({ getValue }) => ( {`aggregate:${String(getValue())}`} ), footer: ({ column }) => {`footer:${column.id}`}, }, ] function CellHarness({ mode }: { mode: CellMode }) { const table = useTable({ data: [{ id: '1', name: 'Ada' }], columns, features: stockFeatures, getRowId: (row) => row.id, }) const cell = table.getRowModel().rows[0]!.getAllCells()[0]! if (mode === 'aggregate') { vi.spyOn(cell, 'getIsAggregated').mockReturnValue(true) } if (mode === 'placeholder') { vi.spyOn(cell, 'getIsAggregated').mockReturnValue(false) vi.spyOn(cell, 'getIsPlaceholder').mockReturnValue(true) } return ( ) } function HeaderFooterHarness() { const table = useTable({ data: [{ id: '1', name: 'Ada' }], columns, features: stockFeatures, getRowId: (row) => row.id, }) const header = table.getHeaderGroups()[0]!.headers[0]! const footer = table.getFooterGroups()[0]!.headers[0]! return ( <> ) } test('renders normal, aggregate, placeholder, header, and footer content', () => { render( <> , ) expect(outputText('normal cell')).toBe('cell:Ada') expect(outputText('aggregate cell')).toBe('aggregate:Ada') expect(outputText('placeholder cell')).toBe('') expect(outputText('Rendered header')).toBe('header:name') expect(outputText('Rendered footer')).toBe('footer:name') }) test('reacts when a stable cell changes grouping mode', () => { function GroupingCellHarness() { const [mode, setMode] = useState('normal') const table = useTable({ data: [{ id: '1', name: 'Ada' }], columns, features: stockFeatures, getRowId: (row) => row.id, }) const cell = table.getRowModel().rows[0]!.getAllCells()[0]! vi.spyOn(cell, 'getIsAggregated').mockImplementation( () => mode === 'aggregate', ) vi.spyOn(cell, 'getIsPlaceholder').mockImplementation( () => mode === 'placeholder', ) return ( <> ) } render() expect(outputText('Grouping cell')).toBe('cell:Ada') act(() => { fireEvent.click(screen.getByRole('button', { name: 'Show aggregate' })) }) expect(outputText('Grouping cell')).toBe('aggregate:Ada') act(() => { fireEvent.click(screen.getByRole('button', { name: 'Show placeholder' })) }) expect(outputText('Grouping cell')).toBe('') act(() => { fireEvent.click(screen.getByRole('button', { name: 'Show normal' })) }) expect(outputText('Grouping cell')).toBe('cell:Ada') }) test('updates when the cell prop is replaced with a new instance', () => { function CellReplacementHarness() { const [data, setData] = useState>([{ id: '1', name: 'Ada' }]) const table = useTable({ data, columns, features: stockFeatures, getRowId: (row) => row.id, }) const cell = table.getRowModel().rows[0]!.getAllCells()[0]! return ( <> ) } render() expect(outputText('Replaced cell')).toBe('cell:Ada') act(() => { fireEvent.click(screen.getByRole('button', { name: 'Replace cell' })) }) expect(outputText('Replaced cell')).toBe('cell:Grace') }) }) describe('table.Subscribe', () => { test('updates mounted content for the selected atom slice', () => { function SubscribeHarness() { const table = useTable( { data: [{ id: '1' }], columns: [{ id: 'id', accessorKey: 'id' }], features: stockFeatures, getRowId: (row) => row.id, }, () => null, ) return ( <> Boolean(selection['1'])} > {(selected) => ( {String(selected)} )} ) } render() expect(outputText('Subscribed selection')).toBe('false') act(() => { fireEvent.click( screen.getByRole('button', { name: 'Select subscribed row' }), ) }) expect(outputText('Subscribed selection')).toBe('true') }) })