diff --git a/client/src/entities/teacher-reported-today.jsx b/client/src/entities/teacher-reported-today.jsx index 43900c04..37fb5304 100644 --- a/client/src/entities/teacher-reported-today.jsx +++ b/client/src/entities/teacher-reported-today.jsx @@ -183,7 +183,7 @@ const LessonRow = ({ row, showDate, until }) => { ); }; -// A lesson from the lessonTimes setting that had no report while later lessons that day did. +// A lesson from the lessonTimes setting that had no report while a later lesson that day did. const GapRow = ({ gap, showDate }) => ( diff --git a/client/src/utils/lessonGaps.js b/client/src/utils/lessonGaps.js index 6868ee51..2b394757 100644 --- a/client/src/utils/lessonGaps.js +++ b/client/src/utils/lessonGaps.js @@ -42,8 +42,8 @@ const getPeriodIndex = (row, periods) => { return periods.findLastIndex((period) => period.startMinutes <= minutes); }; -// The class's lessons with an empty slot ({ isGap: true, period, reportDate }) before each reported lesson -// that comes after one or more unreported lessons of the same day. Nothing is added after the last report of the day. +// The class's lessons with an empty slot ({ isGap: true, period, reportDate }) for each unreported lesson of the day +// that comes before a reported one (including lessons before the first report). Nothing is added after the last report of the day. // A lesson is placed by the time it was reported, so a report entered late counts for the lesson it was entered in. export function withLessonGaps(lessons, lessonTimes) { if (!Array.isArray(lessonTimes) || !lessonTimes.length) { @@ -58,11 +58,9 @@ export function withLessonGaps(lessons, lessonTimes) { const index = getPeriodIndex(row, periods); const lastIndex = lastIndexByDate.get(reportDate) ?? -1; if (index > lastIndex) { - if (lastIndex >= 0) { - periods.slice(lastIndex + 1, index).forEach((period) => { - result.push({ isGap: true, id: `gap_${reportDate}_${period.number}`, reportDate: row.reportDate, period }); - }); - } + periods.slice(lastIndex + 1, index).forEach((period) => { + result.push({ isGap: true, id: `gap_${reportDate}_${period.number}`, reportDate: row.reportDate, period }); + }); lastIndexByDate.set(reportDate, index); } result.push(row); diff --git a/client/src/utils/lessonGaps.test.js b/client/src/utils/lessonGaps.test.js index afb25282..07766c86 100644 --- a/client/src/utils/lessonGaps.test.js +++ b/client/src/utils/lessonGaps.test.js @@ -42,14 +42,25 @@ describe('withLessonGaps', () => { }); it('counts a report in the break for the lesson before it', () => { - // 09:35 is the break after lesson 2, 09:50 is lesson 3: nothing skipped. - const result = withLessonGaps([lesson(1, '2026-10-14T05:50:00Z'), lesson(2, '2026-10-14T06:35:00Z'), lesson(3, '2026-10-14T06:50:00Z')], lessonTimes); + // 08:40 is lesson 1, 09:35 is the break after lesson 2, 09:50 is lesson 3: nothing skipped. + const result = withLessonGaps([lesson(1, '2026-10-14T05:40:00Z'), lesson(2, '2026-10-14T06:35:00Z'), lesson(3, '2026-10-14T06:50:00Z')], lessonTimes); expect(result.some((row) => row.isGap)).toBe(false); }); - it('adds nothing before the first or after the last report of the day', () => { + it('adds an empty slot for each unreported lesson before the first report of the day', () => { + // 09:50 is lesson 3: lessons 1-2 were skipped. const result = withLessonGaps([lesson(1, '2026-10-14T06:50:00Z')], lessonTimes); - expect(result).toEqual([lesson(1, '2026-10-14T06:50:00Z')]); + expect(result.map((row) => (row.isGap ? `gap${row.period.number}` : row.id))).toEqual(['gap1', 'gap2', 1]); + }); + + it('adds nothing after the last report of the day', () => { + const result = withLessonGaps([lesson(1, '2026-10-14T05:10:00Z')], lessonTimes); + expect(result).toEqual([lesson(1, '2026-10-14T05:10:00Z')]); + }); + + it('adds nothing for a report before the first lesson', () => { + const result = withLessonGaps([lesson(1, '2026-10-14T04:30:00Z')], lessonTimes); + expect(result.some((row) => row.isGap)).toBe(false); }); it('does not add a slot between two reports of the same lesson', () => {