Files
dodo/frontend/src/lib/today-section-collapse.test.ts
T
bboysoul f5a1521a4f
ci / gitleaks (push) Successful in 8s
ci / docker (push) Successful in 6m1s
feat: collapse Today sections
2026-09-15 09:47:02 +08:00

44 lines
1.9 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { readTodaySectionCollapse, writeTodaySectionCollapse } from './today-section-collapse'
const defaults = { overdue: false, tasks: false, habits: false }
function fakeStorage(initial?: string) {
const values = new Map<string, string>()
if (initial !== undefined) values.set('dodo.today-section-collapse.v1', initial)
return {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => values.set(key, value),
value: () => values.get('dodo.today-section-collapse.v1'),
}
}
describe('Today section collapse persistence', () => {
it('defaults every section to expanded when storage is absent or malformed', () => {
expect(readTodaySectionCollapse(fakeStorage(), 'dodo.today-section-collapse.v1')).toEqual(defaults)
expect(readTodaySectionCollapse(fakeStorage('{bad'), 'dodo.today-section-collapse.v1')).toEqual(defaults)
})
it('accepts only boolean fields and falls back invalid or missing fields independently', () => {
expect(readTodaySectionCollapse(fakeStorage(JSON.stringify({ overdue: true, tasks: 'yes', extra: true })), 'dodo.today-section-collapse.v1')).toEqual({
overdue: true,
tasks: false,
habits: false,
})
})
it('does not break collapsing when storage rejects a write', () => {
const storage = {
getItem: () => null,
setItem: () => { throw new Error('quota exceeded') },
}
expect(() => writeTodaySectionCollapse(storage, 'dodo.today-section-collapse.v1', defaults)).not.toThrow()
})
it('writes all three independent section states under the single versioned key', () => {
const storage = fakeStorage()
writeTodaySectionCollapse(storage, 'dodo.today-section-collapse.v1', { overdue: true, tasks: false, habits: true })
expect(storage.value()).toBe(JSON.stringify({ overdue: true, tasks: false, habits: true }))
})
})