68 lines
2.3 KiB
JavaScript
68 lines
2.3 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { setPlanningDay } from '#tofu/tools/planning/setPlanningDay.js'
|
|
|
|
function buildPlanning() {
|
|
return {
|
|
weekId: '2026-W39',
|
|
days: [
|
|
{ date: '2026-09-21', dish: '', ingredients: '' },
|
|
{ date: '2026-09-22', dish: 'Soupe', ingredients: 'poireaux, pommes de terre' },
|
|
],
|
|
}
|
|
}
|
|
|
|
test('patches the matching day only', () => {
|
|
const next = setPlanningDay(buildPlanning(), '2026-09-21', { dish: 'Gnocchis à la sauge' })
|
|
assert.deepEqual(next.days[0], {
|
|
date: '2026-09-21',
|
|
dish: 'Gnocchis à la sauge',
|
|
ingredients: '',
|
|
})
|
|
assert.deepEqual(next.days[1], buildPlanning().days[1])
|
|
assert.equal(next.weekId, '2026-W39')
|
|
})
|
|
|
|
test('a partial patch keeps the other fields of the day', () => {
|
|
const next = setPlanningDay(buildPlanning(), '2026-09-22', { ingredients: 'poireaux' })
|
|
assert.equal(next.days[1].dish, 'Soupe')
|
|
assert.equal(next.days[1].ingredients, 'poireaux')
|
|
})
|
|
|
|
test('returns a new planning and a new days array', () => {
|
|
const planning = buildPlanning()
|
|
const next = setPlanningDay(planning, '2026-09-21', { dish: 'Gratin' })
|
|
assert.notEqual(next, planning)
|
|
assert.notEqual(next.days, planning.days)
|
|
assert.notEqual(next.days[0], planning.days[0])
|
|
})
|
|
|
|
test('does not mutate the received planning, even deeply', () => {
|
|
const planning = buildPlanning()
|
|
setPlanningDay(planning, '2026-09-21', { dish: 'Gratin', ingredients: 'courgettes' })
|
|
assert.deepEqual(planning, buildPlanning())
|
|
})
|
|
|
|
test('does not mutate the day objects handed over to the caller before', () => {
|
|
const planning = buildPlanning()
|
|
const firstDay = planning.days[0]
|
|
setPlanningDay(planning, '2026-09-21', { dish: 'Gratin' })
|
|
assert.equal(firstDay.dish, '')
|
|
})
|
|
|
|
test('an unknown date key gives the planning back unchanged', () => {
|
|
const planning = buildPlanning()
|
|
const next = setPlanningDay(planning, '2026-12-25', { dish: 'Dinde' })
|
|
assert.equal(next, planning)
|
|
assert.deepEqual(next, buildPlanning())
|
|
})
|
|
|
|
test('a planning without days is given back as is', () => {
|
|
const planning = { weekId: '2026-W39' }
|
|
assert.equal(setPlanningDay(planning, '2026-09-21', { dish: 'Gratin' }), planning)
|
|
})
|
|
|
|
test('an empty patch leaves the day untouched', () => {
|
|
const next = setPlanningDay(buildPlanning(), '2026-09-22', {})
|
|
assert.deepEqual(next, buildPlanning())
|
|
})
|