- render.test.mjs drives the real UI under jsdom: employees tab renders and add works, planning tab renders the calendar grid (makeDayCell), generate fills slots, a manual slot change updates history, and a beforeunload flush persists state to localStorage; data tab renders the history view. - persistence.test.mjs seeds malformed persisted state (employee without constraints, out-of-range moderatorsPerDay) and asserts the startup load normalizes it without crashing. - jsdom added as the single devDependency; node_modules gitignored; both suites wired into npm test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
44 lines
1.9 KiB
JavaScript
44 lines
1.9 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { JSDOM } from 'jsdom';
|
|
|
|
const HTML = `<!DOCTYPE html><html><body>
|
|
<button class="tab-btn active" data-tab="employees">Mitarbeiter</button>
|
|
<button class="tab-btn" data-tab="planning">Monatsplanung</button>
|
|
<button class="tab-btn" data-tab="data">Daten</button>
|
|
<section id="tab-employees" class="tab-content"></section>
|
|
<section id="tab-planning" class="tab-content hidden"></section>
|
|
<section id="tab-data" class="tab-content hidden"></section>
|
|
</body></html>`;
|
|
|
|
const dom = new JSDOM(HTML, { url: 'http://localhost/' });
|
|
global.window = dom.window;
|
|
global.document = dom.window.document;
|
|
global.localStorage = dom.window.localStorage;
|
|
dom.window.alert = () => {};
|
|
global.alert = () => {};
|
|
|
|
// Seed malformed persisted state BEFORE app.js loads:
|
|
// - an employee with no constraints (would crash plan generation if not normalized)
|
|
// - an out-of-range moderatorsPerDay
|
|
localStorage.setItem('morning-planner-state', JSON.stringify({
|
|
employees: [{ id: 'x', name: 'NoConstraints' }],
|
|
calendar: { holidays: [], companyClosures: [] },
|
|
history: [],
|
|
settings: { moderatorsPerDay: 99 }
|
|
}));
|
|
|
|
const { state } = await import('../js/app.js');
|
|
|
|
// Employee is loaded but normalized (constraints filled) so nothing crashes later
|
|
assert.equal(state.employees.length, 1, 'employee loaded from storage');
|
|
assert.ok(Array.isArray(state.employees[0].constraints.neverDays), 'missing constraints normalized');
|
|
assert.equal(state.employees[0].constraints.minGapDays, 14, 'minGapDays defaulted');
|
|
|
|
// Invalid moderatorsPerDay (99) falls back to the valid default
|
|
assert.equal(state.settings.moderatorsPerDay, 1, 'out-of-range setting falls back to 1');
|
|
|
|
// Rendering the employee tab with the normalized data does not throw
|
|
assert.ok(document.getElementById('emp-name-input'), 'app rendered after recovering from malformed state');
|
|
|
|
console.log('All persistence tests passed.');
|