diff --git a/stores/__tests__/settings-push-dnd.test.ts b/stores/__tests__/settings-push-dnd.test.ts new file mode 100644 index 000000000..52df90530 --- /dev/null +++ b/stores/__tests__/settings-push-dnd.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, it, vi } from 'vitest'; + +const writePushPreferences = vi.hoisted(() => vi.fn(async () => {})); +vi.mock('@/lib/push-preferences', () => ({ writePushPreferences })); + +import { useSettingsStore } from '../settings-store'; + +describe('do not disturb and push notifications (CO-13)', () => { + it('keeps pushed notifications silent while do not disturb is on', () => { + useSettingsStore.setState({ emailNotificationsEnabled: true, emailNotificationSound: true, doNotDisturb: false }); + writePushPreferences.mockClear(); + useSettingsStore.getState().updateSetting('doNotDisturb', true); + expect(writePushPreferences).toHaveBeenLastCalledWith({ sound: false }); + useSettingsStore.getState().updateSetting('doNotDisturb', false); + expect(writePushPreferences).toHaveBeenLastCalledWith({ sound: true }); + }); +}); diff --git a/stores/settings-store.ts b/stores/settings-store.ts index 3a9cd7cb4..688c0a1d4 100644 --- a/stores/settings-store.ts +++ b/stores/settings-store.ts @@ -2131,7 +2131,9 @@ if (typeof window !== 'undefined') { // The service worker shows push notifications without this store, so it // reads the sound preference from its own Cache store (#1289). - const pushSound = (s: SettingsState) => s.emailNotificationsEnabled && s.emailNotificationSound; + // Do not disturb can't keep a push from showing (browsers require one per + // push), but it keeps it silent. + const pushSound = (s: SettingsState) => s.emailNotificationsEnabled && s.emailNotificationSound && !s.doNotDisturb; let lastPushSound = pushSound(store); void writePushPreferences({ sound: lastPushSound }); useSettingsStore.subscribe((state) => {