Skip to content

Commit

Permalink
Refactor: 상수 컨벤션, MemoryRouter, screen, userEvent 사용해서 테스트코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hwinkr committed Apr 7, 2023
1 parent 40d95cd commit 48394df
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/components/Card/InformCard/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
import ThemeProvider from '@styles/ThemeProvider';
import { fireEvent, render } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter } from 'react-router-dom';

import '@testing-library/jest-dom';
import InformCard from './index';

// 3. memoryRouter, 가상 라우터

describe('Card 클릭 시 페이지 이동이 잘 되는지에 대한 테스트', () => {
it('공지사항 클릭 테스트', () => {
const icon = 'notification';
const title = '공지사항';
const path = 'announcement';
const testComponent = render(
const ICON = 'notification';
const TITLE = '공지사항';
const PATH = 'announcement';
render(
<ThemeProvider>
<BrowserRouter>
<InformCard icon={icon} title={title} path={path} />,
</BrowserRouter>
<InformCard icon={ICON} title={TITLE} path={PATH} />
</ThemeProvider>,
{ wrapper: MemoryRouter },
);

const card = testComponent.container.querySelector('div');
if (card) {
fireEvent.click(card);
expect(window.location.pathname).toBe(`/${path}`);
}
const card = screen.getByTestId('card');

userEvent.click(card);
waitFor(() => {
expect(window.location.pathname).toBe(`/${PATH}`);
});
});

it('졸업요건 클릭 테스트', () => {
const icon = 'school';
const title = '졸업요건';
const path = 'announcement';
const testComponent = render(
const ICON = 'school';
const TITLE = '졸업요건';
const PATH = 'graduation-requirements';
render(
<ThemeProvider>
<BrowserRouter>
<InformCard icon={icon} title={title} path={path} />,
</BrowserRouter>
<InformCard icon={ICON} title={TITLE} path={PATH} />
</ThemeProvider>,
{ wrapper: MemoryRouter },
);

const card = testComponent.container.querySelector('div');
const card = screen.getByTestId('card');

if (card) {
fireEvent.click(card);
expect(window.location.pathname).toBe(`/${path}`);
}
userEvent.click(card);
waitFor(() => {
expect(window.location.pathname).toBe(`/${PATH}`);
});
});
});

0 comments on commit 48394df

Please sign in to comment.