Skip to content

Commit 4f74e57

Browse files
committed
feat: apply msw when app is lauched dev mode
#19
1 parent 7b99a08 commit 4f74e57

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

App.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import {NavigationContainer} from '@react-navigation/native';
22
import {QueryClientProvider} from '@tanstack/react-query';
3-
import React from 'react';
3+
import React, {useEffect, useState} from 'react';
44

55
import queryClient from '@/api/queryClient';
6+
import {initMSW} from '@/mocks/init';
67
import SignUpStackNavigator from '@/navigations/stack/SignUpStackNavigator';
78

89
export default function App() {
10+
const [isMockingEnabled, setMockingEnabled] = useState(false);
11+
12+
useEffect(() => {
13+
initMSW().then(() => setMockingEnabled(true));
14+
}, []);
15+
16+
if (!isMockingEnabled) return null;
17+
918
return (
1019
<QueryClientProvider client={queryClient}>
1120
<NavigationContainer>

src/mocks/handlers/index.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {http, HttpResponse} from 'msw';
2+
import {Config} from 'react-native-config';
3+
4+
const {BASE_URL: API_BASE_URL} = Config;
5+
6+
const getHandlers = [
7+
http.get(API_BASE_URL + '/auth/signout', () => {
8+
return HttpResponse.json({});
9+
}),
10+
http.get(API_BASE_URL + '/question/today', () => {
11+
return HttpResponse.json({
12+
question: '오늘의 질문 샘플 질문입니다',
13+
});
14+
}),
15+
http.get(API_BASE_URL + '/members', () => {
16+
return HttpResponse.json({});
17+
}),
18+
];
19+
20+
const postHandlers = [
21+
http.post(API_BASE_URL + '/auth/signup', () => {
22+
return HttpResponse.json({
23+
token: 'token',
24+
name: 'test',
25+
role: 'son',
26+
});
27+
}),
28+
http.post(API_BASE_URL + '/auth/signin', () => {
29+
return HttpResponse.json({
30+
token: 'token',
31+
name: 'test',
32+
role: 'son',
33+
});
34+
}),
35+
];
36+
37+
export const handlers = [...getHandlers, ...postHandlers];

src/mocks/init.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export async function initMSW() {
2+
if (!__DEV__) {
3+
return;
4+
}
5+
await import('./msw.polyfills');
6+
const {server} = await import('./server');
7+
server.listen();
8+
}

src/mocks/msw.polyfills.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import 'fast-text-encoding';
2+
import 'react-native-url-polyfill/auto';

src/mocks/server.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {setupServer} from 'msw/native';
2+
3+
import {handlers} from './handlers';
4+
5+
export const server = setupServer(...handlers);

tsconfig.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
"compilerOptions": {
44
"baseUrl": ".",
55
"paths": {
6-
"@/*": ["src/*"]
7-
}
6+
"@/*": [
7+
"src/*"
8+
]
9+
},
10+
"module": "ES2022"
811
},
912
"target": "ES5"
10-
}
13+
}

0 commit comments

Comments
 (0)