Files
Meeting-Mixer/tests/data.test.mjs
T

80 lines
2.9 KiB
JavaScript

import assert from 'node:assert/strict';
import { addHistoryEntries, removeHistoryEntry } from '../js/data.js';
function makeState(history = []) {
return {
employees: [],
calendar: { holidays: [], companyClosures: [] },
history: [...history]
};
}
// addHistoryEntries: two entries with same date but different employees both kept
{
const state = makeState();
addHistoryEntries(state, [
{ date: '2026-04-01', employeeId: 'alice', manual: false },
{ date: '2026-04-01', employeeId: 'bob', manual: false }
]);
assert.equal(state.history.length, 2, 'two distinct entries kept on same date');
const ids = state.history.map(h => h.employeeId).sort();
assert.deepEqual(ids, ['alice', 'bob'], 'both employees present');
}
// addHistoryEntries: same (date, employeeId) replaced, others on same date untouched
{
const state = makeState([
{ date: '2026-04-01', employeeId: 'alice', manual: false },
{ date: '2026-04-01', employeeId: 'bob', manual: false }
]);
addHistoryEntries(state, [
{ date: '2026-04-01', employeeId: 'alice', manual: true }
]);
assert.equal(state.history.length, 2, 'still two entries');
const alice = state.history.find(h => h.employeeId === 'alice');
assert.equal(alice.manual, true, 'alice replaced with manual=true');
const bob = state.history.find(h => h.employeeId === 'bob');
assert.equal(bob.manual, false, 'bob untouched');
}
// addHistoryEntries: empty input → no change
{
const state = makeState([{ date: '2026-04-01', employeeId: 'alice', manual: false }]);
addHistoryEntries(state, []);
assert.equal(state.history.length, 1, 'empty entries → no change');
}
// removeHistoryEntry without employeeId: removes all entries on that date
{
const state = makeState([
{ date: '2026-04-01', employeeId: 'alice', manual: false },
{ date: '2026-04-01', employeeId: 'bob', manual: false },
{ date: '2026-04-02', employeeId: 'alice', manual: false }
]);
removeHistoryEntry(state, '2026-04-01');
assert.equal(state.history.length, 1, 'only 2026-04-02 alice remains');
assert.equal(state.history[0].date, '2026-04-02', 'remaining entry is 2026-04-02');
}
// removeHistoryEntry with employeeId: removes only that one entry
{
const state = makeState([
{ date: '2026-04-01', employeeId: 'alice', manual: false },
{ date: '2026-04-01', employeeId: 'bob', manual: false }
]);
removeHistoryEntry(state, '2026-04-01', 'alice');
assert.equal(state.history.length, 1, 'one entry remains');
assert.equal(state.history[0].employeeId, 'bob', 'bob still there');
}
// removeHistoryEntry with employeeId that does not exist: no-op
{
const state = makeState([
{ date: '2026-04-01', employeeId: 'alice', manual: false }
]);
removeHistoryEntry(state, '2026-04-01', 'nonexistent');
assert.equal(state.history.length, 1, 'no change when employeeId not found');
}
console.log('All data tests passed.');