Files
nexu-io__open-design/apps/web/tests/utils/notifications.test.ts
PerishFire bbdd4e84b5 chore: enforce test directory conventions (#496)
* chore: enforce test directory conventions

Move package, app, and tool tests out of src and add guard enforcement so source directories stay source-only.

* ci: use guard and package-scoped tests

Run the new repository guard in CI and keep test execution aligned with package-scoped commands after removing root aliases.

* ci: align stable release guard check

Use the new repository guard in stable release verification after replacing the residual-JS-only script.

* chore: tighten test layout enforcement

Enforce sibling tests directories, typecheck moved test suites with dedicated configs, and refresh remaining guidance that pointed at src-based tests.

* chore: clarify no-emit test tsconfigs

Explicitly disable declaration-only emit in test tsconfigs so review tooling sees they are no-emit typecheck configs.
2026-05-05 15:34:22 +08:00

98 lines
2.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { showCompletionNotification } from '../../src/utils/notifications';
type NotificationOptionsWithRenotify = NotificationOptions & { renotify?: boolean };
class MockNotification {
static permission: NotificationPermission = 'granted';
static instances: MockNotification[] = [];
onclose: (() => void) | null = null;
onclick: (() => void) | null = null;
onerror: (() => void) | null = null;
constructor(
public title: string,
public options?: NotificationOptionsWithRenotify,
) {
MockNotification.instances.push(this);
}
close(): void {
// Fire synchronously so tests can observe cleanup without browser events.
this.onclose?.();
}
}
afterEach(() => {
vi.unstubAllGlobals();
MockNotification.permission = 'granted';
MockNotification.instances = [];
});
describe('showCompletionNotification', () => {
it('creates a renotifying desktop notification when permission is granted', async () => {
vi.stubGlobal('Notification', MockNotification as unknown as typeof Notification);
const result = await showCompletionNotification({
status: 'succeeded',
title: 'Task completed',
body: 'Done',
});
expect(result).toBe('shown');
expect(MockNotification.instances).toHaveLength(1);
expect(MockNotification.instances[0]!.title).toBe('Task completed');
expect(MockNotification.instances[0]!.options).toMatchObject({
body: 'Done',
tag: 'od-task-succeeded',
renotify: true,
});
});
it('uses the service worker notification API when available', async () => {
const showNotification = vi.fn().mockResolvedValue(undefined);
const registration = { showNotification };
const register = vi.fn().mockResolvedValue(registration);
vi.stubGlobal('Notification', MockNotification as unknown as typeof Notification);
vi.stubGlobal('navigator', {
serviceWorker: {
register,
ready: Promise.resolve(registration),
},
});
const result = await showCompletionNotification({
status: 'succeeded',
title: 'Task completed',
body: 'Done',
});
expect(result).toBe('shown');
expect(register).toHaveBeenCalledWith('/od-notifications-sw.js');
expect(showNotification).toHaveBeenCalledWith(
'Task completed',
expect.objectContaining({
body: 'Done',
tag: 'od-task-succeeded',
renotify: true,
}),
);
expect(MockNotification.instances).toHaveLength(0);
});
it('does not create a notification when permission is not granted', async () => {
MockNotification.permission = 'denied';
vi.stubGlobal('Notification', MockNotification as unknown as typeof Notification);
const result = await showCompletionNotification({
status: 'failed',
title: 'Task failed',
body: 'Error',
});
expect(result).toBe('permission-denied');
expect(MockNotification.instances).toHaveLength(0);
});
});