DREW | 5b1883e | 2025-06-07 10:41:32 +0800 | [diff] [blame^] | 1 | import MockAdapter from 'axios-mock-adapter'; |
| 2 | import { api } from './auth'; |
| 3 | import { notificationApi } from './notification'; |
| 4 | |
| 5 | describe('通知API', () => { |
| 6 | let mockAxios; |
| 7 | |
| 8 | beforeEach(() => { |
| 9 | mockAxios = new MockAdapter(api); |
| 10 | }); |
| 11 | |
| 12 | afterEach(() => { |
| 13 | mockAxios.restore(); |
| 14 | }); |
| 15 | |
| 16 | describe('getNotifications - 获取通知列表', () => { |
| 17 | it('应该成功获取用户通知列表', async () => { |
| 18 | const userId = 'user123'; |
| 19 | const mockData = [ |
| 20 | { id: '1', message: '通知1', read: false }, |
| 21 | { id: '2', message: '通知2', read: true } |
| 22 | ]; |
| 23 | |
| 24 | mockAxios.onGet('/api/notifications', { params: { userId } }) |
| 25 | .reply(200, mockData); |
| 26 | |
| 27 | const response = await notificationApi.getNotifications(userId); |
| 28 | expect(response).toEqual(mockData); |
| 29 | }); |
| 30 | |
| 31 | |
| 32 | }); |
| 33 | |
| 34 | describe('markNotificationAsRead - 标记通知为已读', () => { |
| 35 | it('应该成功发送标记请求', async () => { |
| 36 | const notificationId = 'notif123'; |
| 37 | const mockResponse = { success: true }; |
| 38 | |
| 39 | mockAxios.onPost(`/api/notifications/${notificationId}/read`) |
| 40 | .reply(200, mockResponse); |
| 41 | |
| 42 | const response = await notificationApi.markNotificationAsRead(notificationId); |
| 43 | expect(response).toEqual(mockResponse); |
| 44 | }); |
| 45 | |
| 46 | |
| 47 | }); |
| 48 | }); |