Skip to content

Commit

Permalink
test: test case 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-hak committed Jun 26, 2024
1 parent 1a34a4b commit 6f6360d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable no-undef */
module.exports = {
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1' // '@' 접두사를 'src' 디렉토리로 매핑
'^@/(.*)$': '<rootDir>/src/$1', // '@' 접두사를 'src' 디렉토리로 매핑
'^@utils/(.*)$': '<rootDir>/src/utils/$1'
},
testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
Expand Down
58 changes: 58 additions & 0 deletions src/components/layout/footer/Footer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import Footer from '@/components/layout/footer/Footer';
import { useRouter } from 'next/navigation';
import { usePathname } from 'next/navigation';

jest.mock('next/navigation', () => ({
useRouter: jest.fn(),
usePathname: jest.fn()
}));

describe('Footer 컴포넌트', () => {
const pushMock = jest.fn();
beforeEach(() => {
(useRouter as jest.Mock).mockImplementation(() => ({
push: pushMock
}));
(usePathname as jest.Mock).mockImplementation(() => '/'); // 기본 경로를 '/'로 설정
sessionStorage.clear();
jest.clearAllMocks();
});

test('정상적으로 렌더링되는지 확인', () => {
render(<Footer />);
expect(screen.getByText('홈')).toBeInTheDocument();
expect(screen.getByText('예약')).toBeInTheDocument();
expect(screen.getByText('내 주변')).toBeInTheDocument();
expect(screen.getByText('커뮤니티')).toBeInTheDocument();
expect(screen.getByText('마이')).toBeInTheDocument();
});

test('각 링크가 올바른 경로로 이동하는지 확인', () => {
render(<Footer />);
expect(screen.getByText('홈').closest('a')).toHaveAttribute('href', '/');
expect(screen.getByText('예약').closest('a')).toHaveAttribute('href', '/reservation');
expect(screen.getByText('내 주변').closest('a')).toHaveAttribute('href', '/map');
expect(screen.getByText('마이').closest('a')).toHaveAttribute('href', '/mypage');
});

test('현재 경로에 따라 활성 링크 스타일이 적용되는지 확인', () => {
(usePathname as jest.Mock).mockImplementation(() => '/community');
render(<Footer />);
expect(screen.getByAltText('community')).toHaveAttribute(
'src',
'/CheckedCommunity.svg'
);
});

test('커뮤니티 클릭 시 세션 스토리지에서 데이터가 제거되고, /community로 이동하는지 확인', () => {
sessionStorage.setItem('scrollPosition', '123');
sessionStorage.setItem('savedData', 'data');
render(<Footer />);
fireEvent.click(screen.getByText('커뮤니티'));
expect(sessionStorage.getItem('scrollPosition')).toBeNull();
expect(sessionStorage.getItem('savedData')).toBeNull();
expect(pushMock).toHaveBeenCalledWith('/community');
});
});
8 changes: 4 additions & 4 deletions src/components/layout/footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import deleteFirstWord from '@/utils/deleteFirtstWord';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useRouter } from 'next/router';

import { useRouter } from 'next/navigation';
import Image from 'next/image';
const Footer = () => {
const router = useRouter();
const pathName = usePathname();
Expand All @@ -21,9 +21,9 @@ const Footer = () => {
<div className=" h-[44.84px] flex-col justify-start items-center gap-2.5 inline-flex">
<div className="w-[22.06px] h-[21.82px] flex-col justify-center items-center">
{filteredPathName === '' ? (
<img className="w-5 h-5 " src="/CheckedHome.svg" alt="home" />
<Image src="/CheckedHome.svg" alt="home" width={20} height={20} />
) : (
<img className="w-5 h-5 " src="/Home.svg" alt="home" />
<Image src="/Home.svg" alt="home" width={20} height={20} />
)}
</div>
<div className="text-center text-black text-xs font-normal font-['Pretendard']">
Expand Down
9 changes: 9 additions & 0 deletions src/components/shared/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { render } from '@testing-library/react';

import Button from './Button';

describe('Button', () => {
test('renders the Button component', () => {
render(<Button label="Hello world!" />);
});
});
9 changes: 9 additions & 0 deletions src/components/shared/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface ButtonProps {
label: string;
}

const Button = (props: ButtonProps) => {
return <button>{props.label}</button>;
};

export default Button;

0 comments on commit 6f6360d

Please sign in to comment.