From 97b3ddefaeecb3d87a0ae4a78ae9efee66899ba1 Mon Sep 17 00:00:00 2001 From: Linus Rath <139418639+rathlinus@users.noreply.github.com> Date: Wed, 4 Nov 2026 00:49:00 +0100 Subject: [PATCH] test: cover what the page tells the worker about a quiet login --- .../notification-actions-host-quiet.test.tsx | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 components/providers/__tests__/notification-actions-host-quiet.test.tsx diff --git a/components/providers/__tests__/notification-actions-host-quiet.test.tsx b/components/providers/__tests__/notification-actions-host-quiet.test.tsx new file mode 100644 index 000000000..d7a30d756 --- /dev/null +++ b/components/providers/__tests__/notification-actions-host-quiet.test.tsx @@ -0,0 +1,58 @@ +import { render } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { NotificationActionsHost } from '../notification-actions-host'; + +// The worker shows nothing for a login with email notifications off or do not +// disturb on. Each login has its own settings, so the answer goes per login. + +const writeLoginQuiet = vi.fn(async (..._args: unknown[]) => {}); +let state = { isAuthenticated: true, username: 'me@example.org' as string | null, emailNotificationsEnabled: true, doNotDisturb: false }; + +vi.mock('@/lib/push-preferences', () => ({ writeLoginQuiet: (...args: unknown[]) => writeLoginQuiet(...args), PUSH_STATE_CACHE: 'x' })); +vi.mock('@/lib/pwa-notify/worker-state', () => ({ writeNotificationActionPrefs: async () => {} })); +vi.mock('@/lib/pwa-notify/page-actions', () => ({ isNotificationActionRequest: () => false, runNotificationAction: async () => {} })); +vi.mock('@/lib/pwa-notify/plugin-veto', () => ({ pluginsMayVeto: () => false })); +vi.mock('@/lib/offline/offline-mode', () => ({ scheduleFlushAll: () => {} })); +vi.mock('@/lib/archive-gate', () => ({ useArchiveActionEnabled: () => false })); +vi.mock('next-intl', () => ({ useTranslations: () => (key: string) => key })); +vi.mock('@/stores/auth-store', () => ({ useAuthStore: (select: (s: typeof state) => unknown) => select(state) })); +vi.mock('@/stores/settings-store', () => ({ + useSettingsStore: (select: (s: typeof state & { archiveMode: string }) => unknown) => select({ ...state, archiveMode: 'single' }), +})); +vi.mock('@/stores/plugin-store', () => ({ usePluginStore: (select: (s: { plugins: unknown[] }) => unknown) => select({ plugins: [] }) })); +vi.mock('@/stores/account-store', () => { + const useAccountStore = (select: (s: { accounts: unknown[] }) => unknown) => select({ accounts: [] }); + useAccountStore.getState = () => ({ accounts: [] }); + return { useAccountStore }; +}); + +beforeEach(() => { + writeLoginQuiet.mockClear(); + state = { isAuthenticated: true, username: 'me@example.org', emailNotificationsEnabled: true, doNotDisturb: false }; +}); + +describe('what the page tells the worker about a login', () => { + it('not quiet with everything on', () => { + render(); + expect(writeLoginQuiet).toHaveBeenCalledWith('me@example.org', false); + }); + + it('quiet with email notifications off', () => { + state.emailNotificationsEnabled = false; + render(); + expect(writeLoginQuiet).toHaveBeenCalledWith('me@example.org', true); + }); + + it('quiet with do not disturb on', () => { + state.doNotDisturb = true; + render(); + expect(writeLoginQuiet).toHaveBeenCalledWith('me@example.org', true); + }); + + it('nothing while signed out', () => { + state.isAuthenticated = false; + state.username = null; + render(); + expect(writeLoginQuiet).not.toHaveBeenCalled(); + }); +});