emi-challenge-fe/tests/app/core/services/notification.spec.ts

87 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { Notification } from '@app/core/services/notification';
describe('Notification', () => {
let service: Notification;
beforeEach(() => {
service = new Notification();
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should create', () => {
expect(service).toBeTruthy();
});
it('should start with empty notifications', () => {
expect(service.notifications()).toEqual([]);
});
it('should add success notification', () => {
service.success('Operation successful');
expect(service.notifications()).toHaveLength(1);
expect(service.notifications()[0].type).toBe('success');
expect(service.notifications()[0].message).toBe('Operation successful');
});
it('should add error notification', () => {
service.error('Something failed');
expect(service.notifications()).toHaveLength(1);
expect(service.notifications()[0].type).toBe('error');
expect(service.notifications()[0].message).toBe('Something failed');
});
it('should add warning notification', () => {
service.warning('Be careful');
expect(service.notifications()).toHaveLength(1);
expect(service.notifications()[0].type).toBe('warning');
});
it('should add info notification', () => {
service.info('FYI');
expect(service.notifications()).toHaveLength(1);
expect(service.notifications()[0].type).toBe('info');
});
it('should dismiss notification by id', () => {
service.success('Test 1');
service.error('Test 2');
expect(service.notifications()).toHaveLength(2);
service.dismiss(service.notifications()[0].id);
expect(service.notifications()).toHaveLength(1);
expect(service.notifications()[0].message).toBe('Test 2');
});
it('should auto-dismiss after 5 seconds', () => {
service.success('Auto dismiss');
expect(service.notifications()).toHaveLength(1);
vi.advanceTimersByTime(5000);
expect(service.notifications()).toHaveLength(0);
});
it('should assign unique ids', () => {
service.success('First');
service.error('Second');
service.warning('Third');
const ids = service.notifications().map(n => n.id);
const uniqueIds = new Set(ids);
expect(uniqueIds.size).toBe(3);
});
});