Files
cinny/src/app/hooks/useNotificationsQuiet.test.ts
T

56 lines
2.1 KiB
TypeScript
Raw Normal View History

import { test } from 'node:test';
import assert from 'node:assert/strict';
// `useNotificationsQuiet.ts` pulls in `state/notificationSnooze.ts`, whose
// `atomWithStorage(..., { getOnInit: true })` reads `localStorage` eagerly at
// module-import time. Plain `node:test` has no `localStorage`, so install a
// minimal stub before dynamically importing the module under test (mirrors the
// mocking approach in `state/settings.test.ts`).
(globalThis as { localStorage?: unknown }).localStorage = {
getItem: () => null,
setItem: () => undefined,
removeItem: () => undefined,
};
const { isNotificationsQuiet } = await import('./useNotificationsQuiet');
const base = {
focusAssistActive: false,
manualDnd: false,
snoozeUntil: 0,
quietHoursEnabled: false,
quietHoursStart: '22:00',
quietHoursEnd: '07:00',
};
test('isNotificationsQuiet: quiet when none of the inputs are active', () => {
assert.equal(isNotificationsQuiet(base), false);
});
test('isNotificationsQuiet: active quiet-hours window suppresses (#28)', () => {
const now = new Date(2026, 0, 1, 23, 0);
assert.equal(isNotificationsQuiet({ ...base, quietHoursEnabled: true, now }), true);
// Disabled setting is not enough on its own, even inside the window.
assert.equal(isNotificationsQuiet({ ...base, quietHoursEnabled: false, now }), false);
// Outside the window, enabling it does not suppress.
const outside = new Date(2026, 0, 1, 12, 0);
assert.equal(isNotificationsQuiet({ ...base, quietHoursEnabled: true, now: outside }), false);
});
test('isNotificationsQuiet: presence/manual DND suppresses (#28)', () => {
assert.equal(isNotificationsQuiet({ ...base, focusAssistActive: true }), true);
assert.equal(isNotificationsQuiet({ ...base, manualDnd: true }), true);
});
test('isNotificationsQuiet: an active snooze suppresses (#28)', () => {
const now = Date.now();
assert.equal(
isNotificationsQuiet({ ...base, snoozeUntil: now + 60_000, now: new Date(now) }),
true,
);
assert.equal(
isNotificationsQuiet({ ...base, snoozeUntil: now - 60_000, now: new Date(now) }),
false,
);
});