DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 1 | import MockAdapter from 'axios-mock-adapter'; |
| 2 | import { api } from './auth'; |
| 3 | import { getAnnouncements, getLatestAnnouncements, getAnnouncementDetail,postAnnouncement } from './announcement'; |
| 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('postAnnouncement - 发布公告', () => { |
| 17 | it('应该成功发布公告', async () => { |
| 18 | const mockData = { |
| 19 | title: '测试公告', |
| 20 | content: '测试内容' |
| 21 | }; |
| 22 | |
| 23 | const mockResponse = { |
| 24 | code: 200, |
| 25 | message: '公告发布成功', |
| 26 | data: { |
| 27 | id: 123, |
| 28 | title: mockData.title, |
| 29 | content: mockData.content, |
| 30 | createTime: new Date().toISOString() |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | mockAxios.onPost('/announcement/create').reply(200, mockResponse); |
| 36 | |
| 37 | const response = await postAnnouncement(mockData); |
| 38 | expect(response.data).toEqual(mockResponse); |
| 39 | expect(response.data.code).toBe(200); |
| 40 | expect(response.data.data.title).toBe(mockData.title); |
| 41 | expect(response.data.data.content).toBe(mockData.content); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe('getAnnouncements - 获取所有公告', () => { |
| 46 | it('应该成功获取公告列表', async () => { |
DREW | 5b1883e | 2025-06-07 10:41:32 +0800 | [diff] [blame] | 47 | const mockAnnouncements = [ |
| 48 | { id: 1, title: '公告1', content: '内容1', createTime: '2023-01-01T00:00:00Z' }, |
| 49 | { id: 2, title: '公告2', content: '内容2', createTime: '2023-01-01T00:00:00Z' } |
| 50 | ]; |
| 51 | |
| 52 | mockAxios.onGet('/announcement/list').reply(200, { |
| 53 | code: 200, |
| 54 | data: { |
| 55 | announcements: mockAnnouncements |
| 56 | } |
| 57 | }); |
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 58 | |
DREW | 5b1883e | 2025-06-07 10:41:32 +0800 | [diff] [blame] | 59 | const announcements = await getAnnouncements(); |
| 60 | expect(announcements).toEqual(mockAnnouncements); // 检查返回的数组 |
| 61 | expect(announcements).toHaveLength(2); |
| 62 | }); |
DREW | ae420b2 | 2025-06-02 14:07:20 +0800 | [diff] [blame] | 63 | |
| 64 | |
| 65 | }); |
| 66 | |
| 67 | describe('getLatestAnnouncements - 获取最新公告', () => { |
| 68 | it('应该成功获取最新公告', async () => { |
| 69 | const mockResponse = { |
| 70 | code: 200, |
| 71 | data: { |
| 72 | announcements: [ |
| 73 | { id: 1, title: '最新公告', content: '最新内容' } |
| 74 | ] |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | mockAxios.onGet('/announcement/latest').reply(200, mockResponse); |
| 79 | |
| 80 | const response = await getLatestAnnouncements(); |
| 81 | expect(response.data).toEqual(mockResponse); |
| 82 | expect(response.data.data.announcements).toHaveLength(1); |
| 83 | }); |
| 84 | |
| 85 | it('应该正确处理服务器错误', async () => { |
| 86 | mockAxios.onGet('/announcement/latest').reply(500); |
| 87 | |
| 88 | await expect(getLatestAnnouncements()).rejects.toThrow(); |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | describe('getAnnouncementDetail - 获取公告详情', () => { |
| 93 | it('应该成功获取公告详情', async () => { |
| 94 | const announcementId = 123; |
| 95 | const mockResponse = { |
| 96 | code: 200, |
| 97 | data: { |
| 98 | announcement: { |
| 99 | id: announcementId, |
| 100 | title: '详细公告', |
| 101 | content: '详细内容', |
| 102 | createTime: '2023-01-01T00:00:00Z' |
| 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | mockAxios.onGet(`/announcement/${announcementId}`).reply(200, mockResponse); |
| 108 | |
| 109 | const response = await getAnnouncementDetail(announcementId); |
| 110 | expect(response.data).toEqual(mockResponse); |
| 111 | expect(response.data.data.announcement.id).toBe(announcementId); |
| 112 | }); |
| 113 | |
| 114 | it('应该处理公告不存在的情况', async () => { |
| 115 | const announcementId = 999; |
| 116 | const mockResponse = { |
| 117 | code: 404, |
| 118 | message: '公告不存在' |
| 119 | }; |
| 120 | |
| 121 | mockAxios.onGet(`/announcement/${announcementId}`).reply(404, mockResponse); |
| 122 | |
| 123 | try { |
| 124 | await getAnnouncementDetail(announcementId); |
| 125 | } catch (error) { |
| 126 | expect(error.response.status).toBe(404); |
| 127 | expect(error.response.data.code).toBe(404); |
| 128 | expect(error.response.data.message).toBe('公告不存在'); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | it('应该正确处理网络错误', async () => { |
| 133 | const announcementId = 456; |
| 134 | mockAxios.onGet(`/announcement/${announcementId}`).networkError(); |
| 135 | |
| 136 | await expect(getAnnouncementDetail(announcementId)).rejects.toThrow(); |
| 137 | }); |
| 138 | }); |
| 139 | }); |