11import React from 'react'
22import { TabNav } from '..'
3- import { behavesAsComponent , checkExports } from '../utils/testing'
4- import { render as HTMLRender , cleanup } from '@testing-library/react'
3+ import { mount , behavesAsComponent , checkExports } from '../utils/testing'
4+ import { fireEvent , render as HTMLRender , cleanup } from '@testing-library/react'
5+ import userEvent from '@testing-library/user-event'
56import { axe , toHaveNoViolations } from 'jest-axe'
67import 'babel-polyfill'
8+ import { Button } from '../Button'
9+ import Box from '../Box'
710expect . extend ( toHaveNoViolations )
811
912describe ( 'TabNav' , ( ) => {
13+ const tabNavMarkup = (
14+ < Box >
15+ < TabNav >
16+ < TabNav . Link id = "first" href = "#" >
17+ First
18+ </ TabNav . Link >
19+ < TabNav . Link id = "middle" href = "#" selected >
20+ Middle
21+ </ TabNav . Link >
22+ < TabNav . Link id = "last" href = "#" >
23+ Last
24+ </ TabNav . Link >
25+ </ TabNav >
26+ < Button id = "my-button" > My Button</ Button >
27+ </ Box >
28+ )
29+
1030 behavesAsComponent ( { Component : TabNav } )
1131
1232 checkExports ( 'TabNav' , {
@@ -29,4 +49,78 @@ describe('TabNav', () => {
2949 expect ( getByLabelText ( 'stuff' ) ) . toBeTruthy ( )
3050 expect ( getByLabelText ( 'stuff' ) . tagName ) . toEqual ( 'NAV' )
3151 } )
52+
53+ it ( 'selects a tab when tab is loaded' , ( ) => {
54+ const component = mount ( tabNavMarkup )
55+ const tab = component . find ( '#middle' ) . first ( )
56+ expect ( tab . getDOMNode ( ) . classList ) . toContain ( 'selected' )
57+ } )
58+
59+ it ( 'selects next tab when pressing right arrow' , ( ) => {
60+ const { getByText} = HTMLRender ( tabNavMarkup )
61+ const middleTab = getByText ( 'Middle' )
62+ const lastTab = getByText ( 'Last' )
63+
64+ fireEvent . focus ( middleTab )
65+ fireEvent . keyDown ( middleTab , { key : 'ArrowRight' } )
66+
67+ expect ( lastTab ) . toHaveFocus ( )
68+ } )
69+
70+ it ( 'selects previous tab when pressing left arrow' , ( ) => {
71+ const { getByText} = HTMLRender ( tabNavMarkup )
72+ const middleTab = getByText ( 'Middle' )
73+ const firstTab = getByText ( 'First' )
74+
75+ fireEvent . focus ( middleTab )
76+ fireEvent . keyDown ( middleTab , { key : 'ArrowLeft' } )
77+
78+ expect ( firstTab ) . toHaveFocus ( )
79+ } )
80+
81+ it ( 'selects last tab when pressing left arrow on first tab' , ( ) => {
82+ const { getByText} = HTMLRender ( tabNavMarkup )
83+ const firstTab = getByText ( 'First' )
84+ const lastTab = getByText ( 'Last' )
85+
86+ fireEvent . focus ( firstTab )
87+ fireEvent . keyDown ( firstTab , { key : 'ArrowLeft' } )
88+
89+ expect ( lastTab ) . toHaveFocus ( )
90+ } )
91+
92+ it ( 'selects first tab when pressing right arrow on last tab' , ( ) => {
93+ const { getByText} = HTMLRender ( tabNavMarkup )
94+ const lastTab = getByText ( 'Last' )
95+ const firstTab = getByText ( 'First' )
96+
97+ fireEvent . focus ( lastTab )
98+ fireEvent . keyDown ( lastTab , { key : 'ArrowRight' } )
99+
100+ expect ( firstTab ) . toHaveFocus ( )
101+ } )
102+
103+ it ( 'moves focus away from TabNav when pressing tab' , ( ) => {
104+ const { getByText, getByRole} = HTMLRender ( tabNavMarkup )
105+ const middleTab = getByText ( 'Middle' )
106+ const button = getByRole ( 'button' )
107+
108+ userEvent . click ( middleTab )
109+ expect ( middleTab ) . toHaveFocus ( )
110+ userEvent . tab ( )
111+
112+ expect ( button ) . toHaveFocus ( )
113+ } )
114+
115+ it ( 'moves focus to selected tab when TabNav regains focus' , ( ) => {
116+ const { getByText, getByRole} = HTMLRender ( tabNavMarkup )
117+ const middleTab = getByText ( 'Middle' )
118+ const button = getByRole ( 'button' )
119+
120+ userEvent . click ( button )
121+ expect ( button ) . toHaveFocus ( )
122+ userEvent . tab ( { shift : true } )
123+
124+ expect ( middleTab ) . toHaveFocus ( )
125+ } )
32126} )
0 commit comments