Skip to content

Commit

Permalink
test: 홈 페이지 렌더링/리다이렉트 로직 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
stakbucks committed Mar 10, 2024
1 parent 847adba commit f60fc39
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 24 deletions.
6 changes: 1 addition & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
{
"extends": [
"next/core-web-vitals",
"prettier",
"plugin:storybook/recommended"
]
"extends": ["next/core-web-vitals", "prettier", "plugin:storybook/recommended"]
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
"@storybook/react": "^7.6.17",
"@storybook/test": "^7.6.17",
"@svgr/webpack": "^8.1.0",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^14.2.1",
"@testing-library/user-event": "^14.5.2",
"@types/jest": "^29.5.12",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand All @@ -73,6 +75,7 @@
"tailwindcss": "^3.3.0",
"tsconfig-paths-webpack-plugin": "^4.1.0",
"typescript": "^5",
"vite-tsconfig-paths": "^4.3.1",
"vitest": "^1.3.1"
}
}
145 changes: 137 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions src/__tests__/home.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Home from '@/app/page';
import render from '@/__tests__/setups/render';
import { screen } from '@testing-library/react';
import { redirect } from 'next/navigation';
import { IsLoggedIn, checkIsLoggedIn } from '@/services/auth/getMember';

vi.mock('next/headers', async () => {
return {
cookies: () => {
return {
get: (name: string) => {
return {
value: 'cookie',
};
},
};
},
};
});
vi.mock('@/services/auth/getMember');

describe('홈 페이지 테스트', () => {
it('로그인 안했을 때 홈 페이지를 렌더링 합니다.', async () => {
vi.mocked(checkIsLoggedIn).mockReturnValue(Promise.resolve({ isLoggedIn: true }));
render(await Home());
const button = screen.getByText('로그인하기');

expect(button).toBeInTheDocument();
vi.resetAllMocks();
});

it('로그인 했을 때는 유저의 홈으로 리다이렉트 합니다.', async () => {
const member = { memberId: 2 };
vi.mocked(checkIsLoggedIn).mockReturnValue(
Promise.resolve({ isLoggedIn: true, member }) as Promise<IsLoggedIn>,
);

vi.mock('next/navigation', async () => {
return {
redirect: vi.fn(),
};
});

render(await Home());

expect(redirect).toHaveBeenCalledWith(`member/2`);
});
});
Loading

0 comments on commit f60fc39

Please sign in to comment.