22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame] | 1 | import axios from 'axios'; |
| 2 | import MockAdapter from 'axios-mock-adapter'; |
| 3 | import { |
| 4 | getAllUsers, |
| 5 | searchUsers, |
| 6 | updateUserAuthority, |
| 7 | getAllDiscounts, |
| 8 | getCurrentDiscount, |
| 9 | addDiscount, |
| 10 | deleteDiscount |
| 11 | } from './administer'; |
| 12 | |
| 13 | describe('Administer API', () => { |
| 14 | let mock; |
| 15 | |
| 16 | beforeAll(() => { |
| 17 | mock = new MockAdapter(axios); |
| 18 | localStorage.setItem('token', 'test-token'); |
| 19 | }); |
| 20 | |
| 21 | afterEach(() => { |
| 22 | mock.reset(); |
| 23 | }); |
| 24 | |
| 25 | describe('getAllUsers', () => { |
| 26 | it('should fetch all users successfully', async () => { |
| 27 | const mockUsers = [ |
| 28 | { |
| 29 | username: 'user1', |
| 30 | authority: 'USER', |
| 31 | registTime: '2023-01-01', |
| 32 | lastLogin: '2023-05-01', |
| 33 | upload: 1000, |
| 34 | download: 500, |
| 35 | shareRate: 2.0, |
| 36 | magicPoints: 100 |
| 37 | } |
| 38 | ]; |
| 39 | |
| 40 | mock.onGet('http://localhost:8088/user/allUser').reply(200, { |
| 41 | code: 200, |
| 42 | data: { data: mockUsers } |
| 43 | }); |
| 44 | |
| 45 | const result = await getAllUsers(); |
| 46 | expect(result).toEqual(mockUsers); |
| 47 | }); |
| 48 | |
| 49 | it('should return empty array when no users', async () => { |
| 50 | mock.onGet('http://localhost:8088/user/allUser').reply(200, { |
| 51 | code: 200, |
| 52 | data: { data: [] } |
| 53 | }); |
| 54 | |
| 55 | const result = await getAllUsers(); |
| 56 | expect(result).toEqual([]); |
| 57 | }); |
| 58 | |
| 59 | it('should handle error when fetching users', async () => { |
| 60 | mock.onGet('http://localhost:8088/user/allUser').reply(500, { |
| 61 | message: 'Request failed with status code 500' |
| 62 | }); |
| 63 | |
| 64 | await expect(getAllUsers()).rejects.toThrow('Request failed with status code 500'); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | describe('searchUsers', () => { |
| 69 | it('should search users successfully', async () => { |
| 70 | const mockUsers = [ |
| 71 | { |
| 72 | username: 'user1', |
| 73 | authority: 'USER' |
| 74 | } |
| 75 | ]; |
| 76 | |
| 77 | mock.onGet('http://localhost:8088/user/searchUser', { params: { key: 'user' } }) |
| 78 | .reply(200, { |
| 79 | code: 200, |
| 80 | data: { data: mockUsers } |
| 81 | }); |
| 82 | |
| 83 | const result = await searchUsers('user'); |
| 84 | expect(result).toEqual(mockUsers); |
| 85 | }); |
| 86 | |
| 87 | it('should handle empty search key', async () => { |
| 88 | const mockUsers = [ |
| 89 | { |
| 90 | username: 'user1', |
| 91 | authority: 'USER' |
| 92 | } |
| 93 | ]; |
| 94 | |
| 95 | mock.onGet('http://localhost:8088/user/searchUser', { params: { key: '' } }) |
| 96 | .reply(200, { |
| 97 | code: 200, |
| 98 | data: { data: mockUsers } |
| 99 | }); |
| 100 | |
| 101 | const result = await searchUsers(''); |
| 102 | expect(result).toEqual(mockUsers); |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | describe('updateUserAuthority', () => { |
| 107 | it('should update user authority successfully', async () => { |
| 108 | const username = 'user1'; |
| 109 | const authority = 'ADMIN'; |
| 110 | |
| 111 | mock.onPut('http://localhost:8088/user/changeAuthority', { |
| 112 | changeUsername: username, |
| 113 | authority: authority |
| 114 | }).reply(200, { |
| 115 | code: 200, |
| 116 | message: '修改用户权限成功' |
| 117 | }); |
| 118 | |
| 119 | const result = await updateUserAuthority(username, authority); |
| 120 | expect(result.code).toBe(200); |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | describe('getAllDiscounts', () => { |
| 125 | it('should fetch all discounts successfully', async () => { |
| 126 | const mockDiscounts = [ |
| 127 | { |
| 128 | id: 1, |
| 129 | name: '五一活动', |
| 130 | discountType: 'FREE', |
| 131 | startTime: '2023-05-01T00:00:00', |
| 132 | endTime: '2023-05-07T23:59:59' |
| 133 | } |
| 134 | ]; |
| 135 | |
| 136 | mock.onGet('http://localhost:8088/discount/all').reply(200, { |
| 137 | code: 200, |
| 138 | data: { data: mockDiscounts } |
| 139 | }); |
| 140 | |
| 141 | const result = await getAllDiscounts(); |
| 142 | expect(result).toEqual(mockDiscounts); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | describe('getCurrentDiscount', () => { |
| 147 | it('should fetch current discount successfully', async () => { |
| 148 | const mockDiscount = { |
| 149 | id: 1, |
| 150 | name: '当前活动', |
| 151 | discountType: 'HALF', |
| 152 | startTime: '2023-05-01T00:00:00', |
| 153 | endTime: '2023-05-07T23:59:59' |
| 154 | }; |
| 155 | |
| 156 | mock.onGet('http://localhost:8088/discount/current').reply(200, { |
| 157 | code: 200, |
| 158 | data: { data: mockDiscount } |
| 159 | }); |
| 160 | |
| 161 | const result = await getCurrentDiscount(); |
| 162 | expect(result).toEqual(mockDiscount); |
| 163 | }); |
| 164 | |
| 165 | it('should return null when no current discount', async () => { |
| 166 | mock.onGet('http://localhost:8088/discount/current').reply(200, { |
| 167 | code: 200, |
| 168 | message: '目前没有进行中的折扣', |
| 169 | data: null |
| 170 | }); |
| 171 | |
| 172 | const result = await getCurrentDiscount(); |
| 173 | expect(result).toBeNull(); |
| 174 | }); |
| 175 | |
| 176 | it('should handle error when fetching current discount', async () => { |
| 177 | mock.onGet('http://localhost:8088/discount/current').reply(500); |
| 178 | |
| 179 | await expect(getCurrentDiscount()).rejects.toThrow(); |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | describe('addDiscount', () => { |
| 184 | it('should add new discount successfully', async () => { |
| 185 | const newDiscount = { |
| 186 | name: '新活动', |
| 187 | discountType: 'DOUBLE', |
| 188 | startTime: '2023-06-01T00:00:00', |
| 189 | endTime: '2023-06-07T23:59:59' |
| 190 | }; |
| 191 | |
| 192 | mock.onPost('http://localhost:8088/discount/add', newDiscount).reply(200, { |
| 193 | code: 200, |
| 194 | data: { data: { id: 2, ...newDiscount } } |
| 195 | }); |
| 196 | |
| 197 | const result = await addDiscount(newDiscount); |
| 198 | expect(result.id).toBe(2); |
| 199 | }); |
| 200 | }); |
| 201 | |
| 202 | describe('deleteDiscount', () => { |
| 203 | it('should delete discount successfully', async () => { |
| 204 | const discountId = 1; |
| 205 | |
| 206 | mock.onDelete(`http://localhost:8088/discount/delete/${discountId}`).reply(200, { |
| 207 | code: 200, |
| 208 | message: '删除成功' |
| 209 | }); |
| 210 | |
| 211 | const result = await deleteDiscount(discountId); |
| 212 | expect(result).toBe(true); |
| 213 | }); |
| 214 | }); |
| 215 | }); |