Files

38 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { afterEach, describe, expect, it, vi } from 'vitest';
import NotificationStack from '../src/lib/components/NotificationStack.svelte';
import { notifications, notify } from '../src/lib/notifications.svelte';
// The icon library compiles over a thousand components on import; the icons are not under test.
vi.mock('@lucide/svelte', () => {
const Icon = (): void => {};
return { CircleAlert: Icon, CircleCheck: Icon, Info: Icon, OctagonAlert: Icon, X: Icon };
});
afterEach(() => {
cleanup();
for (const entry of [...notifications.entries]) notifications.dismiss(entry.id);
});
describe('NotificationStack', () => {
it('renders each notification with its variant and message', async () => {
render(NotificationStack);
notify.success('Saved', 0);
notify.error('Failed', 0);
await waitFor(() => expect(screen.getByText('Saved')).toBeTruthy());
expect(screen.getByText('Failed')).toBeTruthy();
const variants = [...document.querySelectorAll('.sc-notification')].map((node) =>
node.getAttribute('data-variant'),
);
expect(variants).toEqual(['success', 'error']);
expect(screen.getByRole('status').getAttribute('aria-live')).toBe('polite');
});
it('dismisses a notification from its close button', async () => {
render(NotificationStack);
notify.info('Dismiss me', 0);
await waitFor(() => expect(screen.getByText('Dismiss me')).toBeTruthy());
await fireEvent.click(screen.getByLabelText('Dismiss notification'));
expect(notifications.entries).toHaveLength(0);
});
});