blob: a1a553fb931d7369450edf6c86c48f4c97198a26 [file] [log] [blame]
ym92332c8a982025-06-09 20:20:14 +08001// src/api/__tests__/request.test.js
2const axios = require('axios');
3const {
4 createRequest,
5 updateMoney,
6 updateLoaduserByName,
7 updateTorrentid,
8 getTorrentid,
9 deleteRequest,
10 findByName,
11 findByUserid,
12 findByLoaduser,
13 getTotalMoneyByName,
14 getAllRequests,
15 getInfoByRequestId,
16} = require('../request');
17
18jest.mock('axios');
19
20describe('Request API Tests', () => {
21 beforeEach(() => {
22 jest.clearAllMocks();
23 });
24
25 test('createRequest should post form data with correct headers', async () => {
26 const formData = new FormData();
27 formData.append('title', 'Help');
28 axios.post.mockResolvedValue({ data: { success: true } });
29
30 const res = await createRequest(formData);
31
32 expect(axios.post).toHaveBeenCalledWith(
33 'http://localhost:8080/request/create',
34 formData,
35 { headers: { 'Content-Type': 'multipart/form-data' } }
36 );
37 expect(res.data).toEqual({ success: true });
38 });
39
40 test('updateMoney should send PUT request with money param', async () => {
41 axios.put.mockResolvedValue({ data: true });
42
43 const res = await updateMoney(1, 100);
44
45 expect(axios.put).toHaveBeenCalledWith(
46 'http://localhost:8080/request/updateMoney/1',
47 null,
48 { params: { money: 100 } }
49 );
50 expect(res.data).toBe(true);
51 });
52
53 test('updateLoaduserByName should send POST request with params', async () => {
54 axios.post.mockResolvedValue({ data: true });
55
56 const res = await updateLoaduserByName('testname', 10);
57
58 expect(axios.post).toHaveBeenCalledWith(
59 'http://localhost:8080/request/updateLoaduserByName',
60 null,
61 { params: { name: 'testname', loaduser: 10 } }
62 );
63 expect(res.data).toBe(true);
64 });
65
66 test('updateTorrentid should send PUT request with params', async () => {
67 axios.put.mockResolvedValue({ data: true });
68
69 const res = await updateTorrentid(5, 'torrent123');
70
71 expect(axios.put).toHaveBeenCalledWith(
72 'http://localhost:8080/request/updateTorrentid/5',
73 null,
74 { params: { torrentid: 'torrent123' } }
75 );
76 expect(res.data).toBe(true);
77 });
78
79 test('getTorrentid should fetch torrentid successfully', async () => {
80 axios.get.mockResolvedValue({ data: 'torrent123' });
81
82 const res = await getTorrentid(5);
83
84 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/request/getTorrentid/5');
85 expect(res).toBe('torrent123');
86 });
87
88 test('getTorrentid should return null on error', async () => {
89 axios.get.mockRejectedValue(new Error('fail'));
90
91 const res = await getTorrentid(5);
92
93 expect(res).toBeNull();
94 });
95
96 test('deleteRequest should send DELETE request', async () => {
97 axios.delete.mockResolvedValue({ data: true });
98
99 const res = await deleteRequest(3);
100
101 expect(axios.delete).toHaveBeenCalledWith('http://localhost:8080/request/delete/3');
102 expect(res.data).toBe(true);
103 });
104
105 test('findByName should return array on success', async () => {
106 axios.get.mockResolvedValue({ data: [{ id: 1 }] });
107
108 const res = await findByName('Alice');
109
110 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/request/findByName', {
111 params: { name: 'Alice' },
112 });
113 expect(res).toEqual([{ id: 1 }]);
114 });
115
116 test('findByName should return empty array on error', async () => {
117 axios.get.mockRejectedValue(new Error('fail'));
118
119 const res = await findByName('Alice');
120
121 expect(res).toEqual([]);
122 });
123
124 test('findByUserid should return array on success', async () => {
125 axios.get.mockResolvedValue({ data: [{ id: 2 }] });
126
127 const res = await findByUserid(10);
128
129 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/request/findByUserid', {
130 params: { userid: 10 },
131 });
132 expect(res).toEqual([{ id: 2 }]);
133 });
134
135 test('findByUserid should return empty array on error', async () => {
136 axios.get.mockRejectedValue(new Error('fail'));
137
138 const res = await findByUserid(10);
139
140 expect(res).toEqual([]);
141 });
142
143 test('findByLoaduser should return array on success', async () => {
144 axios.get.mockResolvedValue({ data: [{ id: 3 }] });
145
146 const res = await findByLoaduser(20);
147
148 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/request/findByLoaduser', {
149 params: { loaduser: 20 },
150 });
151 expect(res).toEqual([{ id: 3 }]);
152 });
153
154 test('findByLoaduser should return empty array on error', async () => {
155 axios.get.mockRejectedValue(new Error('fail'));
156
157 const res = await findByLoaduser(20);
158
159 expect(res).toEqual([]);
160 });
161
162 test('getTotalMoneyByName should return number on success', async () => {
163 axios.get.mockResolvedValue({ data: 123 });
164
165 const res = await getTotalMoneyByName('Bob');
166
167 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/request/totalMoneyByName', {
168 params: { name: 'Bob' },
169 });
170 expect(res).toBe(123);
171 });
172
173 test('getTotalMoneyByName should return 0 on error or invalid data', async () => {
174 axios.get.mockResolvedValue({ data: 'NaN' });
175
176 const res1 = await getTotalMoneyByName('Bob');
177 expect(res1).toBe(0);
178
179 axios.get.mockRejectedValue(new Error('fail'));
180 const res2 = await getTotalMoneyByName('Bob');
181 expect(res2).toBe(0);
182 });
183
184 test('getAllRequests should return array on success', async () => {
185 axios.get.mockResolvedValue({ data: [{ id: 4 }] });
186
187 const res = await getAllRequests();
188
189 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/request/all');
190 expect(res).toEqual([{ id: 4 }]);
191 });
192
193 test('getAllRequests should return empty array on error', async () => {
194 axios.get.mockRejectedValue(new Error('fail'));
195
196 const res = await getAllRequests();
197
198 expect(res).toEqual([]);
199 });
200
201 test('getInfoByRequestId should return object on success', async () => {
202 axios.get.mockResolvedValue({ data: { torrentid: 'abc', money: 100, loaduser: 10 } });
203
204 const res = await getInfoByRequestId(8);
205
206 expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/request/info/8');
207 expect(res).toEqual({ torrentid: 'abc', money: 100, loaduser: 10 });
208 });
209
210 test('getInfoByRequestId should return empty object on error', async () => {
211 axios.get.mockRejectedValue(new Error('fail'));
212
213 const res = await getInfoByRequestId(8);
214
215 expect(res).toEqual({});
216 });
217});