Skip to content

Commit ccb53cb

Browse files
authored
Merge pull request #145 from Team-INSERT/test/useModal
useModal 테스트 코드 작성
2 parents 7f14de8 + 867abfe commit ccb53cb

14 files changed

+786
-295
lines changed

Diff for: .eslintignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
jest.config.js
2-
next.config.js
2+
jest.setup.js
3+
next.config.js
4+
commitlint.config.js

Diff for: .eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module.exports = {
2828
],
2929
// 'React' must be in scope when using JSX 에러 지우기(Next.js)
3030
"react/react-in-jsx-scope": "off",
31+
"react/jsx-no-useless-fragment": 0,
32+
"react/button-has-type": 0,
3133
// ts파일에서 tsx구문 허용(Next.js)
3234
"@typescript-eslint/no-use-before-define": "off",
3335
"react/jsx-filename-extension": [1, { extensions: [".ts", ".tsx"] }], // should add ".ts" if typescript project

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ yarn-error.log*
3636
# typescript
3737
*.tsbuildinfo
3838
next-env.d.ts
39+
40+
# Sentry Config File
41+
.sentryclirc

Diff for: jest.config.js

+9-48
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,10 @@
1-
// jest.config.js
2-
module.exports = {
3-
collectCoverage: true,
4-
// on node 14.x coverage provider v8 offers good speed and more or less good report
5-
coverageProvider: "v8",
6-
collectCoverageFrom: [
7-
"**/*.{js,jsx,ts,tsx}",
8-
"!**/e2e/**",
9-
"!**/*.d.ts",
10-
"!**/node_modules/**",
11-
"!<rootDir>/out/**",
12-
"!<rootDir>/.next/**",
13-
"!<rootDir>/*.config.js",
14-
"!<rootDir>/*.config.ts",
15-
"!<rootDir>/*rc.js",
16-
"!<rootDir>/coverage/**",
17-
],
18-
moduleNameMapper: {
19-
// Handle CSS imports (with CSS modules)
20-
// https://jestjs.io/docs/webpack#mocking-css-modules
21-
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy",
22-
23-
// Handle CSS imports (without CSS modules)
24-
"^.+\\.(css|sass|scss)$": "<rootDir>/__mocks__/styleMock.js",
25-
26-
// Handle image imports
27-
// https://jestjs.io/docs/webpack#handling-static-assets
28-
"^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i": `<rootDir>/__mocks__/fileMock.js`,
29-
30-
// Handle module aliases
31-
"^@/components/(.*)$": "<rootDir>/components/$1",
32-
},
33-
// Add more setup options before each test is run
34-
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
35-
testPathIgnorePatterns: [
36-
"<rootDir>/node_modules/",
37-
"<rootDir>/.next/",
38-
"<rootDir>/e2e/",
39-
],
40-
transform: {
41-
// Use babel-jest to transpile tests with the next/babel preset
42-
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
43-
"^.+\\.(js|jsx|ts|tsx)$": ["babel-jest", { presets: ["next/babel"] }],
44-
},
45-
transformIgnorePatterns: [
46-
"/node_modules/",
47-
"^.+\\.module\\.(css|sass|scss)$",
48-
],
1+
const nextJest = require("next/jest");
2+
const createJestConfig = nextJest({
3+
dir: "./",
4+
});
5+
const customJestConfig = {
6+
setupFilesAfterEnv: ["<rootDir>/setupTests.ts"],
7+
moduleDirectories: ["node_modules", "<rootDir>/"],
8+
testEnvironment: "jest-environment-jsdom",
499
};
10+
module.exports = createJestConfig(customJestConfig);

Diff for: next.config.js

+43
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,46 @@ const nextConfig = {
1212
};
1313

1414
module.exports = nextConfig;
15+
16+
17+
// Injected content via Sentry wizard below
18+
19+
const { withSentryConfig } = require("@sentry/nextjs");
20+
21+
module.exports = withSentryConfig(
22+
module.exports,
23+
{
24+
// For all available options, see:
25+
// https://github.com/getsentry/sentry-webpack-plugin#options
26+
27+
// Suppresses source map uploading logs during build
28+
silent: true,
29+
org: "team-insert-leader",
30+
project: "javascript-nextjs",
31+
},
32+
{
33+
// For all available options, see:
34+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
35+
36+
// Upload a larger set of source maps for prettier stack traces (increases build time)
37+
widenClientFileUpload: true,
38+
39+
// Transpiles SDK to be compatible with IE11 (increases bundle size)
40+
transpileClientSDK: true,
41+
42+
// Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load)
43+
tunnelRoute: "/monitoring",
44+
45+
// Hides source maps from generated client bundles
46+
hideSourceMaps: true,
47+
48+
// Automatically tree-shake Sentry logger statements to reduce bundle size
49+
disableLogger: true,
50+
51+
// Enables automatic instrumentation of Vercel Cron Monitors.
52+
// See the following for more information:
53+
// https://docs.sentry.io/product/crons/
54+
// https://vercel.com/docs/cron-jobs
55+
automaticVercelMonitors: true,
56+
}
57+
);

Diff for: package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
"build": "next lint && next build",
88
"start": "next start",
99
"lint": "next lint",
10-
"test": "yarn && yarn build && yarn lint && yarn install --immutable --immutable-cache --check-cache && npx playwright install --with-deps && npx playwright test"
10+
"test": "jest --watch",
11+
"test:ci": "jest --ci"
1112
},
1213
"dependencies": {
1314
"@playwright/test": "^1.29.1",
15+
"@sentry/nextjs": "^7.90.0",
1416
"@tanstack/react-query": "^4.35.3",
17+
"@testing-library/jest-dom": "^6.1.5",
18+
"@testing-library/react": "^14.1.2",
1519
"@tinymce/tinymce-react": "^4.3.0",
1620
"@types/d3": "^7.4.0",
1721
"@types/node": "18.16.19",
@@ -36,7 +40,8 @@
3640
"eslint-plugin-prettier": "^4.2.1",
3741
"graphql": "^16.8.0",
3842
"graphql-request": "^6.1.0",
39-
"jest": "^29.3.1",
43+
"jest": "^29.7.0",
44+
"jest-dom": "^4.0.0",
4045
"jotai": "^2.5.1",
4146
"jwt-decode": "^3.1.2",
4247
"msw": "^1.2.3",
@@ -87,7 +92,7 @@
8792
"eslint-plugin-react": "^7.31.10",
8893
"eslint-plugin-react-hooks": "^4.6.0",
8994
"husky": "^8.0.2",
90-
"jest-environment-jsdom": "^29.3.1"
95+
"jest-environment-jsdom": "^29.7.0"
9196
},
9297
"config": {
9398
"commitizen": {

Diff for: sentry.client.config.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This file configures the initialization of Sentry on the client.
2+
// The config you add here will be used whenever a users loads a page in their browser.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from "@sentry/nextjs";
6+
7+
Sentry.init({
8+
dsn: "https://d9b95ec831a783292206973ebf392318@o4506432587563008.ingest.sentry.io/4506432597262336",
9+
10+
// Adjust this value in production, or use tracesSampler for greater control
11+
tracesSampleRate: 1,
12+
13+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
14+
debug: false,
15+
16+
replaysOnErrorSampleRate: 1.0,
17+
18+
// This sets the sample rate to be 10%. You may want this to be 100% while
19+
// in development and sample at a lower rate in production
20+
replaysSessionSampleRate: 0.1,
21+
22+
// You can remove this option if you're not planning to use the Sentry Session Replay feature:
23+
integrations: [
24+
new Sentry.Replay({
25+
// Additional Replay configuration goes in here, for example:
26+
maskAllText: true,
27+
blockAllMedia: true,
28+
}),
29+
],
30+
});

Diff for: sentry.edge.config.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This file configures the initialization of Sentry for edge features (middleware, edge routes, and so on).
2+
// The config you add here will be used whenever one of the edge features is loaded.
3+
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
4+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
5+
6+
import * as Sentry from "@sentry/nextjs";
7+
8+
Sentry.init({
9+
dsn: "https://d9b95ec831a783292206973ebf392318@o4506432587563008.ingest.sentry.io/4506432597262336",
10+
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1,
13+
14+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
15+
debug: false,
16+
});

Diff for: sentry.server.config.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file configures the initialization of Sentry on the server.
2+
// The config you add here will be used whenever the server handles a request.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from "@sentry/nextjs";
6+
7+
Sentry.init({
8+
dsn: "https://d9b95ec831a783292206973ebf392318@o4506432587563008.ingest.sentry.io/4506432597262336",
9+
10+
// Adjust this value in production, or use tracesSampler for greater control
11+
tracesSampleRate: 1,
12+
13+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
14+
debug: false,
15+
});

Diff for: setupTests.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "@testing-library/jest-dom";

Diff for: src/@modal/hooks/useModal.spec.tsx

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { render, screen, fireEvent } from "@testing-library/react";
2+
import useModal from "./useModal";
3+
4+
jest.mock("./useModal");
5+
6+
const TestComponent = () => {
7+
const { openModal, closeModal } = useModal();
8+
return (
9+
<div>
10+
<button onClick={() => openModal({ component: <></> })}>Open</button>
11+
<button onClick={closeModal}>Close</button>
12+
</div>
13+
);
14+
};
15+
16+
describe("useModal", () => {
17+
const mockUseModal = useModal as jest.MockedFunction<typeof useModal>;
18+
mockUseModal.mockReturnValue({
19+
openModal: jest.fn(),
20+
closeModal: jest.fn(),
21+
visible: false,
22+
});
23+
24+
it("openModal이 호출되면 화면에 모달이 render 되어야 한다", async () => {
25+
render(<TestComponent />);
26+
27+
fireEvent.click(screen.getByRole("button", { name: "Open" }));
28+
expect(mockUseModal().openModal).toHaveBeenCalled();
29+
expect(document.activeElement).toBeInstanceOf(HTMLElement);
30+
});
31+
32+
it("closeModal이 호출되면 화면에 render된 모달이 unmount 되어야 한다.", () => {
33+
render(<TestComponent />);
34+
35+
fireEvent.click(screen.getByRole("button", { name: "Close" }));
36+
expect(mockUseModal().closeModal).toHaveBeenCalled();
37+
});
38+
});

Diff for: tests/example.spec.ts

-18
This file was deleted.

Diff for: tsconfig.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"target": "es5",
44
"lib": ["dom", "dom.iterable", "esnext"],
5+
"types": ["jest"],
56
"allowJs": true,
67
"skipLibCheck": true,
78
"strict": true,
@@ -30,7 +31,9 @@
3031
"**/*.tsx",
3132
"tailwind.config.js",
3233
".eslintrc.js",
33-
".next/types/**/*.ts"
34-
, "public/mockServiceWorker.js" ],
34+
".next/types/**/*.ts",
35+
"public/mockServiceWorker.js",
36+
"jest.setup.js"
37+
],
3538
"exclude": ["node_modules"]
3639
}

0 commit comments

Comments
 (0)