Skip to content

Commit

Permalink
[feat] msw ์„ค์ • (#19)
Browse files Browse the repository at this point in the history
* chore: msw ์„ค์น˜ ๋ฐ ์ ์šฉ

* style: alias ์ ์šฉ

* chore: msw-storybook-addon ์ ์šฉ

* chore: storybook ์„ค์ • ์ถ”๊ฐ€

* chore: jest msw ์„ค์ • ์ถ”๊ฐ€
  • Loading branch information
hwookim authored Dec 8, 2022
1 parent 962aa8b commit d0529ca
Show file tree
Hide file tree
Showing 11 changed files with 1,562 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

module.exports = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: ['@storybook/addon-links', '@storybook/addon-essentials', '@storybook/addon-interactions'],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'storybook-addon-next-router',
],
framework: '@storybook/react',
core: {
builder: '@storybook/builder-webpack5',
Expand Down
25 changes: 19 additions & 6 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { initialize, mswDecorator } from 'msw-storybook-addon';
import { RouterContext } from 'next/dist/shared/lib/router-context';
import * as NextImage from 'next/image';

import mockApis from '../__mocks__/apis';
import '../src/styles/reset.css';

initialize();

export const decorators = [
mswDecorator,
(Story) => (
<QueryClientProvider client={new QueryClient()}>
<Story />
</QueryClientProvider>
),
];

export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
msw: mockApis,
actions: { argTypesRegex: '^on.*' },
nextRouter: {
Provider: RouterContext.Provider,
},
};

Expand Down
5 changes: 5 additions & 0 deletions __mocks__/apis/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SetupWorkerApi, setupWorker } from 'msw';

import mockApis from '.';

export const worker: SetupWorkerApi = setupWorker(...mockApis);
9 changes: 9 additions & 0 deletions __mocks__/apis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RequestHandler } from 'msw';

import * as sample from './sample';

const mockApis = Object.values({
sample,
}).flatMap((apis: Record<string, RequestHandler>) => Object.values(apis));

export default mockApis;
4 changes: 4 additions & 0 deletions __mocks__/apis/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//TODO: api ์ƒ์„ฑ ํ›„ ์‚ญ์ œ
import { rest } from 'msw';

export const mockApiSample = rest.post('/test', (req, res, ctx) => res(ctx.json(1)));
5 changes: 5 additions & 0 deletions __mocks__/apis/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SetupServerApi, setupServer } from 'msw/node';

import mockApis from '.';

export const server: SetupServerApi = setupServer(...mockApis);
14 changes: 10 additions & 4 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Optional: configure or set up a testing framework before each test.
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js`
// Used for __tests__/testing-library.js
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';

import { server } from './__mocks__/apis/server';

/* eslint-disable */

beforeAll(() => server.listen());
afterEach(() => {
server.resetHandlers();
});
afterAll(() => server.close());
Loading

0 comments on commit d0529ca

Please sign in to comment.