Skip to content

Commit

Permalink
feat(TabSet): Set tabIndex and aria-hidden based on active
Browse files Browse the repository at this point in the history
  • Loading branch information
m7kvqbe1 committed Jul 15, 2020
1 parent c8f2d4d commit d6358db
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const TabContent: React.FC<TabContentProps> = ({
className={classes}
role="tabpanel"
aria-labelledby={tabId}
aria-hidden={!isActive}
tabIndex={0}
data-testid="content"
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const TabItem = forwardRef<HTMLLIElement, TabItemProps>(
role="tab"
aria-controls={tabId}
aria-selected={!!isActive}
tabIndex={!isActive ? -1 : 0}
>
<button className={tabClasses} onClick={handleClick}>
<div>{children}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@ describe('TabSet', () => {
expect(wrapper.queryAllByTestId('content').length).toEqual(2)
})

it('should set the `tabIndex` values correctly', () => {
expect(wrapper.getAllByTestId('tab')[0]).toHaveAttribute(
'tabIndex',
'0'
)

expect(wrapper.getAllByTestId('tab')[1]).toHaveAttribute(
'tabIndex',
'-1'
)
})

it('should set the `aria-hidden` attributes correctly', () => {
expect(wrapper.getAllByTestId('content')[0]).toHaveAttribute(
'aria-hidden',
'false'
)

expect(wrapper.getAllByTestId('content')[1]).toHaveAttribute(
'aria-hidden',
'true'
)
})

describe('when the user clicks on a tab', () => {
beforeEach(() => {
wrapper.getByText('Title 2').click()
Expand All @@ -102,6 +126,30 @@ describe('TabSet', () => {
expect(onChangeSpy).toHaveBeenCalledTimes(1)
expect(onChangeSpy).toHaveBeenCalledWith(1)
})

it('should set the first tab `tabIndex` to -1', () => {
expect(wrapper.getAllByTestId('tab')[0]).toHaveAttribute(
'tabIndex',
'-1'
)

expect(wrapper.getAllByTestId('tab')[1]).toHaveAttribute(
'tabIndex',
'0'
)
})

it('should set the `aria-hidden` attributes correctly', () => {
expect(wrapper.getAllByTestId('content')[0]).toHaveAttribute(
'aria-hidden',
'true'
)

expect(wrapper.getAllByTestId('content')[1]).toHaveAttribute(
'aria-hidden',
'false'
)
})
})
})

Expand Down

0 comments on commit d6358db

Please sign in to comment.