|
1 | 1 | import { Pool, QueryResult } from 'pg'; |
2 | 2 | import { DBError } from '@/exception'; |
3 | 3 | import { LeaderboardRepository } from '@/repositories/leaderboard.repository'; |
| 4 | +import { UserLeaderboardSortType, PostLeaderboardSortType } from '@/types'; |
4 | 5 |
|
5 | 6 | jest.mock('pg'); |
6 | 7 |
|
@@ -60,31 +61,19 @@ describe('LeaderboardRepository', () => { |
60 | 61 | expect(result).toEqual(mockResult); |
61 | 62 | }); |
62 | 63 |
|
63 | | - it('sort가 viewCount인 경우 view_diff 필드를 기준으로 내림차순 정렬해야 한다', async () => { |
64 | | - await repo.getUserLeaderboard('viewCount', 30, 10); |
65 | | - |
66 | | - expect(mockPool.query).toHaveBeenCalledWith( |
67 | | - expect.stringContaining('ORDER BY view_diff DESC'), |
68 | | - expect.anything(), |
69 | | - ); |
70 | | - }); |
71 | | - |
72 | | - it('sort가 likeCount인 경우 like_diff 필드를 기준으로 내림차순 정렬해야 한다', async () => { |
73 | | - await repo.getUserLeaderboard('likeCount', 30, 10); |
74 | | - |
75 | | - expect(mockPool.query).toHaveBeenCalledWith( |
76 | | - expect.stringContaining('ORDER BY like_diff DESC'), |
77 | | - expect.anything(), |
78 | | - ); |
79 | | - }); |
80 | | - |
81 | | - it('sort가 postCount인 경우 post_diff 필드를 기준으로 내림차순 정렬해야 한다', async () => { |
82 | | - await repo.getUserLeaderboard('postCount', 30, 10); |
83 | | - |
84 | | - expect(mockPool.query).toHaveBeenCalledWith( |
85 | | - expect.stringContaining('ORDER BY post_diff DESC'), |
86 | | - expect.anything(), |
87 | | - ); |
| 64 | + describe.each([ |
| 65 | + { sort: 'viewCount', field: 'view_diff' }, |
| 66 | + { sort: 'likeCount', field: 'like_diff' }, |
| 67 | + { sort: 'postCount', field: 'post_diff' }, |
| 68 | + ])('sort 파라미터에 따라 내림차순 정렬되어야 한다', ({ sort, field }) => { |
| 69 | + it(`sort가 ${sort}인 경우 ${field} 필드를 기준으로 정렬해야 한다`, async () => { |
| 70 | + await repo.getUserLeaderboard(sort as UserLeaderboardSortType, 30, 10); |
| 71 | + |
| 72 | + expect(mockPool.query).toHaveBeenCalledWith( |
| 73 | + expect.stringContaining(`ORDER BY ${field} DESC`), |
| 74 | + expect.anything(), |
| 75 | + ); |
| 76 | + }); |
88 | 77 | }); |
89 | 78 |
|
90 | 79 | it('limit 파라미터가 쿼리에 올바르게 적용되어야 한다', async () => { |
@@ -148,22 +137,18 @@ describe('LeaderboardRepository', () => { |
148 | 137 | expect(mockPool.query).toHaveBeenCalledWith(expect.stringContaining('FROM posts_post p'), expect.anything()); |
149 | 138 | }); |
150 | 139 |
|
151 | | - it('sort가 viewCount인 경우 view_diff 필드를 기준으로 내림차순 정렬해야 한다', async () => { |
152 | | - await repo.getPostLeaderboard('viewCount', 30, 10); |
153 | | - |
154 | | - expect(mockPool.query).toHaveBeenCalledWith( |
155 | | - expect.stringContaining('ORDER BY view_diff DESC'), |
156 | | - expect.anything(), |
157 | | - ); |
158 | | - }); |
159 | | - |
160 | | - it('sort가 likeCount인 경우 like_diff 필드를 기준으로 내림차순 정렬해야 한다', async () => { |
161 | | - await repo.getPostLeaderboard('likeCount', 30, 10); |
162 | | - |
163 | | - expect(mockPool.query).toHaveBeenCalledWith( |
164 | | - expect.stringContaining('ORDER BY like_diff DESC'), |
165 | | - expect.anything(), |
166 | | - ); |
| 140 | + describe.each([ |
| 141 | + { sort: 'viewCount', field: 'view_diff' }, |
| 142 | + { sort: 'likeCount', field: 'like_diff' }, |
| 143 | + ])('sort 파라미터에 따라 내림차순 정렬되어야 한다', ({ sort, field }) => { |
| 144 | + it(`sort가 ${sort}인 경우 ${field} 필드를 기준으로 정렬해야 한다`, async () => { |
| 145 | + await repo.getPostLeaderboard(sort as PostLeaderboardSortType, 30, 10); |
| 146 | + |
| 147 | + expect(mockPool.query).toHaveBeenCalledWith( |
| 148 | + expect.stringContaining(`ORDER BY ${field} DESC`), |
| 149 | + expect.anything(), |
| 150 | + ); |
| 151 | + }); |
167 | 152 | }); |
168 | 153 |
|
169 | 154 | it('limit 파라미터가 쿼리에 올바르게 적용되어야 한다', async () => { |
|
0 commit comments