|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { arrayDataFrame } from '../../src/helpers/dataframeV2.js' |
| 3 | + |
| 4 | +describe('arrayDataFrame', () => { |
| 5 | + const testData = [ |
| 6 | + { id: 1, name: 'Alice', age: 30 }, |
| 7 | + { id: 2, name: 'Bob', age: 25 }, |
| 8 | + { id: 3, name: 'Charlie', age: 35 }, |
| 9 | + ] |
| 10 | + |
| 11 | + it('should create a DataFrame with correct header and numRows', () => { |
| 12 | + const df = arrayDataFrame(testData) |
| 13 | + expect(df.header).toEqual(['id', 'name', 'age']) |
| 14 | + expect(df.numRows).toBe(3) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should return the cell value without first fetching the column', () => { |
| 18 | + const df = arrayDataFrame(testData) |
| 19 | + const cell = df.getCell({ row: 1, column: 'name' }) |
| 20 | + expect(cell?.value).toBe('Bob') |
| 21 | + }) |
| 22 | + |
| 23 | + it('should throw if accessing data from an unknown column', () => { |
| 24 | + const df = arrayDataFrame(testData) |
| 25 | + expect(() => { |
| 26 | + df.getCell({ row: 0, column: 'doesnotexist' } ) |
| 27 | + }).toThrow('Invalid column: doesnotexist') |
| 28 | + }) |
| 29 | + |
| 30 | + it('should throw if accessing data from an unknown row', () => { |
| 31 | + const df = arrayDataFrame(testData) |
| 32 | + expect(() => { |
| 33 | + df.getCell({ row: 3, column: 'id' } ) |
| 34 | + }).toThrow('Invalid row index: 3') |
| 35 | + }) |
| 36 | + |
| 37 | + it('should throw if accessing data from an empty array', () => { |
| 38 | + const df = arrayDataFrame([]) |
| 39 | + expect(df.header).toEqual([]) |
| 40 | + expect(df.numRows).toBe(0) |
| 41 | + expect(() => { |
| 42 | + df.getCell({ row: 0, column: 'name' } ) |
| 43 | + }).toThrow('Invalid row index: 0') |
| 44 | + }) |
| 45 | + |
| 46 | + it('provides fetch, even if it\'s a no-op function', () => { |
| 47 | + const df = arrayDataFrame([]) |
| 48 | + expect(df.fetch).toBeDefined() |
| 49 | + }) |
| 50 | + |
| 51 | +}) |
0 commit comments