Skip to content

Commit

Permalink
test: Add table data freeze tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Jul 6, 2024
1 parent e27eb9f commit 364c2a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/table/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Table {
*/
constructor(columns, names, filter, group, order, params) {
const data = Object.freeze({ ...columns });
names = names ?? Object.keys(data);
names = names?.slice() ?? Object.keys(data);
const nrows = names.length ? data[names[0]].length : 0;
/** @private */
this._names = Object.freeze(names);
Expand Down
19 changes: 19 additions & 0 deletions test/table/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ describe('Table', () => {
assert.deepEqual(scanned, ref, 'scanned values match');
});

it('copies and freezes column object', () => {
const cols = { x: [1, 2, 3 ]};
const table = new Table(cols);
const data = table.data();
assert.notStrictEqual(data, cols, 'is copied');
assert.strictEqual(data, table._data, 'is direct');
assert.ok(Object.isFrozen(data), 'is frozen');
assert.throws(() => data.y = [4, 5, 6], 'throws on edit');
});

it('copies and freezes column name list', () => {
const names = ['y', 'x'];
const cols = { x: [1, 2, 3 ], y: [4, 5, 6]};
const table = new Table(cols, names);
assert.notStrictEqual(table._names, names, 'is copied');
assert.ok(Object.isFrozen(table._names), 'is frozen');
assert.throws(() => table._names.push('z'), 'throws on edit');
});

it('scan supports filtering and ordering', () => {
const table = new Table({
a: ['a', 'a', 'a', 'b', 'b'],
Expand Down

0 comments on commit 364c2a6

Please sign in to comment.