|
| 1 | +/** |
| 2 | + * AccessorKey Inference Tests |
| 3 | + * |
| 4 | + * Tests that accessorKey-format columns receive type inference |
| 5 | + * via inferColumnType() + getCellRenderer(), matching the behavior |
| 6 | + * of ListColumn (field) format columns. |
| 7 | + */ |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 10 | +import '@testing-library/jest-dom'; |
| 11 | +import React from 'react'; |
| 12 | +import { ObjectGrid } from '../ObjectGrid'; |
| 13 | +import { registerAllFields } from '@object-ui/fields'; |
| 14 | +import { ActionProvider } from '@object-ui/react'; |
| 15 | + |
| 16 | +registerAllFields(); |
| 17 | + |
| 18 | +// --- Mock Data with various types --- |
| 19 | +const mockData = [ |
| 20 | + { |
| 21 | + _id: '1', |
| 22 | + name: 'Project Alpha', |
| 23 | + status: 'in_progress', |
| 24 | + priority: 'high', |
| 25 | + progress: 75, |
| 26 | + start_date: '2024-02-01T00:00:00.000Z', |
| 27 | + }, |
| 28 | + { |
| 29 | + _id: '2', |
| 30 | + name: 'Project Beta', |
| 31 | + status: 'completed', |
| 32 | + priority: 'low', |
| 33 | + progress: 100, |
| 34 | + start_date: '2024-03-15T00:00:00.000Z', |
| 35 | + }, |
| 36 | +]; |
| 37 | + |
| 38 | +// Helper: Render ObjectGrid with accessorKey-format columns |
| 39 | +function renderAccessorGrid(columns: any[], data?: any[]) { |
| 40 | + const schema: any = { |
| 41 | + type: 'object-grid' as const, |
| 42 | + objectName: 'test_object', |
| 43 | + columns, |
| 44 | + data: { provider: 'value', items: data || mockData }, |
| 45 | + }; |
| 46 | + |
| 47 | + return render( |
| 48 | + <ActionProvider> |
| 49 | + <ObjectGrid schema={schema} /> |
| 50 | + </ActionProvider> |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +// ========================================================================= |
| 55 | +// 1. accessorKey columns get type inference |
| 56 | +// ========================================================================= |
| 57 | +describe('accessorKey-format: type inference', () => { |
| 58 | + it('should infer select type for status field and render badges', async () => { |
| 59 | + renderAccessorGrid([ |
| 60 | + { header: 'Name', accessorKey: 'name' }, |
| 61 | + { header: 'Status', accessorKey: 'status' }, |
| 62 | + ]); |
| 63 | + |
| 64 | + await waitFor(() => { |
| 65 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 66 | + }); |
| 67 | + |
| 68 | + // Status should be inferred as select and render humanized badges |
| 69 | + expect(screen.getByText('In Progress')).toBeInTheDocument(); |
| 70 | + expect(screen.getByText('Completed')).toBeInTheDocument(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should infer date type for date fields', async () => { |
| 74 | + renderAccessorGrid([ |
| 75 | + { header: 'Name', accessorKey: 'name' }, |
| 76 | + { header: 'Start Date', accessorKey: 'start_date' }, |
| 77 | + ]); |
| 78 | + |
| 79 | + await waitFor(() => { |
| 80 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + // Date fields should NOT show raw ISO strings |
| 84 | + expect(screen.queryByText('2024-02-01T00:00:00.000Z')).not.toBeInTheDocument(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should infer percent type for progress field and render progress bar', async () => { |
| 88 | + renderAccessorGrid([ |
| 89 | + { header: 'Name', accessorKey: 'name' }, |
| 90 | + { header: 'Progress', accessorKey: 'progress' }, |
| 91 | + ]); |
| 92 | + |
| 93 | + await waitFor(() => { |
| 94 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + |
| 97 | + // Progress should render as percentage with progress bar |
| 98 | + expect(screen.getByText('75%')).toBeInTheDocument(); |
| 99 | + const bars = screen.getAllByRole('progressbar'); |
| 100 | + expect(bars.length).toBeGreaterThan(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should NOT override columns that already have a cell renderer', async () => { |
| 104 | + const customRenderer = (value: any) => <span data-testid="custom">{value}-custom</span>; |
| 105 | + renderAccessorGrid([ |
| 106 | + { header: 'Name', accessorKey: 'name' }, |
| 107 | + { header: 'Status', accessorKey: 'status', cell: customRenderer }, |
| 108 | + ]); |
| 109 | + |
| 110 | + await waitFor(() => { |
| 111 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + // Custom renderer should be preserved |
| 115 | + expect(screen.getByText('in_progress-custom')).toBeInTheDocument(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should pass through columns with explicit type', async () => { |
| 119 | + renderAccessorGrid([ |
| 120 | + { header: 'Name', accessorKey: 'name' }, |
| 121 | + { header: 'Priority', accessorKey: 'priority', type: 'select' }, |
| 122 | + ]); |
| 123 | + |
| 124 | + await waitFor(() => { |
| 125 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + // Priority with explicit select type should render as humanized badge |
| 129 | + expect(screen.getByText('High')).toBeInTheDocument(); |
| 130 | + expect(screen.getByText('Low')).toBeInTheDocument(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments