| 1 | import { Agent } from '../agent'; |
| 2 | import { platform, ranking } from '../mockData'; |
| 3 | import supertest from 'supertest'; |
| 4 | import { RankingType, RankingStatus } from '../../src/db/schema/ranking.schema'; |
| 5 | |
| 6 | describe('RankingController (e2e)', () => { |
| 7 | let agent: supertest.Agent; |
| 8 | |
| 9 | beforeAll(async () => { |
| 10 | agent = Agent.get(); |
| 11 | }); |
| 12 | |
| 13 | describe('getAllRankings', () => { |
| 14 | it('should return all rankings', async () => { |
| 15 | const result = await agent.get('/ranking').expect(200); |
| 16 | |
| 17 | expect(Array.isArray(result.body.data)).toBe(true); |
| 18 | expect(result.body.data[0]).toMatchObject({ |
| 19 | id: expect.any(String), |
| 20 | name: expect.any(String), |
| 21 | type: expect.any(String), |
| 22 | status: expect.any(Number), |
| 23 | platform: expect.any(Object), |
| 24 | }); |
| 25 | }); |
| 26 | }); |
| 27 | |
| 28 | describe('getRanking', () => { |
| 29 | it('should return ranking by id', async () => { |
| 30 | const result = await agent.get(`/ranking/${ranking._id}`).expect(200); |
| 31 | |
| 32 | expect(result.body.data).toMatchObject({ |
| 33 | id: ranking._id.toString(), |
| 34 | name: ranking.name, |
| 35 | type: ranking.type, |
| 36 | status: ranking.status, |
| 37 | platform: expect.objectContaining({ |
| 38 | id: platform._id.toString(), |
| 39 | }), |
| 40 | }); |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | describe('createRanking', () => { |
| 45 | it('should create new ranking', async () => { |
| 46 | const newRanking = { |
| 47 | name: 'Test Ranking', |
| 48 | platformId: platform._id.toString(), |
| 49 | type: RankingType.DAILY, |
| 50 | description: 'Test Description', |
| 51 | updateFrequency: '每日12点', |
| 52 | }; |
| 53 | |
| 54 | const result = await agent.post('/ranking').send(newRanking).expect(201); |
| 55 | |
| 56 | expect(result.body.data).toMatchObject({ |
| 57 | name: newRanking.name, |
| 58 | type: newRanking.type, |
| 59 | description: newRanking.description, |
| 60 | status: RankingStatus.OPEN, |
| 61 | updateFrequency: newRanking.updateFrequency, |
| 62 | }); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe('updateRanking', () => { |
| 67 | it('should update ranking', async () => { |
| 68 | const updateData = { |
| 69 | name: 'Updated Ranking', |
| 70 | description: 'Updated Description', |
| 71 | platformId: platform._id.toString(), |
| 72 | }; |
| 73 | |
| 74 | const result = await agent |
| 75 | .put(`/ranking/${ranking._id}`) |
| 76 | .send(updateData) |
| 77 | .expect(200); |
| 78 | |
| 79 | expect(result.body.data).toMatchObject({ |
| 80 | id: ranking._id.toString(), |
| 81 | name: updateData.name, |
| 82 | description: updateData.description, |
| 83 | }); |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | describe('deleteRanking', () => { |
| 88 | it('should delete ranking', async () => { |
| 89 | await agent.delete(`/ranking/${ranking._id}`).expect(200); |
| 90 | |
| 91 | const result = await agent.get(`/ranking/${ranking._id}`).expect(200); |
| 92 | expect(result.body.data.status).toBe(RankingStatus.DELETE); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | describe('getRankingsByPlatform', () => { |
| 97 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 98 | let testRanking; |
| 99 | |
| 100 | beforeEach(async () => { |
| 101 | // 创建一个测试榜单 |
| 102 | const newRanking = { |
| 103 | name: 'Test Platform Ranking', |
| 104 | platformId: platform._id.toString(), |
| 105 | type: RankingType.DAILY, |
| 106 | description: 'Test Description', |
| 107 | status: RankingStatus.OPEN, |
| 108 | }; |
| 109 | |
| 110 | const createResult = await agent |
| 111 | .post('/ranking') |
| 112 | .send(newRanking) |
| 113 | .expect(201); |
| 114 | testRanking = createResult.body; |
| 115 | }); |
| 116 | |
| 117 | it('should return rankings by platform', async () => { |
| 118 | const result = await agent |
| 119 | .get('/ranking/platform') |
| 120 | .query({ |
| 121 | platformId: platform._id.toString(), |
| 122 | }) |
| 123 | .expect(200); |
| 124 | |
| 125 | expect(Array.isArray(result.body.data)).toBe(true); |
| 126 | expect(result.body.data[0]).toMatchObject({ |
| 127 | id: expect.any(String), |
| 128 | name: expect.any(String), |
| 129 | type: expect.any(String), |
| 130 | status: RankingStatus.OPEN, |
| 131 | platform: expect.objectContaining({ |
| 132 | id: platform._id.toString(), |
| 133 | }), |
| 134 | }); |
| 135 | }); |
| 136 | |
| 137 | it('should return rankings by platform with specific status', async () => { |
| 138 | const result = await agent |
| 139 | .get('/ranking/platform') |
| 140 | .query({ |
| 141 | platformId: platform._id.toString(), |
| 142 | status: RankingStatus.OPEN, |
| 143 | }) |
| 144 | .expect(200); |
| 145 | |
| 146 | expect(Array.isArray(result.body.data)).toBe(true); |
| 147 | result.body.data.forEach((ranking) => { |
| 148 | expect(ranking.status).toBe(RankingStatus.OPEN); |
| 149 | expect(ranking.platform.id).toBe(platform._id.toString()); |
| 150 | }); |
| 151 | }); |
| 152 | |
| 153 | it('should return empty array when no rankings found', async () => { |
| 154 | const nonExistentPlatformId = '507f1f77bcf86cd799439011'; |
| 155 | const result = await agent |
| 156 | .get('/ranking/platform') |
| 157 | .query({ |
| 158 | platformId: nonExistentPlatformId, |
| 159 | }) |
| 160 | .expect(200); |
| 161 | |
| 162 | expect(result.body.data).toEqual([]); |
| 163 | }); |
| 164 | }); |
| 165 | }); |
| 166 |