Files
AtreidesAssetsFree/tests/ui/table-edit-values.test.mjs
T
2026-10-29 11:57:29 +01:00

60 lines
2.8 KiB
JavaScript

// Generated, do not edit. sha256:ade336b7bb05d651a613138854c9a018ee8f1d1702e7f4ffe8b66a1fb63b515b
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { seedValues, normalizeOptions, loadOptions } from '../../client/ui/table-edit-values.js';
const columns = [
{ key: 'name', edit: { type: 'text' } },
{ key: 'amount', edit: { type: 'number', value: r => (r ? r.amount * 2 : 'leeg') } },
{ key: 'shown' },
{ key: 'kind', edit: { type: 'select', options: ['a', { value: 'b', label: 'B' }] } },
{ key: 'acc', edit: { type: 'select', options: async vals => [{ value: vals.kind, label: 'from ' + vals.kind }] } },
];
describe('seedValues', () => {
it('an existing row: edit.value over the raw field, only editable columns, no defaults', () => {
assert.deepEqual(seedValues(columns, { name: 'n', amount: 2, shown: 's', kind: 'a', acc: 1 }, { name: 'default' }),
{ name: 'n', amount: 4, kind: 'a', acc: 1 });
});
it('a new row: edit.value gets null, the rest undefined, and newDefaults go on top', () => {
const v = seedValues(columns, null, { kind: 'b', extra: 1 });
assert.deepEqual(v, { name: undefined, amount: 'leeg', kind: 'b', acc: undefined, extra: 1 });
});
it('a new row without defaults', () => {
assert.deepEqual(Object.keys(seedValues(columns, null)), ['name', 'amount', 'kind', 'acc']);
});
});
describe('normalizeOptions', () => {
it('a plain value becomes value and label; an object stays as it is; nothing is an empty list', () => {
assert.deepEqual(normalizeOptions(['x', 2, { value: 'y', label: 'Y' }]), [
{ value: 'x', label: 'x' }, { value: 2, label: 2 }, { value: 'y', label: 'Y' },
]);
assert.deepEqual(normalizeOptions(null), []);
assert.deepEqual(normalizeOptions(undefined), []);
});
});
describe('loadOptions', () => {
it('fills the cache for every column with options, awaiting a callback with the current values', async () => {
const cache = await loadOptions(columns, { kind: 'z' }, new Map());
assert.deepEqual([...cache.keys()], ['kind', 'acc']);
assert.deepEqual(cache.get('kind'), [{ value: 'a', label: 'a' }, { value: 'b', label: 'B' }]);
assert.deepEqual(cache.get('acc'), [{ value: 'z', label: 'from z' }]);
});
it('empties the cache first, so a list of an earlier row does not linger', async () => {
const cache = new Map([['old', [{ value: 1, label: 1 }]], ['kind', []]]);
await loadOptions(columns, {}, cache);
assert.equal(cache.has('old'), false);
assert.equal(cache.get('kind').length, 2);
});
it('a callback that returns nothing gives an empty list, not a missing one', async () => {
const cache = await loadOptions([{ key: 'k', edit: { options: async () => null } }], {}, new Map());
assert.deepEqual(cache.get('k'), []);
});
});