|
9 | 9 | import { describe, it, expect, vi } from 'vitest'; |
10 | 10 | import { render, screen, waitFor } from '@testing-library/react'; |
11 | 11 | import React from 'react'; |
12 | | -import { ObjectDataTable } from '../ObjectDataTable'; |
| 12 | +import { ObjectDataTable, normalizeColumns } from '../ObjectDataTable'; |
13 | 13 | import { SchemaRendererProvider } from '@object-ui/react'; |
14 | 14 |
|
15 | 15 | describe('ObjectDataTable', () => { |
@@ -119,4 +119,93 @@ describe('ObjectDataTable', () => { |
119 | 119 |
|
120 | 120 | expect(dataSource.find).not.toHaveBeenCalled(); |
121 | 121 | }); |
| 122 | + |
| 123 | + it('should normalize string[] columns without crashing', () => { |
| 124 | + const schema = { |
| 125 | + ...baseSchema, |
| 126 | + columns: ['name', 'amount', 'close_date'], |
| 127 | + data: [ |
| 128 | + { name: 'Deal A', amount: 1000, close_date: '2025-01-01' }, |
| 129 | + { name: 'Deal B', amount: 2000, close_date: '2025-06-01' }, |
| 130 | + ], |
| 131 | + }; |
| 132 | + |
| 133 | + // Should not crash when columns are strings |
| 134 | + const { container } = render( |
| 135 | + <SchemaRendererProvider> |
| 136 | + <ObjectDataTable schema={schema} /> |
| 137 | + </SchemaRendererProvider>, |
| 138 | + ); |
| 139 | + |
| 140 | + expect(container).toBeDefined(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should pass through object[] columns unchanged', () => { |
| 144 | + const schema = { |
| 145 | + ...baseSchema, |
| 146 | + columns: [ |
| 147 | + { header: 'Name', accessorKey: 'name' }, |
| 148 | + { header: 'Amount', accessorKey: 'amount' }, |
| 149 | + ], |
| 150 | + data: [ |
| 151 | + { name: 'Deal A', amount: 1000 }, |
| 152 | + ], |
| 153 | + }; |
| 154 | + |
| 155 | + const { container } = render( |
| 156 | + <SchemaRendererProvider> |
| 157 | + <ObjectDataTable schema={schema} /> |
| 158 | + </SchemaRendererProvider>, |
| 159 | + ); |
| 160 | + |
| 161 | + expect(container).toBeDefined(); |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe('normalizeColumns', () => { |
| 166 | + it('should convert snake_case string to title-cased header', () => { |
| 167 | + const result = normalizeColumns(['close_date']); |
| 168 | + expect(result).toEqual([{ header: 'Close Date', accessorKey: 'close_date' }]); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should convert camelCase string to title-cased header', () => { |
| 172 | + const result = normalizeColumns(['firstName']); |
| 173 | + expect(result).toEqual([{ header: 'First Name', accessorKey: 'firstName' }]); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should convert simple string to capitalized header', () => { |
| 177 | + const result = normalizeColumns(['name']); |
| 178 | + expect(result).toEqual([{ header: 'Name', accessorKey: 'name' }]); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should handle multiple string columns', () => { |
| 182 | + const result = normalizeColumns(['name', 'total_amount', 'createdAt']); |
| 183 | + expect(result).toEqual([ |
| 184 | + { header: 'Name', accessorKey: 'name' }, |
| 185 | + { header: 'Total Amount', accessorKey: 'total_amount' }, |
| 186 | + { header: 'Created At', accessorKey: 'createdAt' }, |
| 187 | + ]); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should pass through object columns unchanged', () => { |
| 191 | + const cols = [ |
| 192 | + { header: 'Custom Name', accessorKey: 'name' }, |
| 193 | + { header: 'Amount ($)', accessorKey: 'amount' }, |
| 194 | + ]; |
| 195 | + const result = normalizeColumns(cols); |
| 196 | + expect(result).toEqual(cols); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should handle mixed string and object columns', () => { |
| 200 | + const result = normalizeColumns([ |
| 201 | + 'name', |
| 202 | + { header: 'Custom Amount', accessorKey: 'amount' }, |
| 203 | + 'close_date', |
| 204 | + ]); |
| 205 | + expect(result).toEqual([ |
| 206 | + { header: 'Name', accessorKey: 'name' }, |
| 207 | + { header: 'Custom Amount', accessorKey: 'amount' }, |
| 208 | + { header: 'Close Date', accessorKey: 'close_date' }, |
| 209 | + ]); |
| 210 | + }); |
122 | 211 | }); |
0 commit comments