From 22034a6f8d5c2ff685849e0b37681b5938227a17 Mon Sep 17 00:00:00 2001 From: Miliky Date: Thu, 16 Dec 2021 11:14:18 +0800 Subject: [PATCH] test(ui:button): add tests --- .../ui/src/components/button/Button.test.tsx | 174 ++++++++++++++++++ packages/ui/src/components/button/README.md | 2 +- .../src/components/button/README.zh-Hant.md | 2 +- 3 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 packages/ui/src/components/button/Button.test.tsx diff --git a/packages/ui/src/components/button/Button.test.tsx b/packages/ui/src/components/button/Button.test.tsx new file mode 100644 index 00000000..667743e0 --- /dev/null +++ b/packages/ui/src/components/button/Button.test.tsx @@ -0,0 +1,174 @@ +import { render, screen } from '@testing-library/react'; + +import { PREFIX } from '../../tests'; +import { DCompose } from '../compose'; +import { DIcon } from '../icon'; +import { DButtonGroup, DButton } from './index'; + +const icon = ( + + + +); + +describe('DButton', () => { + const text = 'This is DButton'; + + const buttonClassList = () => { + return screen.getByRole('button').classList; + }; + + it('should `children` work', () => { + const { getByText } = render(); + expect(getByText(text)).toBeInTheDocument(); + }); + + it('should `dType` work', () => { + const dType = 'secondary'; + render(); + expect(buttonClassList().contains(`${PREFIX}button--${dType}`)).toBeTruthy(); + }); + + it('should `dColor` work', () => { + const dColor = 'danger'; + render(); + expect(buttonClassList().contains(`t-${dColor}`)).toBeTruthy(); + }); + + it('should `dLoading` work', () => { + const { getByRole } = render(); + expect(buttonClassList().contains(`is-loading`)).toBeTruthy(); + expect(getByRole('button').children.length).toBe(1); + }); + + it('should `dIcon` work', () => { + const { getByRole } = render(); + expect(getByRole('button').children.length).toBe(1); + }); + + it('should `dIconRight` work', () => { + const { getByRole } = render(); + expect(getByRole('button').childNodes.length).toBe(2); + expect(getByRole('button').firstChild?.textContent).toBe(text); + }); + + it('should `dBlock` work', () => { + render(); + expect(buttonClassList().contains(`${PREFIX}button--block`)).toBeTruthy(); + }); + + it('should `dShape` work', () => { + const shape = 'round'; + render(); + expect(buttonClassList().contains(`${PREFIX}button--${shape}`)).toBeTruthy(); + expect(buttonClassList().contains(`${PREFIX}button--circle`)).toBeFalsy(); + }); + + it('should `dSize` work', () => { + const size = 'larger'; + render(); + expect(buttonClassList().contains(`${PREFIX}button--${size}`)).toBeTruthy(); + }); + + it('should `disabled` work', () => { + const { getByRole } = render(); + expect(getByRole('button')).toBeDisabled(); + }); +}); + +describe('ButtonGroup', () => { + const expectBtnClass = (className: string) => { + const Buttons = screen.getAllByRole('button'); + for (const iterator of Buttons) { + expect(iterator.classList.contains(className)).toBeTruthy(); + } + }; + + it('should `children` work', () => { + const { getAllByRole } = render( + + L + M + R + + ); + expect(getAllByRole('button').length).toBe(3); + }); + + it('should `dType` work', () => { + const type = 'outline'; + render( + + L + M + R + + ); + expectBtnClass(`${PREFIX}button--${type}`); + }); + + it('should `dColor` work', () => { + const color = 'warning'; + render( + + L + M + R + + ); + expectBtnClass(`t-${color}`); + }); + + it('should `dSize` work', () => { + const size = 'smaller'; + render( + + L + M + R + + ); + expectBtnClass(`${PREFIX}button--${size}`); + }); + + it('should `dDisabled` work', () => { + const { getAllByRole } = render( + + L + M + R + + ); + const buttons = getAllByRole('button'); + expect(buttons[0]).toBeDisabled(); + expect(buttons[1]).toBeDisabled(); + expect(buttons[2]).toBeDisabled(); + }); +}); + +describe('DCompose', () => { + const expectBtnClass = (className: string) => { + const Buttons = screen.getAllByRole('button'); + for (const iterator of Buttons) { + expect(iterator.classList.contains(className)).toBeTruthy(); + } + }; + it('should `dSize` work', () => { + const size = 'smaller'; + render( + + + + ); + expectBtnClass(`${PREFIX}button--${size}`); + }); + + it('should `dDisabled` work', () => { + const { getByRole } = render( + + + + ); + expect(getByRole('button')).toBeDisabled(); + }); +}); diff --git a/packages/ui/src/components/button/README.md b/packages/ui/src/components/button/README.md index c039a534..169764ca 100644 --- a/packages/ui/src/components/button/README.md +++ b/packages/ui/src/components/button/README.md @@ -44,5 +44,5 @@ Extend `React.HTMLAttributes`. | dType | Set the shape of the buttons in the button group | Reference DButtonProps['dType'] | 'secondary' | | dColor | Set the color of the buttons in the button group | Reference DButtonProps['dColor'] | 'primary' | | dSize | Set the size of the buttons in the button group | Reference DButtonProps['dSize'] | - | -| buttonGroupDisabled | Disable the buttons in the button group | boolean | false | +| dDisabled | Disable the buttons in the button group | boolean | false | diff --git a/packages/ui/src/components/button/README.zh-Hant.md b/packages/ui/src/components/button/README.zh-Hant.md index 1283944e..ff82feba 100644 --- a/packages/ui/src/components/button/README.zh-Hant.md +++ b/packages/ui/src/components/button/README.zh-Hant.md @@ -43,5 +43,5 @@ export type DButtonRef = HTMLButtonElement; | dType | 设置按钮组中按钮的形态 | 参考 DButtonProps['dType'] | 'secondary' | | dColor | 设置按钮组中按钮的颜色 | 参考 DButtonProps['dColor'] | 'primary' | | dSize | 设置按钮组中按钮的尺寸 | 参考 DButtonProps['dSize'] | - | -| buttonGroupDisabled | 禁用按钮组中的按钮 | boolean | false | +| dDisabled | 禁用按钮组中的按钮 | boolean | false |