ym923 | ed3de70 | 2025-06-09 20:10:16 +0800 | [diff] [blame] | 1 | // src/api/__tests__/comment.test.js |
| 2 | const axios = require('axios'); |
| 3 | jest.mock('axios'); |
| 4 | |
| 5 | const { |
| 6 | createComment, |
| 7 | deleteComment, |
| 8 | updateComment, |
| 9 | getCommentsByPostId, |
| 10 | likeComment, |
| 11 | unlikeComment |
| 12 | } = require('../comment'); |
| 13 | |
| 14 | describe('Comment API Tests', () => { |
| 15 | beforeEach(() => { |
| 16 | jest.clearAllMocks(); |
| 17 | }); |
| 18 | |
| 19 | test('createComment should send POST request', async () => { |
| 20 | const mockData = { userid: 1, postid: 2, postCommentcontent: 'Test comment' }; |
| 21 | axios.post.mockResolvedValue({ data: mockData }); |
| 22 | |
| 23 | const response = await createComment(mockData); |
| 24 | |
| 25 | expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/comment/create', mockData); |
| 26 | expect(response.data).toEqual(mockData); |
| 27 | }); |
| 28 | |
| 29 | test('deleteComment should send DELETE request', async () => { |
| 30 | axios.delete.mockResolvedValue({ data: true }); |
| 31 | |
| 32 | const response = await deleteComment(123); |
| 33 | |
| 34 | expect(axios.delete).toHaveBeenCalledWith('http://localhost:8080/comment/delete/123'); |
| 35 | expect(response.data).toBe(true); |
| 36 | }); |
| 37 | |
| 38 | test('updateComment should send PUT request', async () => { |
| 39 | const updatedData = { commentid: 1, postCommentcontent: 'Updated comment' }; |
| 40 | axios.put.mockResolvedValue({ data: true }); |
| 41 | |
| 42 | const response = await updateComment(updatedData); |
| 43 | |
| 44 | expect(axios.put).toHaveBeenCalledWith('http://localhost:8080/comment/update', updatedData); |
| 45 | expect(response.data).toBe(true); |
| 46 | }); |
| 47 | |
| 48 | test('getCommentsByPostId should fetch comments by post ID', async () => { |
| 49 | const mockResponse = { data: [{ commentid: 1, postCommentcontent: 'Nice!' }] }; |
| 50 | axios.get.mockResolvedValue(mockResponse); |
| 51 | |
| 52 | const response = await getCommentsByPostId(5); |
| 53 | |
| 54 | expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/comment/post/5'); |
| 55 | expect(response).toEqual(mockResponse.data); |
| 56 | }); |
| 57 | |
| 58 | test('getCommentsByPostId should return empty array on error', async () => { |
| 59 | axios.get.mockRejectedValue(new Error('Network Error')); |
| 60 | |
| 61 | const response = await getCommentsByPostId(99); |
| 62 | |
| 63 | expect(response).toEqual([]); |
| 64 | }); |
| 65 | |
| 66 | test('likeComment should send POST request', async () => { |
| 67 | axios.post.mockResolvedValue({ data: true }); |
| 68 | |
| 69 | const response = await likeComment(10); |
| 70 | |
| 71 | expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/comment/like/10'); |
| 72 | expect(response.data).toBe(true); |
| 73 | }); |
| 74 | |
| 75 | test('unlikeComment should send POST request', async () => { |
| 76 | axios.post.mockResolvedValue({ data: true }); |
| 77 | |
| 78 | const response = await unlikeComment(10); |
| 79 | |
| 80 | expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/comment/unlike/10'); |
| 81 | expect(response.data).toBe(true); |
| 82 | }); |
| 83 | }); |