|
1 | 1 | import React from 'react' |
2 | | -import { |
3 | | - Button, |
4 | | - ButtonPrimary, |
5 | | - ButtonClose, |
6 | | - ButtonDanger, |
7 | | - ButtonOutline, |
8 | | - ButtonInvisible, |
9 | | - ButtonTableList |
10 | | -} from '../deprecated' |
11 | | -import {ButtonGroup} from '../' |
12 | | -import {render, behavesAsComponent, checkExports} from '../utils/testing' |
13 | | -import {render as HTMLRender, cleanup} from '@testing-library/react' |
| 2 | +import {IconButton, Button} from '../Button' |
| 3 | +import {behavesAsComponent} from '../utils/testing' |
| 4 | +import {render, cleanup, fireEvent} from '@testing-library/react' |
14 | 5 | import {axe, toHaveNoViolations} from 'jest-axe' |
15 | 6 | import 'babel-polyfill' |
| 7 | +import {SearchIcon} from '@primer/octicons-react' |
16 | 8 | expect.extend(toHaveNoViolations) |
17 | 9 |
|
18 | | -// eslint-disable-next-line @typescript-eslint/no-empty-function |
19 | | -function noop() {} |
20 | | - |
21 | 10 | describe('Button', () => { |
22 | | - behavesAsComponent({Component: Button}) |
23 | | - |
24 | | - checkExports('deprecated/Button', { |
25 | | - default: Button, |
26 | | - ButtonPrimary, |
27 | | - ButtonDanger, |
28 | | - ButtonOutline, |
29 | | - ButtonInvisible, |
30 | | - ButtonTableList, |
31 | | - ButtonClose |
32 | | - }) |
33 | | - checkExports('ButtonGroup', { |
34 | | - default: ButtonGroup |
35 | | - }) |
| 11 | + behavesAsComponent({Component: Button, options: {skipAs: true}}) |
36 | 12 |
|
37 | | - it('renders a <button>', () => { |
38 | | - expect(render(<Button />).type).toEqual('button') |
| 13 | + it('renders a <button>', async () => { |
| 14 | + const container = render(<Button>Default</Button>) |
| 15 | + const button = await container.findByRole('button') |
| 16 | + expect(button.textContent).toEqual('Default') |
39 | 17 | }) |
40 | 18 |
|
41 | 19 | it('should have no axe violations', async () => { |
42 | | - const {container} = HTMLRender(<Button>Click here</Button>) |
| 20 | + const {container} = render(<Button>Click here</Button>) |
43 | 21 | const results = await axe(container) |
44 | 22 | expect(results).toHaveNoViolations() |
45 | 23 | cleanup() |
46 | 24 | }) |
47 | 25 |
|
48 | | - it('preserves "onClick" prop', () => { |
49 | | - expect(render(<Button onClick={noop} />).props.onClick).toEqual(noop) |
50 | | - }) |
51 | | - |
52 | | - it('respects width props', () => { |
53 | | - expect(render(<Button sx={{width: 200}} />)).toHaveStyleRule('width', '200px') |
| 26 | + it('preserves "onClick" prop', async () => { |
| 27 | + const onClick = jest.fn() |
| 28 | + const container = render(<Button onClick={onClick}>Noop</Button>) |
| 29 | + const button = await container.findByRole('button') |
| 30 | + fireEvent.click(button) |
| 31 | + expect(onClick).toHaveBeenCalledTimes(1) |
54 | 32 | }) |
55 | 33 |
|
56 | | - it('respects the "disabled" prop', () => { |
57 | | - const item = render(<Button disabled />) |
58 | | - expect(item.props.disabled).toEqual(true) |
59 | | - expect(item).toMatchSnapshot() |
| 34 | + it('respects width props', async () => { |
| 35 | + const container = render(<Button sx={{width: 200}}>Block</Button>) |
| 36 | + const button = await container.findByRole('button') |
| 37 | + expect(button).toHaveStyleRule('width', '200px') |
60 | 38 | }) |
61 | 39 |
|
62 | | - it('respects the "variant" prop', () => { |
63 | | - expect(render(<Button variant="small" />)).toHaveStyleRule('font-size', '12px') |
64 | | - expect(render(<Button variant="large" />)).toHaveStyleRule('font-size', '16px') |
65 | | - }) |
66 | | - |
67 | | - it('respects the "fontSize" prop over the "variant" prop', () => { |
68 | | - expect(render(<Button variant="small" sx={{fontSize: 20}} />)).toHaveStyleRule('font-size', '20px') |
69 | | - }) |
70 | | -}) |
71 | | - |
72 | | -describe('ButtonPrimary', () => { |
73 | | - behavesAsComponent({Component: ButtonPrimary}) |
74 | | - |
75 | | - it('renders a <button>', () => { |
76 | | - expect(render(<ButtonPrimary />).type).toEqual('button') |
| 40 | + it('respects the "disabled" prop', async () => { |
| 41 | + const onClick = jest.fn() |
| 42 | + const container = render( |
| 43 | + <Button onClick={onClick} disabled> |
| 44 | + Disabled |
| 45 | + </Button> |
| 46 | + ) |
| 47 | + const button = await container.findByRole('button') |
| 48 | + expect(button.hasAttribute('disabled')).toEqual(true) |
| 49 | + fireEvent.click(button) |
| 50 | + expect(onClick).toHaveBeenCalledTimes(0) |
77 | 51 | }) |
78 | 52 |
|
79 | | - it('renders correct disabled styles', () => { |
80 | | - const item = render(<ButtonPrimary disabled />) |
81 | | - expect(item).toMatchSnapshot() |
| 53 | + it('respects the "variant" prop', async () => { |
| 54 | + const container = render(<Button size="small">Smol</Button>) |
| 55 | + const button = await container.findByRole('button') |
| 56 | + expect(button).toHaveStyleRule('font-size', '12px') |
82 | 57 | }) |
83 | | -}) |
84 | | - |
85 | | -describe('ButtonDanger', () => { |
86 | | - behavesAsComponent({Component: ButtonDanger}) |
87 | 58 |
|
88 | | - it('renders a <button>', () => { |
89 | | - expect(render(<ButtonDanger />).type).toEqual('button') |
| 59 | + it('respects the "fontSize" prop over the "variant" prop', async () => { |
| 60 | + const container = render( |
| 61 | + <Button size="small" sx={{fontSize: 20}}> |
| 62 | + Big Smol |
| 63 | + </Button> |
| 64 | + ) |
| 65 | + const button = await container.findByRole('button') |
| 66 | + expect(button).toHaveStyleRule('font-size', '20px') |
90 | 67 | }) |
91 | 68 |
|
92 | | - it('renders correct disabled styles', () => { |
93 | | - const item = render(<ButtonDanger disabled />) |
94 | | - expect(item).toMatchSnapshot() |
| 69 | + it('styles primary button appropriately', async () => { |
| 70 | + const container = render(<Button variant="primary">Primary</Button>) |
| 71 | + const button = await container.findByRole('button') |
| 72 | + expect(button).toMatchSnapshot() |
95 | 73 | }) |
96 | | -}) |
97 | | - |
98 | | -describe('ButtonOutline', () => { |
99 | | - behavesAsComponent({Component: ButtonOutline}) |
100 | 74 |
|
101 | | - it('renders a <button> by default', () => { |
102 | | - expect(render(<ButtonOutline />).type).toEqual('button') |
| 75 | + it('styles invisible button appropriately', async () => { |
| 76 | + const container = render(<Button variant="invisible">Invisible</Button>) |
| 77 | + const button = await container.findByRole('button') |
| 78 | + expect(button).toMatchSnapshot() |
103 | 79 | }) |
104 | 80 |
|
105 | | - it('renders correct disabled styles', () => { |
106 | | - const item = render(<ButtonOutline disabled />) |
107 | | - expect(item).toMatchSnapshot() |
| 81 | + it('styles danger button appropriately', async () => { |
| 82 | + const container = render(<Button variant="danger">Danger</Button>) |
| 83 | + const button = await container.findByRole('button') |
| 84 | + expect(button).toMatchSnapshot() |
108 | 85 | }) |
109 | | -}) |
110 | 86 |
|
111 | | -describe('ButtonInvisible', () => { |
112 | | - behavesAsComponent({Component: ButtonOutline}) |
113 | | - |
114 | | - it('renders a <button> by default', () => { |
115 | | - expect(render(<ButtonInvisible />).type).toEqual('button') |
| 87 | + it('styles outline button appropriately', async () => { |
| 88 | + const container = render(<Button variant="outline">Outline</Button>) |
| 89 | + const button = await container.findByRole('button') |
| 90 | + expect(button).toMatchSnapshot() |
116 | 91 | }) |
117 | 92 |
|
118 | | - it('renders correct disabled styles', () => { |
119 | | - const item = render(<ButtonInvisible disabled />) |
120 | | - expect(item).toMatchSnapshot() |
| 93 | + it('styles icon only button to make it a square', async () => { |
| 94 | + const container = render(<IconButton icon={SearchIcon} iconLabel="Search icon only button" />) |
| 95 | + const IconOnlyButton = await container.findByRole('button') |
| 96 | + expect(IconOnlyButton).toHaveStyleRule('padding-right', '8px') |
121 | 97 | }) |
122 | 98 | }) |
123 | | - |
124 | | -describe('ButtonGroup', () => { |
125 | | - behavesAsComponent({Component: ButtonGroup}) |
126 | | -}) |
127 | | - |
128 | | -describe('ButtonTableList', () => { |
129 | | - behavesAsComponent({Component: ButtonTableList}) |
130 | | -}) |
0 commit comments