blob: 89fe48f5e348f000f53eb5de959bac52d2deddda [file] [log] [blame]
ym9234558e9f2025-06-09 20:16:16 +08001const axios = require('axios');
2const MockAdapter = require('axios-mock-adapter');
3
4const {
5 createPost,
6 togglePinPost,
7 deletePost,
8 updatePost,
9 searchPosts,
10 likePost,
11 unlikePost,
12 pinPost,
13 unpinPost,
14 findPostsByUserId,
15 findPinnedPosts,
16 getAllPostsSorted,
17 getPostById
18} = require('../post'); // 注意根据你的实际路径修改
19
20jest.mock('axios');
21
22describe('Post API Tests', () => {
23 beforeEach(() => {
24 jest.clearAllMocks();
25 });
26
27 test('createPost should post form data', async () => {
28 const formData = new FormData();
29 formData.append('userid', '1');
30 formData.append('post_title', 'Test');
31 axios.post.mockResolvedValue({ data: true });
32
33 const response = await createPost(formData);
34
35 expect(axios.post).toHaveBeenCalledWith(
36 'http://localhost:8080/post/create',
37 formData,
38 { headers: { 'Content-Type': 'multipart/form-data' } }
39 );
40 expect(response.data).toBe(true);
41 });
42
43 test('togglePinPost should send PUT request', async () => {
44 axios.put.mockResolvedValue({ data: true });
45
46 const response = await togglePinPost(123);
47
48 expect(axios.put).toHaveBeenCalledWith('http://localhost:8080/post/togglePin/123');
49 expect(response.data).toBe(true);
50 });
51
52 test('deletePost should send DELETE request', async () => {
53 axios.delete.mockResolvedValue({ data: true });
54
55 const response = await deletePost(123);
56
57 expect(axios.delete).toHaveBeenCalledWith('http://localhost:8080/post/delete/123');
58 expect(response.data).toBe(true);
59 });
60
61 test('updatePost should send PUT request with JSON', async () => {
62 const post = { postid: 1, post_title: 'Updated Title' };
63 axios.put.mockResolvedValue({ data: true });
64
65 const response = await updatePost(post);
66
67 expect(axios.put).toHaveBeenCalledWith(
68 'http://localhost:8080/post/update',
69 JSON.stringify(post),
70 { headers: { 'Content-Type': 'application/json' } }
71 );
72 expect(response.data).toBe(true);
73 });
74
75 test('searchPosts should send GET request with keyword', async () => {
76 const mockData = { data: [{ post_title: 'Keyword Match' }] };
77 axios.get.mockResolvedValue(mockData);
78
79 const response = await searchPosts('test');
80
81 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/post/search?keyword=test');
82 expect(response.data).toEqual(mockData.data);
83 });
84
85 test('likePost should send PUT request', async () => {
86 axios.put.mockResolvedValue({ data: true });
87
88 const response = await likePost(1);
89
90 expect(axios.put).toHaveBeenCalledWith('http://localhost:8080/post/like/1');
91 expect(response.data).toBe(true);
92 });
93
94 test('unlikePost should send PUT request', async () => {
95 axios.put.mockResolvedValue({ data: true });
96
97 const response = await unlikePost(1);
98
99 expect(axios.put).toHaveBeenCalledWith('http://localhost:8080/post/unlike/1');
100 expect(response.data).toBe(true);
101 });
102
103 test('pinPost should send PUT request', async () => {
104 axios.put.mockResolvedValue({ data: true });
105
106 const response = await pinPost(1);
107
108 expect(axios.put).toHaveBeenCalledWith('http://localhost:8080/post/pin/1');
109 expect(response.data).toBe(true);
110 });
111
112 test('unpinPost should send PUT request', async () => {
113 axios.put.mockResolvedValue({ data: true });
114
115 const response = await unpinPost(1);
116
117 expect(axios.put).toHaveBeenCalledWith('http://localhost:8080/post/unpin/1');
118 expect(response.data).toBe(true);
119 });
120
121 test('findPostsByUserId should fetch user posts', async () => {
122 const mockData = { data: [{ postid: 1, userid: 2 }] };
123 axios.get.mockResolvedValue(mockData);
124
125 const response = await findPostsByUserId(2);
126
127 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/post/findByUserid?userid=2');
128 expect(response.data).toEqual(mockData.data);
129 });
130
131 test('findPinnedPosts should fetch pinned posts', async () => {
132 const mockData = { data: [{ postid: 1, is_pinned: 1 }] };
133 axios.get.mockResolvedValue(mockData);
134
135 const response = await findPinnedPosts();
136
137 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/post/findPinned');
138 expect(response.data).toEqual(mockData.data);
139 });
140
141 test('getAllPostsSorted should fetch all posts sorted', async () => {
142 const mockData = { data: [{ postid: 1 }] };
143 axios.get.mockResolvedValue(mockData);
144
145 const response = await getAllPostsSorted();
146
147 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/post/all');
148 expect(response.data).toEqual(mockData.data);
149 });
150
151 test('getPostById should fetch post by ID', async () => {
152 const mockData = { data: { postid: 1 } };
153 axios.get.mockResolvedValue(mockData);
154
155 const response = await getPostById(1);
156
157 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/post/get/1');
158 expect(response.data).toEqual(mockData.data);
159 });
160});