From ceff0b69d6f5d6da152fc79135e513050a6965c8 Mon Sep 17 00:00:00 2001 From: akai Date: Mon, 19 Oct 2020 16:46:59 +0800 Subject: [PATCH] Add test cases (#9) * test: update LocaleProvider * test: update Time * test: update Modal * test: update Toolbar * test: update Toast * test: add snapshots * test: update Stepper * test: remove SendConfirm & Tree test files * test: update Goods * test: update Form * test: update Input * test: add useMessages test * test: add useQuickReplies test * docs: update README --- README.md | 8 + README.zh-CN.md | 8 + .../Form/__tests__/actions.test.tsx | 25 ++ src/components/Form/__tests__/index.test.tsx | 21 ++ src/components/Form/__tests__/item.test.tsx | 44 ++++ src/components/Goods/__tests__/index.test.tsx | 30 ++- src/components/Input/__tests__/index.test.tsx | 100 ++++++- .../LocaleProvider/__tests__/index.test.tsx | 113 ++++++-- src/components/LocaleProvider/index.tsx | 9 +- src/components/Modal/Base.tsx | 2 +- src/components/Modal/__tests__/base.test.tsx | 148 +++++++++++ .../Modal/__tests__/confirm.test.tsx | 20 ++ src/components/Modal/__tests__/index.test.tsx | 75 ------ src/components/Modal/__tests__/modal.test.tsx | 13 + src/components/Modal/__tests__/popup.test.tsx | 14 + .../SendConfirm/__tests__/index.test.tsx | 23 -- src/components/Stepper/Stepper.tsx | 2 +- .../Stepper/__tests__/index.test.tsx | 44 +++- .../Stepper/__tests__/step.test.tsx | 58 ++++ src/components/Time/__tests__/index.test.tsx | 34 +-- src/components/Time/__tests__/parser.test.ts | 51 ++++ src/components/Time/parser.ts | 8 +- src/components/Toast/__tests__/index.test.ts | 71 +++++ src/components/Toast/__tests__/index.test.tsx | 27 -- src/components/Toast/__tests__/toast.test.tsx | 60 +++++ src/components/Toolbar/Toolbar.tsx | 2 +- .../Toolbar/__tests__/button.test.tsx | 36 +++ .../Toolbar/__tests__/index.test.tsx | 56 +++- src/components/Tree/__tests__/index.test.tsx | 90 ------- .../__snapshots__/index.test.tsx.snap | 25 ++ .../__snapshots__/index.test.tsx.snap | 9 + src/hooks/__test__/useMessages.test.tsx | 249 ++++++++++++++++++ src/hooks/__test__/useQuickReplies.test.tsx | 188 +++++++++++++ 33 files changed, 1378 insertions(+), 285 deletions(-) create mode 100644 src/components/Form/__tests__/actions.test.tsx create mode 100644 src/components/Form/__tests__/index.test.tsx create mode 100644 src/components/Form/__tests__/item.test.tsx create mode 100644 src/components/Modal/__tests__/base.test.tsx create mode 100644 src/components/Modal/__tests__/confirm.test.tsx delete mode 100644 src/components/Modal/__tests__/index.test.tsx create mode 100644 src/components/Modal/__tests__/modal.test.tsx create mode 100644 src/components/Modal/__tests__/popup.test.tsx delete mode 100644 src/components/SendConfirm/__tests__/index.test.tsx create mode 100644 src/components/Stepper/__tests__/step.test.tsx create mode 100644 src/components/Time/__tests__/parser.test.ts create mode 100644 src/components/Toast/__tests__/index.test.ts delete mode 100644 src/components/Toast/__tests__/index.test.tsx create mode 100644 src/components/Toast/__tests__/toast.test.tsx create mode 100644 src/components/Toolbar/__tests__/button.test.tsx delete mode 100644 src/components/Tree/__tests__/index.test.tsx create mode 100644 src/components/Typing/__tests__/__snapshots__/index.test.tsx.snap create mode 100644 src/components/VisuallyHidden/__tests__/__snapshots__/index.test.tsx.snap create mode 100644 src/hooks/__test__/useMessages.test.tsx create mode 100644 src/hooks/__test__/useQuickReplies.test.tsx diff --git a/README.md b/README.md index fa2e1a56..2c7cd1e9 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,14 @@ const App = () => { [![DEMO](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/chatui-demo-o6n3z?fontsize=14&hidenavigation=1&theme=dark) +### Development + +```bash +cd storybook +npm i +npm run storybook +``` + ## Theme Visit [Customize Theme](https://chatui.io/docs/customize-theme) for detail diff --git a/README.zh-CN.md b/README.zh-CN.md index 52ff4c5a..15d718bc 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -98,6 +98,14 @@ const App = () => { [![DEMO](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/chatui-demo-o6n3z?fontsize=14&hidenavigation=1&theme=dark) +### 本地开发 + +```bash +cd storybook +npm i +npm run storybook +``` + ### 定制主题 参考 [定制主题](https://chatui.io/docs/customize-theme) 文档。 diff --git a/src/components/Form/__tests__/actions.test.tsx b/src/components/Form/__tests__/actions.test.tsx new file mode 100644 index 00000000..15ce31c1 --- /dev/null +++ b/src/components/Form/__tests__/actions.test.tsx @@ -0,0 +1,25 @@ +import React from 'react'; +import { render, cleanup } from '@testing-library/react'; +import { FormActions } from '..'; + +afterEach(cleanup); + +describe('', () => { + it('should render a form actions', () => { + const { getByTestId } = render(); + const formActions = getByTestId('formActions'); + + expect(formActions).toHaveClass('FormActions'); + }); + + it('should render children', () => { + const { getByTestId } = render( + + testChild + , + ); + const formActions = getByTestId('formActions'); + + expect(formActions).toContainHTML('testChild'); + }); +}); diff --git a/src/components/Form/__tests__/index.test.tsx b/src/components/Form/__tests__/index.test.tsx new file mode 100644 index 00000000..ea42a292 --- /dev/null +++ b/src/components/Form/__tests__/index.test.tsx @@ -0,0 +1,21 @@ +import React from 'react'; +import { render, cleanup } from '@testing-library/react'; +import { Form } from '..'; + +afterEach(cleanup); + +describe('
', () => { + it('should render a form', () => { + const { getByTestId } = render(); + const form = getByTestId('form'); + + expect(form).toBeInTheDocument(); + }); + + it('should render a form (light)', () => { + const { getByTestId } = render(); + const form = getByTestId('form'); + + expect(form).toHaveClass('is-light'); + }); +}); diff --git a/src/components/Form/__tests__/item.test.tsx b/src/components/Form/__tests__/item.test.tsx new file mode 100644 index 00000000..bfe10bd4 --- /dev/null +++ b/src/components/Form/__tests__/item.test.tsx @@ -0,0 +1,44 @@ +import React from 'react'; +import { render, cleanup } from '@testing-library/react'; +import { FormItem } from '..'; + +afterEach(cleanup); + +describe('', () => { + it('should render a label', () => { + const { container } = render(); + const formItem = container.querySelector('.FormItem'); + + expect(formItem?.querySelector('.Label')).not.toBeNull(); + expect(formItem).toHaveTextContent('testLabel'); + }); + + it('should render a help text', () => { + const { container } = render(); + const formItem = container.querySelector('.FormItem'); + + expect(formItem?.querySelector('.HelpText')).not.toBeNull(); + expect(formItem).toHaveTextContent('testHelp'); + }); + + it('should have a status (required)', () => { + const { container } = render(); + const formItem = container.querySelector('.FormItem'); + + expect(formItem).toHaveClass('required'); + }); + + it('should have a status (invalid)', () => { + const { container } = render(); + const formItem = container.querySelector('.FormItem'); + + expect(formItem).toHaveClass('is-invalid'); + }); + + it('should be hidden if specified', () => { + const { container } = render(