|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { PREFIX } from '../../tests'; |
| 4 | +import { DCompose } from '../compose'; |
| 5 | +import { DIcon } from '../icon'; |
| 6 | +import { DButtonGroup, DButton } from './index'; |
| 7 | + |
| 8 | +const icon = ( |
| 9 | + <DIcon data-testid="custom-icon" viewBox="64 64 896 896"> |
| 10 | + <path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 00203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path> |
| 11 | + </DIcon> |
| 12 | +); |
| 13 | + |
| 14 | +describe('DButton', () => { |
| 15 | + const text = 'This is DButton'; |
| 16 | + |
| 17 | + const buttonClassList = () => { |
| 18 | + return screen.getByRole('button').classList; |
| 19 | + }; |
| 20 | + |
| 21 | + it('should `children` work', () => { |
| 22 | + const { getByText } = render(<DButton children={text} />); |
| 23 | + expect(getByText(text)).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should `dType` work', () => { |
| 27 | + const dType = 'secondary'; |
| 28 | + render(<DButton dType={dType} />); |
| 29 | + expect(buttonClassList().contains(`${PREFIX}button--${dType}`)).toBeTruthy(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should `dColor` work', () => { |
| 33 | + const dColor = 'danger'; |
| 34 | + render(<DButton dColor={dColor} />); |
| 35 | + expect(buttonClassList().contains(`t-${dColor}`)).toBeTruthy(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should `dLoading` work', () => { |
| 39 | + const { getByRole } = render(<DButton dLoading={true} />); |
| 40 | + expect(buttonClassList().contains(`is-loading`)).toBeTruthy(); |
| 41 | + expect(getByRole('button').children.length).toBe(1); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should `dIcon` work', () => { |
| 45 | + const { getByRole } = render(<DButton dIcon={icon} />); |
| 46 | + expect(getByRole('button').children.length).toBe(1); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should `dIconRight` work', () => { |
| 50 | + const { getByRole } = render(<DButton dIcon={icon} dIconRight={true} children={text} />); |
| 51 | + expect(getByRole('button').childNodes.length).toBe(2); |
| 52 | + expect(getByRole('button').firstChild?.textContent).toBe(text); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should `dBlock` work', () => { |
| 56 | + render(<DButton dBlock={true} />); |
| 57 | + expect(buttonClassList().contains(`${PREFIX}button--block`)).toBeTruthy(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should `dShape` work', () => { |
| 61 | + const shape = 'round'; |
| 62 | + render(<DButton dShape={shape} />); |
| 63 | + expect(buttonClassList().contains(`${PREFIX}button--${shape}`)).toBeTruthy(); |
| 64 | + expect(buttonClassList().contains(`${PREFIX}button--circle`)).toBeFalsy(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should `dSize` work', () => { |
| 68 | + const size = 'larger'; |
| 69 | + render(<DButton dSize={size} />); |
| 70 | + expect(buttonClassList().contains(`${PREFIX}button--${size}`)).toBeTruthy(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should `disabled` work', () => { |
| 74 | + const { getByRole } = render(<DButton disabled />); |
| 75 | + expect(getByRole('button')).toBeDisabled(); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('ButtonGroup', () => { |
| 80 | + const expectBtnClass = (className: string) => { |
| 81 | + const Buttons = screen.getAllByRole('button'); |
| 82 | + for (const iterator of Buttons) { |
| 83 | + expect(iterator.classList.contains(className)).toBeTruthy(); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + it('should `children` work', () => { |
| 88 | + const { getAllByRole } = render( |
| 89 | + <DButtonGroup> |
| 90 | + <DButton>L</DButton> |
| 91 | + <DButton>M</DButton> |
| 92 | + <DButton>R</DButton> |
| 93 | + </DButtonGroup> |
| 94 | + ); |
| 95 | + expect(getAllByRole('button').length).toBe(3); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should `dType` work', () => { |
| 99 | + const type = 'outline'; |
| 100 | + render( |
| 101 | + <DButtonGroup dType={type}> |
| 102 | + <DButton>L</DButton> |
| 103 | + <DButton>M</DButton> |
| 104 | + <DButton>R</DButton> |
| 105 | + </DButtonGroup> |
| 106 | + ); |
| 107 | + expectBtnClass(`${PREFIX}button--${type}`); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should `dColor` work', () => { |
| 111 | + const color = 'warning'; |
| 112 | + render( |
| 113 | + <DButtonGroup dColor={color}> |
| 114 | + <DButton>L</DButton> |
| 115 | + <DButton>M</DButton> |
| 116 | + <DButton>R</DButton> |
| 117 | + </DButtonGroup> |
| 118 | + ); |
| 119 | + expectBtnClass(`t-${color}`); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should `dSize` work', () => { |
| 123 | + const size = 'smaller'; |
| 124 | + render( |
| 125 | + <DButtonGroup dSize={size}> |
| 126 | + <DButton>L</DButton> |
| 127 | + <DButton>M</DButton> |
| 128 | + <DButton>R</DButton> |
| 129 | + </DButtonGroup> |
| 130 | + ); |
| 131 | + expectBtnClass(`${PREFIX}button--${size}`); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should `dDisabled` work', () => { |
| 135 | + const { getAllByRole } = render( |
| 136 | + <DButtonGroup dDisabled> |
| 137 | + <DButton>L</DButton> |
| 138 | + <DButton>M</DButton> |
| 139 | + <DButton>R</DButton> |
| 140 | + </DButtonGroup> |
| 141 | + ); |
| 142 | + const buttons = getAllByRole('button'); |
| 143 | + expect(buttons[0]).toBeDisabled(); |
| 144 | + expect(buttons[1]).toBeDisabled(); |
| 145 | + expect(buttons[2]).toBeDisabled(); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('DCompose', () => { |
| 150 | + const expectBtnClass = (className: string) => { |
| 151 | + const Buttons = screen.getAllByRole('button'); |
| 152 | + for (const iterator of Buttons) { |
| 153 | + expect(iterator.classList.contains(className)).toBeTruthy(); |
| 154 | + } |
| 155 | + }; |
| 156 | + it('should `dSize` work', () => { |
| 157 | + const size = 'smaller'; |
| 158 | + render( |
| 159 | + <DCompose dSize={size}> |
| 160 | + <DButton></DButton> |
| 161 | + </DCompose> |
| 162 | + ); |
| 163 | + expectBtnClass(`${PREFIX}button--${size}`); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should `dDisabled` work', () => { |
| 167 | + const { getByRole } = render( |
| 168 | + <DCompose dDisabled> |
| 169 | + <DButton></DButton> |
| 170 | + </DCompose> |
| 171 | + ); |
| 172 | + expect(getByRole('button')).toBeDisabled(); |
| 173 | + }); |
| 174 | +}); |
0 commit comments