const test = require('node:test');
const assert = require('node:assert/strict');
const { boxesIn, headerState, selectionKeys } = require('./bulk.js');
const box = (value, { checked = false, disabled = false } = {}) => ({
value,
checked,
disabled,
});
const rootOf = (...boxes) => ({ querySelectorAll: () => boxes });
test('boxesIn drops the disabled boxes a page renders', () => {
const root = rootOf(box('ada'), box('ken', { disabled: true }), box('grace'));
assert.deepEqual(boxesIn(root).map((b) => b.value), ['ada', 'grace']);
});
test('the tri-state header reads "all" with a denied row on the page', () => {
const root = rootOf(box('ada', { checked: true }), box('ken', { disabled: true }));
assert.deepEqual(headerState(boxesIn(root)), { checked: true, indeterminate: false });
});
test('a page of only denied boxes offers nothing to check', () => {
const root = rootOf(box('ken', { disabled: true }), box('bob', { disabled: true }));
assert.deepEqual(boxesIn(root), []);
assert.deepEqual(headerState(boxesIn(root)), { checked: false, indeterminate: false });
});
test('a disabled box never enters the wire, checked or not', () => {
const boxes = [
box('ada', { checked: true }),
box('ken', { checked: true, disabled: true }),
box('grace'),
];
assert.deepEqual(selectionKeys(boxes, ''), ['ada']);
});
test('a stale wire entry for a now-denied row is dropped', () => {
assert.deepEqual(selectionKeys([box('ken', { disabled: true })], ',ken,'), []);
});
test('keys selected on other pages survive the page-local read', () => {
const boxes = [box('ada', { checked: true }), box('ken', { disabled: true })];
assert.deepEqual(selectionKeys(boxes, ',grace,'), ['grace', 'ada']);
});
test('a key on this page is decided by this page, never duplicated', () => {
const boxes = [box('ada', { checked: true })];
assert.deepEqual(selectionKeys(boxes, ',ada,'), ['ada']);
});
test('the header reads "all" when every selectable row is checked', () => {
const selectable = [box('ada', { checked: true }), box('grace', { checked: true })];
assert.deepEqual(headerState(selectable), { checked: true, indeterminate: false });
});
test('the header reads "partial" on a partial selection', () => {
const selectable = [box('ada', { checked: true }), box('grace')];
assert.deepEqual(headerState(selectable), { checked: false, indeterminate: true });
});