// Generated, do not edit. sha256:455789d5a2acadd3972e4561a3135c6234ab395e2d111780c60328e8b4850468 import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { initialState, chooseGroup, revealGroup } from '../../client/ui/table-state.js'; const columns = [ { key: 'name' }, { key: 'kind', group: { value: r => r.kind } }, { key: 'place', group: { value: r => r.place } }, { key: 'year', group: { value: r => r.year } }, ]; describe('initialState', () => { it('without stored state: no sort, no grouping, empty maps, no sticky', () => { assert.deepEqual(initialState({ columns }), { sort: null, groupKey: null, groupKey2: null, groupKey3: null, groupFilter: {}, groupCollapsed: {}, sticky: null, }); }); it('a stored sort on a column that is gone falls back on defaultSort, and that one is checked too', () => { assert.deepEqual(initialState({ columns, sort: { key: 'gone', dir: 'asc' }, defaultSort: { key: 'name', dir: 'desc' } }).sort, { key: 'name', dir: 'desc' }); assert.equal(initialState({ columns, sort: { key: 'gone' }, defaultSort: { key: 'also-gone' } }).sort, null); }); it('a stored group on a column that is gone or cannot group is ignored, per level', () => { const s = initialState({ columns, group: 'name', group2: 'gone', group3: 'year' }); assert.equal(s.groupKey, null); assert.equal(s.groupKey2, null); assert.equal(s.groupKey3, 'year'); }); it('copies what it takes, so the configuration is never changed through the state', () => { const cfg = { columns, sort: { key: 'name', dir: 'asc' }, groupFilter: { kind: ['a'] }, groupCollapsed: { kind: ['b'] }, sticky: { name: 'x' } }; const s = initialState(cfg); s.sort.dir = 'desc'; s.groupFilter.kind = []; s.groupCollapsed.place = ['p']; s.sticky.name = 'y'; assert.deepEqual(cfg.sort, { key: 'name', dir: 'asc' }); assert.deepEqual(cfg.groupFilter, { kind: ['a'] }); assert.deepEqual(cfg.groupCollapsed, { kind: ['b'] }); assert.deepEqual(cfg.sticky, { name: 'x' }); }); }); describe('chooseGroup', () => { const all = { groupKey: 'kind', groupKey2: 'place', groupKey3: 'year' }; it('level 1 to none drops the deeper levels', () => { assert.deepEqual(chooseGroup(all, 1, ''), { groupKey: null, groupKey2: null, groupKey3: null }); assert.deepEqual(chooseGroup(all, 1, null), { groupKey: null, groupKey2: null, groupKey3: null }); }); it('level 1 onto a column used deeper takes it from that level, and keeps the other', () => { assert.deepEqual(chooseGroup(all, 1, 'place'), { groupKey: 'place', groupKey2: null, groupKey3: 'year' }); assert.deepEqual(chooseGroup(all, 1, 'year'), { groupKey: 'year', groupKey2: 'place', groupKey3: null }); }); it('level 2 to none drops level 3; level 2 onto level 3 clears level 3', () => { assert.deepEqual(chooseGroup(all, 2, ''), { groupKey: 'kind', groupKey2: null, groupKey3: null }); assert.deepEqual(chooseGroup(all, 2, 'year'), { groupKey: 'kind', groupKey2: 'year', groupKey3: null }); }); it('level 3 only sets level 3, and the input is not modified', () => { const keys = { ...all }; assert.deepEqual(chooseGroup(keys, 3, ''), { groupKey: 'kind', groupKey2: 'place', groupKey3: null }); assert.deepEqual(keys, all); }); }); describe('revealGroup', () => { it('takes the token out of the filter AND unfolds it, and says it changed', () => { const r = revealGroup({ groupKey: 'kind', groupFilter: { kind: ['a', 'b'] }, groupCollapsed: { kind: ['a'] } }, 'a'); assert.deepEqual(r, { groupFilter: { kind: ['b'] }, groupCollapsed: { kind: [] }, changed: true }); }); it('only collapsed, or only hidden, is a change too', () => { assert.equal(revealGroup({ groupKey: 'kind', groupFilter: {}, groupCollapsed: { kind: ['a'] } }, 'a').changed, true); assert.equal(revealGroup({ groupKey: 'kind', groupFilter: { kind: ['a'] }, groupCollapsed: {} }, 'a').changed, true); }); it('a visible group, or no grouping at all, changes nothing and returns the same maps', () => { const groupFilter = { kind: ['b'] }; const groupCollapsed = {}; const r = revealGroup({ groupKey: 'kind', groupFilter, groupCollapsed }, 'a'); assert.equal(r.changed, false); assert.equal(r.groupFilter, groupFilter); assert.equal(revealGroup({ groupKey: null, groupFilter: { kind: ['a'] }, groupCollapsed: {} }, 'a').changed, false); }); it('does not modify the maps it was given', () => { const groupFilter = { kind: ['a'], place: ['x'] }; revealGroup({ groupKey: 'kind', groupFilter, groupCollapsed: {} }, 'a'); assert.deepEqual(groupFilter, { kind: ['a'], place: ['x'] }); }); });