|
| 1 | +/** |
| 2 | + * Data Table - Custom Cell Renderer Tests |
| 3 | + * |
| 4 | + * Tests that data-table respects col.cell() function for custom cell rendering. |
| 5 | + */ |
| 6 | +import { describe, it, expect, vi } from 'vitest'; |
| 7 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 8 | +import '@testing-library/jest-dom'; |
| 9 | +import React from 'react'; |
| 10 | +import { SchemaRenderer } from '@object-ui/react'; |
| 11 | + |
| 12 | +// Import data-table to ensure it's registered |
| 13 | +import '../data-table'; |
| 14 | + |
| 15 | +describe('Data Table: col.cell() custom renderer', () => { |
| 16 | + it('should invoke col.cell function to render cell content', async () => { |
| 17 | + const cellFn = vi.fn((value: any, _row: any) => ( |
| 18 | + <span data-testid="custom-cell">{`Custom: ${value}`}</span> |
| 19 | + )); |
| 20 | + |
| 21 | + const schema = { |
| 22 | + type: 'data-table', |
| 23 | + columns: [ |
| 24 | + { header: 'Name', accessorKey: 'name', cell: cellFn }, |
| 25 | + { header: 'Email', accessorKey: 'email' }, |
| 26 | + ], |
| 27 | + data: [ |
| 28 | + { name: 'Alice', email: 'alice@test.com' }, |
| 29 | + { name: 'Bob', email: 'bob@test.com' }, |
| 30 | + ], |
| 31 | + pagination: false, |
| 32 | + searchable: false, |
| 33 | + }; |
| 34 | + |
| 35 | + render(<SchemaRenderer schema={schema} />); |
| 36 | + |
| 37 | + await waitFor(() => { |
| 38 | + expect(screen.getByText('Custom: Alice')).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + expect(screen.getByText('Custom: Bob')).toBeInTheDocument(); |
| 42 | + expect(cellFn).toHaveBeenCalledTimes(2); |
| 43 | + |
| 44 | + // Non-custom cell should render value directly |
| 45 | + expect(screen.getByText('alice@test.com')).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should pass both value and row to cell function', async () => { |
| 49 | + const cellFn = vi.fn((value: any, row: any) => ( |
| 50 | + <span data-testid="combo-cell">{`${value} (${row.email})`}</span> |
| 51 | + )); |
| 52 | + |
| 53 | + const schema = { |
| 54 | + type: 'data-table', |
| 55 | + columns: [ |
| 56 | + { header: 'Name', accessorKey: 'name', cell: cellFn }, |
| 57 | + ], |
| 58 | + data: [ |
| 59 | + { name: 'Alice', email: 'alice@test.com' }, |
| 60 | + ], |
| 61 | + pagination: false, |
| 62 | + searchable: false, |
| 63 | + }; |
| 64 | + |
| 65 | + render(<SchemaRenderer schema={schema} />); |
| 66 | + |
| 67 | + await waitFor(() => { |
| 68 | + expect(screen.getByText('Alice (alice@test.com)')).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + |
| 71 | + expect(cellFn).toHaveBeenCalledWith('Alice', expect.objectContaining({ name: 'Alice', email: 'alice@test.com' })); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should render raw value when no cell function is provided', async () => { |
| 75 | + const schema = { |
| 76 | + type: 'data-table', |
| 77 | + columns: [ |
| 78 | + { header: 'Name', accessorKey: 'name' }, |
| 79 | + ], |
| 80 | + data: [ |
| 81 | + { name: 'Alice' }, |
| 82 | + ], |
| 83 | + pagination: false, |
| 84 | + searchable: false, |
| 85 | + }; |
| 86 | + |
| 87 | + render(<SchemaRenderer schema={schema} />); |
| 88 | + |
| 89 | + await waitFor(() => { |
| 90 | + expect(screen.getByText('Alice')).toBeInTheDocument(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should prefer editing state over cell function when in edit mode', async () => { |
| 95 | + // The editing state should take precedence over custom cell renderer |
| 96 | + // This test verifies the rendering priority: editing > cell > raw value |
| 97 | + const cellFn = vi.fn((value: any) => ( |
| 98 | + <span>{`Custom: ${value}`}</span> |
| 99 | + )); |
| 100 | + |
| 101 | + const schema = { |
| 102 | + type: 'data-table', |
| 103 | + columns: [ |
| 104 | + { header: 'Name', accessorKey: 'name', cell: cellFn }, |
| 105 | + ], |
| 106 | + data: [ |
| 107 | + { name: 'Alice' }, |
| 108 | + ], |
| 109 | + pagination: false, |
| 110 | + searchable: false, |
| 111 | + editable: false, |
| 112 | + }; |
| 113 | + |
| 114 | + render(<SchemaRenderer schema={schema} />); |
| 115 | + |
| 116 | + await waitFor(() => { |
| 117 | + expect(screen.getByText('Custom: Alice')).toBeInTheDocument(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments