ym923 | 2a70a62 | 2025-06-09 20:12:13 +0800 | [diff] [blame] | 1 | const axios = require('axios'); |
| 2 | jest.mock('axios'); |
| 3 | |
| 4 | const { |
| 5 | createComplain, |
| 6 | deleteComplain, |
| 7 | updateComplain, |
| 8 | getComplainsByTargetUser, |
| 9 | getComplainsByPostingUser, |
| 10 | getAllComplains, |
| 11 | getComplainDetailById |
| 12 | } = require('../complain'); |
| 13 | |
| 14 | describe('Complain API Tests', () => { |
| 15 | beforeEach(() => { |
| 16 | jest.clearAllMocks(); |
| 17 | }); |
| 18 | |
| 19 | test('createComplain should post complain data', async () => { |
| 20 | const mockData = { data: { complainid: 1 } }; |
| 21 | const complainPayload = { puse: 1, duser: 2, content: 'test', torrentid: 99 }; |
| 22 | axios.post.mockResolvedValue(mockData); |
| 23 | |
| 24 | const response = await createComplain(complainPayload); |
| 25 | |
| 26 | expect(axios.post).toHaveBeenCalledWith( |
| 27 | 'http://localhost:8080/complain/create', |
| 28 | complainPayload |
| 29 | ); |
| 30 | expect(response).toEqual(mockData.data); |
| 31 | }); |
| 32 | |
| 33 | test('deleteComplain should send delete request', async () => { |
| 34 | const mockData = { data: true }; |
| 35 | axios.delete.mockResolvedValue(mockData); |
| 36 | |
| 37 | const response = await deleteComplain(1); |
| 38 | |
| 39 | expect(axios.delete).toHaveBeenCalledWith('http://localhost:8080/complain/delete/1'); |
| 40 | expect(response).toBe(true); |
| 41 | }); |
| 42 | |
| 43 | test('updateComplain should put complain data', async () => { |
| 44 | const complainPayload = { complainid: 1, content: 'updated' }; |
| 45 | const mockData = { data: true }; |
| 46 | axios.put.mockResolvedValue(mockData); |
| 47 | |
| 48 | const response = await updateComplain(complainPayload); |
| 49 | |
| 50 | expect(axios.put).toHaveBeenCalledWith( |
| 51 | 'http://localhost:8080/complain/update', |
| 52 | complainPayload |
| 53 | ); |
| 54 | expect(response).toBe(true); |
| 55 | }); |
| 56 | |
| 57 | test('getComplainsByTargetUser should fetch complains by duser', async () => { |
| 58 | const mockData = { data: [{ complainid: 1, duser: 2 }] }; |
| 59 | axios.get.mockResolvedValue(mockData); |
| 60 | |
| 61 | const response = await getComplainsByTargetUser(2); |
| 62 | |
| 63 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/complain/target/2'); |
| 64 | expect(response).toEqual(mockData.data); |
| 65 | }); |
| 66 | |
| 67 | test('getComplainsByPostingUser should fetch complains by puse', async () => { |
| 68 | const mockData = { data: [{ complainid: 1, puse: 1 }] }; |
| 69 | axios.get.mockResolvedValue(mockData); |
| 70 | |
| 71 | const response = await getComplainsByPostingUser(1); |
| 72 | |
| 73 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/complain/from/1'); |
| 74 | expect(response).toEqual(mockData.data); |
| 75 | }); |
| 76 | |
| 77 | test('getAllComplains should fetch all complains', async () => { |
| 78 | const mockData = { data: [{ complainid: 1 }] }; |
| 79 | axios.get.mockResolvedValue(mockData); |
| 80 | |
| 81 | const response = await getAllComplains(); |
| 82 | |
| 83 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/complain/all'); |
| 84 | expect(response).toEqual(mockData.data); |
| 85 | }); |
| 86 | |
| 87 | test('getComplainDetailById should fetch detailed complain info', async () => { |
| 88 | const mockData = { data: { complainid: 1, puse: 1, duser: 2 } }; |
| 89 | axios.get.mockResolvedValue(mockData); |
| 90 | |
| 91 | const response = await getComplainDetailById(1); |
| 92 | |
| 93 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/complain/detail/1'); |
| 94 | expect(response).toEqual(mockData.data); |
| 95 | }); |
| 96 | }); |