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 { |
| 4 | getRecommendations, |
| 5 | markRecommendationShown, |
| 6 | markRecommendationClicked |
| 7 | } from './recommend'; |
| 8 | |
| 9 | describe('推荐API', () => { |
| 10 | let mockAxios; |
| 11 | |
| 12 | beforeEach(() => { |
| 13 | mockAxios = new MockAdapter(api); |
| 14 | }); |
| 15 | |
| 16 | afterEach(() => { |
| 17 | mockAxios.restore(); |
| 18 | }); |
| 19 | |
| 20 | describe('getRecommendations - 获取推荐内容', () => { |
| 21 | it('应该成功获取推荐列表', async () => { |
| 22 | const mockData = [ |
| 23 | { id: '1', title: '推荐1' }, |
| 24 | { id: '2', title: '推荐2' } |
| 25 | ]; |
| 26 | const limit = 5; |
| 27 | |
| 28 | mockAxios.onGet('/recommend/for-user', { params: { limit } }) |
| 29 | .reply(200, mockData); |
| 30 | |
| 31 | const response = await getRecommendations(limit); |
| 32 | expect(response).toEqual(mockData); |
| 33 | }); |
| 34 | |
| 35 | it('应该在请求失败时抛出错误', async () => { |
| 36 | mockAxios.onGet('/recommend/for-user').networkError(); |
| 37 | |
| 38 | await expect(getRecommendations()).rejects.toThrow(); |
| 39 | }); |
| 40 | }); |
| 41 | |
| 42 | describe('markRecommendationShown - 标记推荐为已显示', () => { |
| 43 | it('应该成功发送标记请求', async () => { |
| 44 | const torrentId = '123'; |
| 45 | mockAxios.onPost(`/recommend/mark-shown/${torrentId}`).reply(200); |
| 46 | |
| 47 | await expect(markRecommendationShown(torrentId)).resolves.not.toThrow(); |
| 48 | }); |
| 49 | |
| 50 | it('应该在请求失败时不抛出错误', async () => { |
| 51 | const torrentId = '456'; |
| 52 | mockAxios.onPost(`/recommend/mark-shown/${torrentId}`).networkError(); |
| 53 | |
| 54 | await expect(markRecommendationShown(torrentId)).resolves.not.toThrow(); |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | describe('markRecommendationClicked - 标记推荐为已点击', () => { |
| 59 | it('应该成功发送标记请求', async () => { |
| 60 | const torrentId = '789'; |
| 61 | mockAxios.onPost(`/recommend/mark-clicked/${torrentId}`).reply(200); |
| 62 | |
| 63 | await expect(markRecommendationClicked(torrentId)).resolves.not.toThrow(); |
| 64 | }); |
| 65 | |
| 66 | it('应该在请求失败时不抛出错误', async () => { |
| 67 | const torrentId = '101'; |
| 68 | mockAxios.onPost(`/recommend/mark-clicked/${torrentId}`).networkError(); |
| 69 | |
| 70 | await expect(markRecommendationClicked(torrentId)).resolves.not.toThrow(); |
| 71 | }); |
| 72 | }); |
| 73 | }); |