Skip to content

Commit 065879c

Browse files
committed
refactor: repository 테스트 코드 메서드 분리
1 parent f6f3936 commit 065879c

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/repositories/__test__/qr.repo.test.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DBError } from '@/exception';
33
import { Pool } from 'pg';
44

55
const mockPool: Partial<Pool> = {
6-
query: jest.fn()
6+
query: jest.fn(),
77
};
88

99
describe('QRLoginTokenRepository', () => {
@@ -17,40 +17,46 @@ describe('QRLoginTokenRepository', () => {
1717
jest.clearAllMocks();
1818
});
1919

20-
it('should insert QR login token', async () => {
21-
(mockPool.query as jest.Mock).mockResolvedValueOnce(undefined);
20+
describe('createQRLoginToken', () => {
21+
it('QR 토큰을 성공적으로 삽입해야 한다', async () => {
22+
(mockPool.query as jest.Mock).mockResolvedValueOnce(undefined);
2223

23-
await expect(
24-
repo.createQRLoginToken('token', 1, 'ip', 'agent')
25-
).resolves.not.toThrow();
24+
await expect(
25+
repo.createQRLoginToken('token', 1, 'ip', 'agent')
26+
).resolves.not.toThrow();
2627

27-
expect(mockPool.query).toHaveBeenCalled();
28-
});
28+
expect(mockPool.query).toHaveBeenCalled();
29+
});
2930

30-
it('should throw DBError on insert failure', async () => {
31-
(mockPool.query as jest.Mock).mockRejectedValueOnce(new Error('fail'));
31+
it('삽입 중 오류 발생 시 DBError를 던져야 한다', async () => {
32+
(mockPool.query as jest.Mock).mockRejectedValueOnce(new Error('fail'));
3233

33-
await expect(repo.createQRLoginToken('token', 1, 'ip', 'agent'))
34-
.rejects.toThrow(DBError);
34+
await expect(
35+
repo.createQRLoginToken('token', 1, 'ip', 'agent')
36+
).rejects.toThrow(DBError);
37+
});
3538
});
3639

37-
it('should return token if found', async () => {
38-
(mockPool.query as jest.Mock).mockResolvedValueOnce({ rows: [{ token: 'token' }] });
40+
describe('findQRLoginToken', () => {
41+
it('토큰이 존재할 경우 반환해야 한다', async () => {
42+
const mockTokenData = { token: 'token', user: 1 };
43+
(mockPool.query as jest.Mock).mockResolvedValueOnce({ rows: [mockTokenData] });
3944

40-
const result = await repo.findQRLoginToken('token');
41-
expect(result).toEqual({ token: 'token' });
42-
});
45+
const result = await repo.findQRLoginToken('token');
46+
expect(result).toEqual(mockTokenData);
47+
});
4348

44-
it('should return null if token not found', async () => {
45-
(mockPool.query as jest.Mock).mockResolvedValueOnce({ rows: [] });
49+
it('토큰이 존재하지 않으면 null을 반환해야 한다', async () => {
50+
(mockPool.query as jest.Mock).mockResolvedValueOnce({ rows: [] });
4651

47-
const result = await repo.findQRLoginToken('token');
48-
expect(result).toBeNull();
49-
});
52+
const result = await repo.findQRLoginToken('token');
53+
expect(result).toBeNull();
54+
});
5055

51-
it('should throw DBError on select failure', async () => {
52-
(mockPool.query as jest.Mock).mockRejectedValueOnce(new Error('fail'));
56+
it('조회 중 오류 발생 시 DBError를 던져야 한다', async () => {
57+
(mockPool.query as jest.Mock).mockRejectedValueOnce(new Error('fail'));
5358

54-
await expect(repo.findQRLoginToken('token')).rejects.toThrow(DBError);
59+
await expect(repo.findQRLoginToken('token')).rejects.toThrow(DBError);
60+
});
5561
});
56-
});
62+
});

0 commit comments

Comments
 (0)