From 4ad520bc50ce54b89d15cb053c29f90446420f83 Mon Sep 17 00:00:00 2001 From: hakyoung12 Date: Tue, 15 Oct 2024 11:41:17 +0900 Subject: [PATCH] =?UTF-8?q?[KAN-46]=20test:=20TokenExpirationTimerLayout?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Gnb/TokenExpirationTimerLayout.test.tsx | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/app/components/Gnb/TokenExpirationTimerLayout.test.tsx diff --git a/src/app/components/Gnb/TokenExpirationTimerLayout.test.tsx b/src/app/components/Gnb/TokenExpirationTimerLayout.test.tsx new file mode 100644 index 00000000..8dc75f5b --- /dev/null +++ b/src/app/components/Gnb/TokenExpirationTimerLayout.test.tsx @@ -0,0 +1,66 @@ +import { render, screen } from '@testing-library/react'; +import TokenExpirationTimerLayout from './TokenExpirationTimerLayout'; +import { TokenExpirationTimer } from '@/utils/TokenExpirationTimer'; +import '@testing-library/jest-dom'; + +// TokenExpirationTimer 모킹 +jest.mock('@/utils/TokenExpirationTimer', () => ({ + TokenExpirationTimer: jest.fn(), +})); + +describe('TokenExpirationTimerLayout 컴포넌트', () => { + const mockToken = 'test-token'; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + // 사용자가 로그인하지 않은 경우 아무것도 렌더링하지 않아야 함 + it('should render nothing if user is not logged in', () => { + (TokenExpirationTimer as jest.Mock).mockReturnValue({ + isLoggedIn: false, + timeLeft: 300, + }); + + render(); + + expect(screen.queryByText(/남은 시간:/)).not.toBeInTheDocument(); + }); + + // 남은 시간이 있는 경우 남은 시간을 표시 + it('should display remaining time when user is logged in and time left is available', () => { + (TokenExpirationTimer as jest.Mock).mockReturnValue({ + isLoggedIn: true, + timeLeft: 120, + }); + + render(); + + expect(screen.getByText(/남은 시간:/)).toBeInTheDocument(); + expect(screen.getByText('남은 시간: 2분 0초')).toBeInTheDocument(); + }); + + // 남은 시간이 0일 경우 아무것도 렌더링하지 않아야 함 + it('should render nothing when time left is 0', () => { + (TokenExpirationTimer as jest.Mock).mockReturnValue({ + isLoggedIn: true, + timeLeft: 0, + }); + + render(); + + expect(screen.queryByText(/남은 시간:/)).not.toBeInTheDocument(); + }); + + // variant가 'dropdown'인 경우에도 남은 시간을 표시 + it('should display remaining time in dropdown format', () => { + (TokenExpirationTimer as jest.Mock).mockReturnValue({ + isLoggedIn: true, + timeLeft: 125, + }); + + render(); + + expect(screen.getByText('2 : 5')).toBeInTheDocument(); + }); +});