Quellcode durchsuchen

update test script

RoyLiu vor 4 Jahren
Ursprung
Commit
4349e84f44

+ 1 - 2
.eslintignore

@@ -1,8 +1,7 @@
 **/node_modules/**
-**/static/**
+**/public/**
 **/__mocks__/**
 **/next-env.d.ts
-**/polyfill.js
 **/next.config.js
 **/_document.js
 **/_app.js

+ 6 - 5
__test__/button.test.tsx

@@ -1,8 +1,9 @@
 /* eslint-disable no-undef */
 import React from 'react';
-import { render, cleanup, fireEvent } from '@testing-library/react';
+import { cleanup, fireEvent } from '@testing-library/react';
 import '@testing-library/jest-dom';
 
+import testWrapper from '../helpers/testWrapper';
 import Button from '../components/Button';
 import theme from '../helpers/theme';
 
@@ -10,13 +11,13 @@ describe('Button component', () => {
   afterEach(cleanup);
 
   test('check button content', () => {
-    const { getByText } = render(<Button>btn content</Button>);
+    const { getByText } = testWrapper(<Button>btn content</Button>);
 
     expect(getByText('btn content').textContent).toBe('btn content');
   });
 
   test('check button primary theme', () => {
-    const { getByText } = render(
+    const { getByText } = testWrapper(
       <Button appearance="primary">btn content</Button>,
     );
 
@@ -32,7 +33,7 @@ describe('Button component', () => {
     const handleClick = (): void => {
       counter += 1;
     };
-    const { getByText } = render(
+    const { getByText } = testWrapper(
       <Button onClick={handleClick}>btn content</Button>,
     );
 
@@ -41,7 +42,7 @@ describe('Button component', () => {
   });
 
   test('check disable status', () => {
-    const { getByText } = render(<Button isDisabled>btn content</Button>);
+    const { getByText } = testWrapper(<Button isDisabled>btn content</Button>);
 
     expect(getByText('btn content')).toBeDisabled();
   });

+ 4 - 3
__test__/dialog.test.tsx

@@ -1,21 +1,22 @@
 /* eslint-disable no-undef */
 import React from 'react';
-import { render, cleanup } from '@testing-library/react';
+import { cleanup } from '@testing-library/react';
 import '@testing-library/jest-dom';
 
+import testWrapper from '../helpers/testWrapper';
 import Dialog from '../components/Dialog';
 
 describe('Dialog component', () => {
   afterEach(cleanup);
 
   test('check open status', () => {
-    const { getByText } = render(<Dialog open>content</Dialog>);
+    const { getByText } = testWrapper(<Dialog open>content</Dialog>);
 
     expect(getByText('content')).toBeVisible();
   });
 
   test('check close status', () => {
-    const { queryByText } = render(<Dialog open={false}>content</Dialog>);
+    const { queryByText } = testWrapper(<Dialog open={false}>content</Dialog>);
 
     expect(queryByText('content')).not.toBeInTheDocument();
   });

+ 5 - 4
__test__/drawer.test.tsx

@@ -1,19 +1,20 @@
 /* eslint-disable no-undef */
 import React from 'react';
-import { render, cleanup } from '@testing-library/react';
+import { cleanup } from '@testing-library/react';
 
+import testWrapper from '../helpers/testWrapper';
 import Drawer from '../components/Drawer';
 
 describe('Drawer component', () => {
   afterEach(cleanup);
 
   test('drawer content', () => {
-    const { getByText } = render(<Drawer>content</Drawer>);
+    const { getByText } = testWrapper(<Drawer>content</Drawer>);
     expect(getByText('content').textContent).toBe('content');
   });
 
   test('close drawer', () => {
-    const { getByTestId } = render(<Drawer>content</Drawer>);
+    const { getByTestId } = testWrapper(<Drawer>content</Drawer>);
     const ele = getByTestId('drawer');
     const style = window.getComputedStyle(ele as HTMLElement);
 
@@ -21,7 +22,7 @@ describe('Drawer component', () => {
   });
 
   test('open drawer', () => {
-    const { getByTestId } = render(<Drawer open>content</Drawer>);
+    const { getByTestId } = testWrapper(<Drawer open>content</Drawer>);
     const ele = getByTestId('drawer');
     const style = window.getComputedStyle(ele as HTMLElement);
 

+ 29 - 10
__test__/inputBox.test.tsx

@@ -1,8 +1,9 @@
 /* eslint-disable no-undef */
 import React from 'react';
-import { render, cleanup, fireEvent } from '@testing-library/react';
+import { cleanup } from '@testing-library/react';
 import '@testing-library/jest-dom';
 
+import testWrapper from '../helpers/testWrapper';
 import InputBox from '../components/InputBox';
 import TextareaBox from '../components/TextareaBox';
 
@@ -10,31 +11,49 @@ describe('Input Box component', () => {
   afterEach(cleanup);
 
   test('input element', () => {
-    const { container } = render(<InputBox />);
+    const handleChange = (value: string) => {
+      console.log(value);
+    };
+
+    const { container } = testWrapper(
+      <InputBox value="test" onChange={handleChange} />,
+    );
     const inputNode = container.querySelector('input') as HTMLElement;
     expect(inputNode.tagName).toBe('INPUT');
   });
 
   test('textarea element', () => {
-    const { container } = render(<TextareaBox />);
+    const handleChange = (value: string) => {
+      console.log(value);
+    };
+
+    const { container } = testWrapper(
+      <TextareaBox value="test" onChange={handleChange} />,
+    );
     const textAreaNode = container.querySelector('TextArea') as HTMLElement;
     expect(textAreaNode.tagName).toBe('TEXTAREA');
   });
 
-  test('onChange event', () => {
-    const { container } = render(<InputBox />);
+  test('test value', () => {
+    const handleChange = (value: string) => {
+      console.log(value);
+    };
+
+    const { container } = testWrapper(
+      <InputBox value="test default value" onChange={handleChange} />,
+    );
     const inputNode = container.querySelector('input') as HTMLInputElement;
-    fireEvent.change(inputNode, { target: { value: 'text' } });
 
-    expect(inputNode.value).toBe('text');
+    // fireEvent.change(inputNode, { target: { value: 'text' } });
+
+    expect(inputNode.value).toBe('test default value');
   });
 
   test('check disabled input', () => {
     const handleChange = jest.fn();
-    const { getByTestId } = render(
-      <InputBox disabled onChange={handleChange} />,
+    const { getByTestId } = testWrapper(
+      <InputBox disabled value="" onChange={handleChange} />,
     );
-    fireEvent.change(getByTestId('input'), { target: { value: 'text' } });
 
     expect(handleChange).toBeCalledTimes(0);
     expect(getByTestId('input')).toBeDisabled();

+ 6 - 6
__test__/selectBox.test.tsx

@@ -1,7 +1,7 @@
-/* eslint-disable no-undef */
 import React from 'react';
-import { render, cleanup, fireEvent } from '@testing-library/react';
+import { cleanup, fireEvent } from '@testing-library/react';
 
+import testWrapper from '../helpers/testWrapper';
 import SelectBox from '../components/SelectBox';
 
 describe('SelectBox component', () => {
@@ -21,7 +21,7 @@ describe('SelectBox component', () => {
   ];
 
   test('default value', () => {
-    const { getAllByTestId } = render(<SelectBox options={options} />);
+    const { getAllByTestId } = testWrapper(<SelectBox options={options} />);
     const ele = getAllByTestId('selected')[0].querySelector(
       'div',
     ) as HTMLDivElement;
@@ -30,17 +30,17 @@ describe('SelectBox component', () => {
   });
 
   test('open list', () => {
-    const { container } = render(<SelectBox options={options} />);
+    const { container } = testWrapper(<SelectBox options={options} />);
 
     fireEvent.mouseDown(
       container.querySelector('[data-testid="selected"]') as HTMLElement,
     );
 
-    expect(container.querySelectorAll('span')[1].textContent).toBe('option 2');
+    expect(container.querySelectorAll('span')[2].textContent).toBe('option 2');
   });
 
   test('use input box', () => {
-    const { container } = render(<SelectBox options={options} useInput />);
+    const { container } = testWrapper(<SelectBox options={options} useInput />);
     const inputNode = container.querySelector('input') as HTMLInputElement;
 
     expect(inputNode.value).toBe('option 1');

+ 3 - 2
__test__/slider.test.tsx

@@ -1,15 +1,16 @@
 /* eslint-disable no-undef */
 import React from 'react';
-import { render, cleanup } from '@testing-library/react';
+import { cleanup } from '@testing-library/react';
 import '@testing-library/jest-dom';
 
+import testWrapper from '../helpers/testWrapper';
 import Sliders from '../components/Sliders';
 
 describe('Slider component', () => {
   afterEach(cleanup);
 
   test('check sliders status', () => {
-    const { getByTestId } = render(<Sliders />);
+    const { getByTestId } = testWrapper(<Sliders />);
 
     expect(getByTestId('sliders')).toBeVisible();
   });

+ 10 - 5
__test__/typography.test.tsx

@@ -1,26 +1,31 @@
 /* eslint-disable no-undef */
 import React from 'react';
-import { render, cleanup } from '@testing-library/react';
+import { cleanup } from '@testing-library/react';
 import '@testing-library/jest-dom';
 
+import testWrapper from '../helpers/testWrapper';
 import Typography from '../components/Typography';
 
 describe('Typography component', () => {
   afterEach(cleanup);
 
   test('check title style', () => {
-    const { getByText } = render(
+    const { getByText } = testWrapper(
       <Typography variant="title">title</Typography>,
     );
 
-    expect(getByText('title')).toHaveStyle('font-size: 16px;color: #000000;');
+    expect(getByText('title')).toHaveStyle(
+      'font-size: 1.35rem;color: #000000;',
+    );
   });
 
   test('check subtitle style', () => {
-    const { queryByText } = render(
+    const { queryByText } = testWrapper(
       <Typography variant="subtitle">title</Typography>,
     );
 
-    expect(queryByText('title')).toHaveStyle('font-size: 14px;color: #000000;');
+    expect(queryByText('title')).toHaveStyle(
+      'font-size: 1.2rem;color: #000000;',
+    );
   });
 });

+ 2 - 1
components/InputBox/index.tsx

@@ -25,6 +25,7 @@ const InputBox = forwardRef<HTMLInputElement, Props>(
 
     return (
       <Input
+        type="text"
         data-testid="input"
         ref={ref}
         disabled={disabled}
@@ -44,7 +45,7 @@ InputBox.defaultProps = {
   onBlur: () => {
     // do something
   },
-  value: '',
+  value: undefined,
   defaultValue: undefined,
   placeholder: '',
   disabled: false,

+ 1 - 1
components/TextareaBox/index.tsx

@@ -43,7 +43,7 @@ InputBox.defaultProps = {
   onBlur: () => {
     // do something
   },
-  value: '',
+  value: undefined,
   defaultValue: undefined,
   placeholder: '',
   disabled: false,

+ 18 - 0
helpers/testWrapper.tsx

@@ -0,0 +1,18 @@
+import React from 'react';
+import { ThemeProvider } from 'styled-components';
+import { render } from '@testing-library/react';
+
+import myTheme from './theme';
+
+const getThemeProviderWrappingComponent = (theme: Record<string, unknown>) => (
+  children: React.ReactNode,
+) => <ThemeProvider theme={theme}>{children}</ThemeProvider>;
+
+const renderwWithTheme = (
+  tree: React.ReactNode,
+  theme: Record<string, unknown> = myTheme,
+) => {
+  return render(getThemeProviderWrappingComponent(theme)(tree));
+};
+
+export default renderwWithTheme;

+ 4 - 1
package.json

@@ -19,6 +19,7 @@
   },
   "lint-staged": {
     "*.@(js|ts|tsx)": [
+      "yarn test",
       "yarn lint",
       "yarn format"
     ]
@@ -50,7 +51,9 @@
   },
   "license": "MIT",
   "devDependencies": {
-    "@babel/node": "^7.2.2",
+    "@babel/cli": "^7.11.6",
+    "@babel/core": "^7.11.6",
+    "@babel/node": "^7.10.5",
     "@babel/preset-env": "^7.8.3",
     "@testing-library/jest-dom": "^5.8.0",
     "@testing-library/react": "^10.0.4",

+ 25 - 4
yarn.lock

@@ -52,6 +52,22 @@
   dependencies:
     cross-fetch "3.0.5"
 
+"@babel/cli@^7.11.6":
+  version "7.11.6"
+  resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.11.6.tgz#1fcbe61c2a6900c3539c06ee58901141f3558482"
+  integrity sha512-+w7BZCvkewSmaRM6H4L2QM3RL90teqEIHDIFXAmrW33+0jhlymnDAEdqVeCZATvxhQuio1ifoGVlJJbIiH9Ffg==
+  dependencies:
+    commander "^4.0.1"
+    convert-source-map "^1.1.0"
+    fs-readdir-recursive "^1.1.0"
+    glob "^7.0.0"
+    lodash "^4.17.19"
+    make-dir "^2.1.0"
+    slash "^2.0.0"
+    source-map "^0.5.0"
+  optionalDependencies:
+    chokidar "^2.1.8"
+
 "@babel/code-frame@7.8.3":
   version "7.8.3"
   resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.8.3.tgz#33e25903d7481181534e12ec0a25f16b6fcf419e"
@@ -95,7 +111,7 @@
     semver "^5.4.1"
     source-map "^0.5.0"
 
-"@babel/core@^7.1.0":
+"@babel/core@^7.1.0", "@babel/core@^7.11.6":
   version "7.11.6"
   resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.11.6.tgz#3a9455dc7387ff1bac45770650bc13ba04a15651"
   integrity sha512-Wpcv03AGnmkgm6uS6k8iwhIwTrcP0m17TL1n1sy7qD0qelDu4XNeW0dN0mHfa+Gei211yDaLoEe/VlbXQzM4Bg==
@@ -350,7 +366,7 @@
     chalk "^2.0.0"
     js-tokens "^4.0.0"
 
-"@babel/node@^7.2.2":
+"@babel/node@^7.10.5":
   version "7.10.5"
   resolved "https://registry.yarnpkg.com/@babel/node/-/node-7.10.5.tgz#30866322aa2c0251a9bdd73d07a9167bd1f4ed64"
   integrity sha512-suosS7zZ2roj+fYVCnDuVezUbRc0sdoyF0Gj/1FzWxD4ebbGiBGtL5qyqHH4NO34B5m4vWWYWgyNhSsrqS8vwA==
@@ -3219,7 +3235,7 @@ conventional-commit-types@^3.0.0:
   resolved "https://registry.yarnpkg.com/conventional-commit-types/-/conventional-commit-types-3.0.0.tgz#7c9214e58eae93e85dd66dbfbafe7e4fffa2365b"
   integrity sha512-SmmCYnOniSsAa9GqWOeLqc179lfr5TRu5b4QFDkbsrJ5TZjPJx85wtOr3zn+1dbeNiXDKGPbZ72IKbPhLXh/Lg==
 
-convert-source-map@1.7.0, convert-source-map@^1.4.0, convert-source-map@^1.5.0, convert-source-map@^1.7.0:
+convert-source-map@1.7.0, convert-source-map@^1.1.0, convert-source-map@^1.4.0, convert-source-map@^1.5.0, convert-source-map@^1.7.0:
   version "1.7.0"
   resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.7.0.tgz#17a2cb882d7f77d3490585e2ce6c524424a3a442"
   integrity sha512-4FJkXzKXEDB1snCFZlLP4gpC3JILicCpGbzG9f9G7tGqGCzETQ2hWPrcinA9oU4wtf2biUaEH5065UnMeR33oA==
@@ -4774,6 +4790,11 @@ fs-minipass@^2.0.0:
   dependencies:
     minipass "^3.0.0"
 
+fs-readdir-recursive@^1.1.0:
+  version "1.1.0"
+  resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz#e32fc030a2ccee44a6b5371308da54be0b397d27"
+  integrity sha512-GNanXlVr2pf02+sPN40XN8HG+ePaNcvM0q5mZBd668Obwb0yD5GiUbZOFgwn8kGMY6I3mdyDJzieUy3PTYyTRA==
+
 fs-write-stream-atomic@^1.0.8:
   version "1.0.10"
   resolved "https://registry.yarnpkg.com/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.10.tgz#b47df53493ef911df75731e70a9ded0189db40c9"
@@ -4890,7 +4911,7 @@ glob@7.1.4:
     once "^1.3.0"
     path-is-absolute "^1.0.0"
 
-glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4:
+glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4:
   version "7.1.6"
   resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
   integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==