Skip to content

Commit d338d44

Browse files
authored
Merge pull request #67 from ssu-commerce/feature/SCF-66
[SCF-66] mock server init
2 parents 0568bba + 12f557f commit d338d44

File tree

13 files changed

+823
-41
lines changed

13 files changed

+823
-41
lines changed

apps/commerce/app/(main)/book/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import BiViewListIcon from "assets/svg/bi_view_list_icon.svg";
99
import { Card } from "component/common/card";
1010
import { Pagination } from "component/common/pagination";
1111
import { BiView } from "component/common/biView";
12-
import { MOCK } from "../../../mock/constants";
12+
import { MOCK } from "../../../mocks/json/constants";
1313
import * as S from "./book.styles";
1414
import { ViewMode } from "./book.constants";
1515

apps/commerce/instrumentation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export async function register() {
2+
if (process.env.NEXT_RUNTIME === "nodejs") {
3+
console.log("server instrucmenttation");
4+
const { server } = await import("./mocks/server");
5+
server.listen();
6+
}
7+
}

apps/commerce/mocks/browser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { setupWorker } from "msw/browser";
2+
import { handlers } from "./handlers";
3+
4+
export const worker = setupWorker(...handlers);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sign from "./sign";
2+
3+
export const handlers = [...sign];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { http, HttpResponse } from "msw";
2+
3+
const signIn = [
4+
http.post(`${process.env.NEXT_PUBLIC_API_KEY}/sign-in`, () => {
5+
return HttpResponse.json({
6+
accessToken: "123",
7+
});
8+
}),
9+
http.post(`${process.env.NEXT_PUBLIC_API_KEY}/sign-up`, () => {
10+
return HttpResponse.json();
11+
}),
12+
];
13+
14+
export default signIn;
File renamed without changes.

apps/commerce/mocks/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { setupServer } from "msw/node";
2+
import { handlers } from "./handlers";
3+
4+
export const server = setupServer(...handlers);

apps/commerce/next.config.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ module.exports = {
55
compiler: {
66
styledComponents: true,
77
},
8+
experimental: {
9+
instrumentationHook: true,
10+
},
811
// svgr : https://react-svgr.com/docs/next/
9-
webpack(config) {
12+
webpack(config, { isServer }) {
1013
// Grab the existing rule that handles SVG imports
1114
const fileLoaderRule = config.module.rules.find((rule) =>
1215
rule.test?.test?.(".svg"),
@@ -31,6 +34,20 @@ module.exports = {
3134
// Modify the file loader rule to ignore *.svg, since we have it handled now.
3235
fileLoaderRule.exclude = /\.svg$/i;
3336

37+
if (isServer) {
38+
if (Array.isArray(config.resolve.alias)) {
39+
config.resolve.alias.push({ name: "msw/browser", alias: false });
40+
} else {
41+
config.resolve.alias["msw/browser"] = false;
42+
}
43+
} else {
44+
if (Array.isArray(config.resolve.alias)) {
45+
config.resolve.alias.push({ name: "msw/node", alias: false });
46+
} else {
47+
config.resolve.alias["msw/node"] = false;
48+
}
49+
}
50+
3451
return config;
3552
},
3653
};

apps/commerce/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"zustand": "^4.5.2"
3232
},
3333
"devDependencies": {
34-
"@sc-config/jest": "workspace:*",
3534
"@next/eslint-plugin-next": "^13.4.19",
35+
"@sc-config/jest": "workspace:*",
3636
"@svgr/webpack": "^8.1.0",
3737
"@tanstack/eslint-plugin-query": "^5.17.7",
3838
"@testing-library/jest-dom": "^6.1.4",
@@ -43,7 +43,13 @@
4343
"eslint-config-sc": "workspace:*",
4444
"jest": "^29.7.0",
4545
"jest-environment-jsdom": "^29.6.4",
46+
"msw": "^2.2.12",
4647
"tsconfig-sc": "workspace:*",
4748
"typescript": "^5.2.2"
49+
},
50+
"msw": {
51+
"workerDirectory": [
52+
"public"
53+
]
4854
}
49-
}
55+
}

apps/commerce/providers/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import type { ReactNode } from "react";
22
import { ReactQueryProvider } from "./reactQuery";
33
import StyledJsxProvider from "./emotion";
44
import { ThemeProvider } from "./theme";
5+
import { MockProvider } from "./msw";
56

67
const Providers = ({ children }: { children: ReactNode }) => {
78
return (
8-
<ReactQueryProvider>
9-
<StyledJsxProvider>
10-
<ThemeProvider>{children}</ThemeProvider>
11-
</StyledJsxProvider>
12-
</ReactQueryProvider>
9+
<MockProvider>
10+
<ReactQueryProvider>
11+
<StyledJsxProvider>
12+
<ThemeProvider>{children}</ThemeProvider>
13+
</StyledJsxProvider>
14+
</ReactQueryProvider>
15+
</MockProvider>
1316
);
1417
};
1518

0 commit comments

Comments
 (0)