Skip to content

Commit 6aaa78f

Browse files
committed
Fix stories and tests. Also add tooltip story
1 parent 5136bb5 commit 6aaa78f

File tree

4 files changed

+71
-28
lines changed

4 files changed

+71
-28
lines changed

src/Button/Button.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export const disabledButton = ({...args}: ButtonProps) => {
180180
</Button>
181181
</Box>
182182
<Box mb={2}>
183-
<IconButton disabled icon={() => <XIcon />} iconLabel="Close" {...args} />
183+
<IconButton disabled icon={() => <XIcon />} aria-label="Close" {...args} />
184184
</Box>
185185
</>
186186
)

src/Button/types.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export type Size = 'small' | 'medium' | 'large'
1111

1212
type StyledButtonProps = ComponentPropsWithRef<typeof StyledButton>
1313

14+
interface ButtonA11yProps {
15+
ariaLabel: string
16+
ariaLabelledby: string
17+
}
18+
1419
export type ButtonBaseProps = {
1520
/**
1621
* Determine's the styles on a button one of 'default' | 'primary' | 'invisible' | 'danger'
@@ -40,12 +45,8 @@ export type ButtonProps = {
4045
children: React.ReactNode
4146
} & ButtonBaseProps
4247

43-
export type IconButtonProps = {
44-
/**
45-
* This is to be used if it is an icon-only button. Will make text visually hidden
46-
*/
48+
export type IconButtonProps = Partial<ButtonA11yProps> & {
4749
icon: React.FunctionComponent<IconProps>
48-
'aria-label': string
4950
} & ButtonBaseProps
5051

5152
// adopted from React.AnchorHTMLAttributes

src/__tests__/Button.test.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ expect.extend(toHaveNoViolations)
1010
describe('Button', () => {
1111
behavesAsComponent({Component: Button, options: {skipAs: true}})
1212

13-
it('renders a <button>', async () => {
13+
it('renders a <button>', () => {
1414
const container = render(<Button>Default</Button>)
15-
const button = await container.findByRole('button')
15+
const button = container.getByRole('button')
1616
expect(button.textContent).toEqual('Default')
1717
})
1818

@@ -23,77 +23,82 @@ describe('Button', () => {
2323
cleanup()
2424
})
2525

26-
it('preserves "onClick" prop', async () => {
26+
it('preserves "onClick" prop', () => {
2727
const onClick = jest.fn()
2828
const container = render(<Button onClick={onClick}>Noop</Button>)
29-
const button = await container.findByRole('button')
29+
const button = container.getByRole('button')
3030
fireEvent.click(button)
3131
expect(onClick).toHaveBeenCalledTimes(1)
3232
})
3333

34-
it('respects width props', async () => {
34+
it('respects width props', () => {
3535
const container = render(<Button sx={{width: 200}}>Block</Button>)
36-
const button = await container.findByRole('button')
36+
const button = container.getByRole('button')
3737
expect(button).toHaveStyleRule('width', '200px')
3838
})
3939

40-
it('respects the "disabled" prop', async () => {
40+
it('respects the "disabled" prop', () => {
4141
const onClick = jest.fn()
4242
const container = render(
4343
<Button onClick={onClick} disabled>
4444
Disabled
4545
</Button>
4646
)
47-
const button = await container.findByRole('button')
47+
const button = container.getByRole('button')
4848
expect(button.hasAttribute('disabled')).toEqual(true)
4949
fireEvent.click(button)
5050
expect(onClick).toHaveBeenCalledTimes(0)
5151
})
5252

53-
it('respects the "variant" prop', async () => {
53+
it('respects the "variant" prop', () => {
5454
const container = render(<Button size="small">Smol</Button>)
55-
const button = await container.findByRole('button')
55+
const button = container.getByRole('button')
5656
expect(button).toHaveStyleRule('font-size', '12px')
5757
})
5858

59-
it('respects the "fontSize" prop over the "variant" prop', async () => {
59+
it('respects the "fontSize" prop over the "variant" prop', () => {
6060
const container = render(
6161
<Button size="small" sx={{fontSize: 20}}>
6262
Big Smol
6363
</Button>
6464
)
65-
const button = await container.findByRole('button')
65+
const button = container.getByRole('button')
6666
expect(button).toHaveStyleRule('font-size', '20px')
6767
})
6868

69-
it('styles primary button appropriately', async () => {
69+
it('styles primary button appropriately', () => {
7070
const container = render(<Button variant="primary">Primary</Button>)
71-
const button = await container.findByRole('button')
71+
const button = container.getByRole('button')
7272
expect(button).toMatchSnapshot()
7373
})
7474

75-
it('styles invisible button appropriately', async () => {
75+
it('styles invisible button appropriately', () => {
7676
const container = render(<Button variant="invisible">Invisible</Button>)
77-
const button = await container.findByRole('button')
77+
const button = container.getByRole('button')
7878
expect(button).toMatchSnapshot()
7979
})
8080

81-
it('styles danger button appropriately', async () => {
81+
it('styles danger button appropriately', () => {
8282
const container = render(<Button variant="danger">Danger</Button>)
83-
const button = await container.findByRole('button')
83+
const button = container.getByRole('button')
8484
expect(button).toMatchSnapshot()
8585
})
8686

87-
it('styles outline button appropriately', async () => {
87+
it('styles outline button appropriately', () => {
8888
const container = render(<Button variant="outline">Outline</Button>)
89-
const button = await container.findByRole('button')
89+
const button = container.getByRole('button')
9090
expect(button).toMatchSnapshot()
9191
})
9292

93-
it('styles icon only button to make it a square', async () => {
93+
it('styles icon only button to make it a square', () => {
9494
const container = render(<IconButton icon={SearchIcon} aria-label="Search button" />)
95-
const IconOnlyButton = await container.findByRole('button')
95+
const IconOnlyButton = container.getByRole('button')
9696
expect(IconOnlyButton).toHaveStyleRule('padding-right', '8px')
9797
expect(IconOnlyButton).toMatchSnapshot()
9898
})
99+
it('makes sure icon button has an aria-label', () => {
100+
const container = render(<IconButton icon={SearchIcon} aria-label="Search button" />)
101+
const IconOnlyButton = container.getByLabelText('Search button')
102+
expect(IconOnlyButton).toBeTruthy()
103+
})
99104
})

src/stories/Tooltip.stories.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import {Meta} from '@storybook/react'
3+
import {BaseStyles, ThemeProvider, IconButton} from '..'
4+
import Box from '../Box'
5+
import Tooltip from '../Tooltip'
6+
import {SearchIcon} from '@primer/octicons-react'
7+
8+
export default {
9+
title: 'Tooltip/Default',
10+
component: Tooltip,
11+
12+
decorators: [
13+
Story => {
14+
return (
15+
<ThemeProvider>
16+
<BaseStyles>
17+
<Story />
18+
</BaseStyles>
19+
</ThemeProvider>
20+
)
21+
}
22+
]
23+
} as Meta
24+
25+
export const TextTooltip = () => (
26+
<Box borderWidth="1px" borderStyle="solid" borderColor="border.default" borderRadius={2} p={3}>
27+
<Tooltip aria-label="Hello, Tooltip!">Text with a tooltip</Tooltip>
28+
</Box>
29+
)
30+
31+
export const IconButtonTooltip = () => (
32+
<Box sx={{p: 5}}>
33+
<Tooltip aria-label="Search">
34+
<IconButton icon={SearchIcon} aria-label="Search" />
35+
</Tooltip>
36+
</Box>
37+
)

0 commit comments

Comments
 (0)