fischerX/apps/web/src/lib/__tests__/notification-api.test.ts

99 lines
3.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { notificationApi } from '../notification-api';
vi.mock('../api', () => ({
apiClient: {
get: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
},
}));
import { apiClient } from '../api';
const mockedGet = vi.mocked(apiClient.get);
const mockedPut = vi.mocked(apiClient.put);
const mockedDelete = vi.mocked(apiClient.delete);
describe('notificationApi', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('getNotifications', () => {
it('should call GET /notifications with default params', async () => {
mockedGet.mockResolvedValue({ data: { notifications: [], total: 0 } });
await notificationApi.getNotifications(1, 20);
expect(mockedGet).toHaveBeenCalledWith('/notifications', {
params: { page: 1, limit: 20 },
});
});
it('should call GET /notifications with all filter params', async () => {
mockedGet.mockResolvedValue({ data: { notifications: [], total: 0 } });
await notificationApi.getNotifications(1, 10, 'in-app', 'sent', 'info');
expect(mockedGet).toHaveBeenCalledWith('/notifications', {
params: { page: 1, limit: 10, channel: 'in-app', status: 'sent', type: 'info' },
});
});
});
describe('getUnreadCount', () => {
it('should call GET /notifications/unread-count', async () => {
mockedGet.mockResolvedValue({ data: { count: 5 } });
const result = await notificationApi.getUnreadCount();
expect(mockedGet).toHaveBeenCalledWith('/notifications/unread-count');
expect(result.data).toEqual({ count: 5 });
});
});
describe('getNotification', () => {
it('should call GET /notifications/:id', async () => {
mockedGet.mockResolvedValue({ data: { id: '123', title: 'Test' } });
await notificationApi.getNotification('123');
expect(mockedGet).toHaveBeenCalledWith('/notifications/123');
});
});
describe('markAsRead', () => {
it('should call PUT /notifications/:id/read', async () => {
mockedPut.mockResolvedValue({ data: { id: '123', readAt: '2024-01-01' } });
await notificationApi.markAsRead('123');
expect(mockedPut).toHaveBeenCalledWith('/notifications/123/read');
});
});
describe('markAllAsRead', () => {
it('should call PUT /notifications/read-all', async () => {
mockedPut.mockResolvedValue({ data: { success: true } });
await notificationApi.markAllAsRead();
expect(mockedPut).toHaveBeenCalledWith('/notifications/read-all');
});
});
describe('deleteNotification', () => {
it('should call DELETE /notifications/:id', async () => {
mockedDelete.mockResolvedValue({ data: { success: true } });
await notificationApi.deleteNotification('123');
expect(mockedDelete).toHaveBeenCalledWith('/notifications/123');
});
});
describe('getPreferences', () => {
it('should call GET /notifications/preferences', async () => {
mockedGet.mockResolvedValue({ data: { emailEnabled: true } });
await notificationApi.getPreferences();
expect(mockedGet).toHaveBeenCalledWith('/notifications/preferences');
});
});
describe('updatePreferences', () => {
it('should call PUT /notifications/preferences with data', async () => {
const prefs = { emailEnabled: false, smsEnabled: true };
mockedPut.mockResolvedValue({ data: prefs });
await notificationApi.updatePreferences(prefs);
expect(mockedPut).toHaveBeenCalledWith('/notifications/preferences', prefs);
});
});
});