2016-07-29 02:37:43 +08:00
|
|
|
import {testData, testDataSource, testDataSourceUneven} from '../../fixtures/table-data'
|
2016-05-06 06:21:41 +08:00
|
|
|
|
|
|
|
|
2016-05-12 07:38:19 +08:00
|
|
|
describe('TableDataSource', function describeBlock() {
|
2016-05-06 06:21:41 +08:00
|
|
|
describe('colAt', () => {
|
|
|
|
it('returns the correct value for column', () => {
|
|
|
|
expect(testDataSource.colAt(1)).toEqual('col2')
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if col does not exist', () => {
|
|
|
|
expect(testDataSource.colAt(3)).toBe(null)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rowAt', () => {
|
|
|
|
it('returns correct row', () => {
|
|
|
|
expect(testDataSource.rowAt(1)).toEqual([4, 5, 6])
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns columns if rowIdx is null', () => {
|
|
|
|
expect(testDataSource.rowAt(null)).toEqual(['col1', 'col2', 'col3'])
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if row does not exist', () => {
|
|
|
|
expect(testDataSource.rowAt(3)).toBe(null)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('cellAt', () => {
|
|
|
|
it('returns correct cell', () => {
|
|
|
|
expect(testDataSource.cellAt({rowIdx: 1, colIdx: 1})).toEqual(5)
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns correct col if rowIdx is null', () => {
|
|
|
|
expect(testDataSource.cellAt({rowIdx: null, colIdx: 1})).toEqual('col2')
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if cell does not exist', () => {
|
|
|
|
expect(testDataSource.cellAt({rowIdx: 3, colIdx: 1})).toBe(null)
|
|
|
|
expect(testDataSource.cellAt({rowIdx: 1, colIdx: 3})).toBe(null)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rows', () => {
|
|
|
|
it('returns all rows', () => {
|
|
|
|
expect(testDataSource.rows()).toBe(testData.rows)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('columns', () => {
|
|
|
|
it('returns all columns', () => {
|
|
|
|
expect(testDataSource.columns()).toBe(testData.columns)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addColumn', () => {
|
|
|
|
it('pushes a new column to the data source\'s columns', () => {
|
|
|
|
const res = testDataSource.addColumn()
|
|
|
|
expect(res.columns()).toEqual(['col1', 'col2', 'col3', null])
|
|
|
|
});
|
|
|
|
|
|
|
|
it('pushes a new column to every row', () => {
|
|
|
|
const res = testDataSource.addColumn()
|
|
|
|
expect(res.rows()).toEqual([
|
|
|
|
[1, 2, 3, null],
|
|
|
|
[4, 5, 6, null],
|
|
|
|
[7, 8, 9, null],
|
|
|
|
])
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-23 05:22:24 +08:00
|
|
|
describe('removeLastColumn', () => {
|
2016-05-06 06:21:41 +08:00
|
|
|
it('removes last column from the data source\'s columns', () => {
|
2016-07-23 05:22:24 +08:00
|
|
|
const res = testDataSource.removeLastColumn()
|
2016-05-06 06:21:41 +08:00
|
|
|
expect(res.columns()).toEqual(['col1', 'col2'])
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes last column from every row', () => {
|
2016-07-23 05:22:24 +08:00
|
|
|
const res = testDataSource.removeLastColumn()
|
2016-05-06 06:21:41 +08:00
|
|
|
expect(res.rows()).toEqual([
|
|
|
|
[1, 2],
|
|
|
|
[4, 5],
|
|
|
|
[7, 8],
|
|
|
|
])
|
|
|
|
});
|
2016-07-29 02:37:43 +08:00
|
|
|
|
|
|
|
it('removes the last column only from every row with that column', () => {
|
|
|
|
const res = testDataSourceUneven.removeLastColumn()
|
|
|
|
expect(res.rows()).toEqual([
|
|
|
|
[1, 2],
|
|
|
|
[4, 5],
|
|
|
|
[7, 8],
|
|
|
|
])
|
|
|
|
})
|
2016-05-06 06:21:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('addRow', () => {
|
|
|
|
it('pushes an empty row with correct number of columns', () => {
|
|
|
|
const res = testDataSource.addRow()
|
|
|
|
expect(res.rows()).toEqual([
|
|
|
|
[1, 2, 3],
|
|
|
|
[4, 5, 6],
|
|
|
|
[7, 8, 9],
|
|
|
|
[null, null, null],
|
|
|
|
])
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeRow', () => {
|
|
|
|
it('removes last row', () => {
|
|
|
|
const res = testDataSource.removeRow()
|
|
|
|
expect(res.rows()).toEqual([
|
|
|
|
[1, 2, 3],
|
|
|
|
[4, 5, 6],
|
|
|
|
])
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateCell', () => {
|
|
|
|
it('updates cell value correctly when updating a cell that is /not/ a header', () => {
|
|
|
|
const res = testDataSource.updateCell({
|
|
|
|
rowIdx: 0, colIdx: 0, isHeader: false, value: 'new-val',
|
|
|
|
})
|
|
|
|
expect(res.columns()).toBe(testDataSource.columns())
|
|
|
|
expect(res.rows()).toEqual([
|
|
|
|
['new-val', 2, 3],
|
|
|
|
[4, 5, 6],
|
|
|
|
[7, 8, 9],
|
|
|
|
])
|
|
|
|
|
|
|
|
// If a row doesn't change, it should be the same row
|
|
|
|
testDataSource.rows().slice(1).forEach((row, rowIdx) => expect(row).toBe(testDataSource.rowAt(rowIdx + 1)))
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates cell value correctly when updating a cell that /is/ a header', () => {
|
|
|
|
const res = testDataSource.updateCell({
|
|
|
|
rowIdx: null, colIdx: 0, isHeader: true, value: 'new-val',
|
|
|
|
})
|
|
|
|
expect(res.columns()).toEqual(['new-val', 'col2', 'col3'])
|
|
|
|
expect(res.rows()).toBe(testDataSource.rows())
|
|
|
|
|
|
|
|
// If a row doesn't change, it should be the same row
|
|
|
|
testDataSource.rows().forEach((row, rowIdx) => expect(row).toBe(testDataSource.rowAt(rowIdx)))
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('clear', () => {
|
|
|
|
it('clears all data correcltly', () => {
|
|
|
|
const res = testDataSource.clear()
|
|
|
|
expect(res.toJSON()).toEqual({
|
|
|
|
columns: [],
|
|
|
|
rows: [[]],
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('toJSON', () => {
|
|
|
|
it('returns correct json object from data source', () => {
|
|
|
|
const res = testDataSource.toJSON()
|
|
|
|
expect(res).toEqual(testData)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|