Skip to content

Commit 466c2cb

Browse files
committed
add test + fix two bugs
1 parent 5dd6274 commit 466c2cb

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/helpers/dataframeV2.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,8 @@ export function arrayDataFrame(data: Cells[]): DataFrameV2 {
6060
},
6161
}
6262
}
63-
if (!(0 in data)) {
64-
return {
65-
numRows: 0,
66-
header: [],
67-
getCell: () => undefined,
68-
fetch,
69-
eventTarget,
70-
}
71-
}
7263

73-
const header = Object.keys(data[0])
64+
const header = 0 in data ? Object.keys(data[0]) : []
7465

7566
return {
7667
numRows: data.length,
@@ -90,7 +81,7 @@ export function arrayDataFrame(data: Cells[]): DataFrameV2 {
9081
throw new Error(`Column "${column}" not found in row ${row}`)
9182
}
9283
// Return a resolved value (which might be undefined as well)
93-
return { value: data[row] }
84+
return { value: cells[column] }
9485
// Note that this function never returns undefined (meaning pending cell), because the data is static.
9586
},
9687
fetch,

test/helpers/dataframeV2.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)