Skip to content

Commit

Permalink
[KAN-46] test: TokenExpirationTimerLayout 테스트코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
hakyoung12 committed Oct 15, 2024
1 parent b579e56 commit 4ad520b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/app/components/Gnb/TokenExpirationTimerLayout.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<TokenExpirationTimerLayout token={mockToken} variant='gnb' />);

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(<TokenExpirationTimerLayout token={mockToken} variant='gnb' />);

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(<TokenExpirationTimerLayout token={mockToken} variant='gnb' />);

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(<TokenExpirationTimerLayout token={mockToken} variant='dropdown' />);

expect(screen.getByText('2 : 5')).toBeInTheDocument();
});
});

0 comments on commit 4ad520b

Please sign in to comment.