@@ -3,7 +3,7 @@ import userEvent from '@testing-library/user-event'
3
3
import '@testing-library/jest-dom'
4
4
import { axe , toHaveNoViolations } from 'jest-axe'
5
5
6
- import { FAQ , FAQGroup } from './'
6
+ import { FAQ , FAQGroup , type FAQGroupProps } from './'
7
7
8
8
expect . extend ( toHaveNoViolations )
9
9
@@ -43,10 +43,27 @@ describe('FAQGroup', () => {
43
43
} ,
44
44
] ,
45
45
} ,
46
+ {
47
+ heading : 'mock heading 3' ,
48
+ faqs : [
49
+ {
50
+ question : 'mock question 7' ,
51
+ answer : 'mock answer 7' ,
52
+ } ,
53
+ {
54
+ question : 'mock question 8' ,
55
+ answer : 'mock answer 8' ,
56
+ } ,
57
+ {
58
+ question : 'mock question 9' ,
59
+ answer : 'mock answer 9' ,
60
+ } ,
61
+ ] ,
62
+ } ,
46
63
]
47
64
48
- const Component = ( ) => (
49
- < FAQGroup data-testid = "root" >
65
+ const Component = ( props : FAQGroupProps ) => (
66
+ < FAQGroup data-testid = "root" { ... props } >
50
67
< FAQGroup . Heading > Frequently asked questions</ FAQGroup . Heading >
51
68
{ testData . map ( ( group , index ) => (
52
69
< FAQ key = { index } >
@@ -66,76 +83,84 @@ describe('FAQGroup', () => {
66
83
67
84
afterEach ( cleanup )
68
85
69
- it ( 'renders groups of FAQS into the document, showing the first group only' , ( ) => {
70
- const { getByTestId, getAllByText, getByText} = render ( < Component /> )
71
-
72
- const rootEl = getByTestId ( 'root' )
73
- const mainHeading = getByText ( 'Frequently asked questions' )
74
-
75
- const [ , headingAsTab , headingAsSubHead ] = getAllByText ( 'mock heading 1' )
86
+ it ( 'has no a11y violations' , async ( ) => {
87
+ const { container} = render ( < Component /> )
88
+ const results = await axe ( container )
76
89
77
- expect ( rootEl ) . toBeInTheDocument ( )
78
- expect ( mainHeading ) . toBeInTheDocument ( )
79
- expect ( headingAsTab ) . toBeInTheDocument ( )
80
- expect ( headingAsSubHead ) . toBeInTheDocument ( )
90
+ expect ( results ) . toHaveNoViolations ( )
81
91
} )
82
92
83
93
it ( 'selects the first tab by default' , ( ) => {
84
- const { getByTestId } = render ( < Component /> )
94
+ const { getByRole , queryByRole } = render ( < Component /> )
85
95
86
- const buttonOneEl = getByTestId ( 'FAQGroup-tab-1 ')
87
- const panelOne = getByTestId ( 'FAQGroup-tab-panel-1 ')
88
- const panelTwo = getByTestId ( 'FAQGroup-tab-panel-2 ')
96
+ expect ( getByRole ( 'tab' , { name : 'mock heading 1' } ) ) . toHaveAttribute ( 'aria-selected' , 'true ')
97
+ expect ( getByRole ( 'tab' , { name : 'mock heading 2' } ) ) . toHaveAttribute ( 'aria-selected' , 'false ')
98
+ expect ( getByRole ( 'tab' , { name : 'mock heading 3' } ) ) . toHaveAttribute ( 'aria-selected' , 'false ')
89
99
90
- expect ( buttonOneEl ) . toBeInTheDocument ( )
91
- expect ( buttonOneEl ) . toHaveAttribute ( 'aria-selected' , 'true' )
92
- expect ( panelOne ) . toBeVisible ( )
93
- expect ( panelTwo ) . not . toBeVisible ( )
100
+ expect ( getByRole ( 'tabpanel' , { name : 'mock heading 1' } ) ) . toBeVisible ( )
101
+ expect ( queryByRole ( 'tabpanel' , { name : 'mock heading 2' } ) ) . not . toBeInTheDocument ( )
102
+ expect ( queryByRole ( 'tabpanel' , { name : 'mock heading 3' } ) ) . not . toBeInTheDocument ( )
94
103
} )
95
104
96
- it ( 'selects the first tab by default' , async ( ) => {
97
- const { getByTestId } = render ( < Component /> )
105
+ it ( 'changes selected tab on ArrowUp and ArrowDown key presses' , ( ) => {
106
+ const { getByRole , queryByRole } = render ( < Component /> )
98
107
99
- const buttonOneEl = getByTestId ( 'FAQGroup-tab-1' )
100
- const buttonTwoEl = getByTestId ( 'FAQGroup-tab-2' )
101
- const panelOne = getByTestId ( 'FAQGroup-tab-panel-1' )
102
- const panelTwo = getByTestId ( 'FAQGroup-tab-panel-2' )
108
+ const headings = [ 'mock heading 1' , 'mock heading 2' , 'mock heading 3' ]
109
+ const assertSelectedTabIndex = ( selectedTabIndex : number ) => {
110
+ for ( let i = 0 ; i < headings . length ; i ++ ) {
111
+ const heading = headings [ i ]
103
112
104
- await userEvent . click ( buttonTwoEl )
113
+ if ( i === selectedTabIndex ) {
114
+ expect ( getByRole ( 'tab' , { name : heading } ) ) . toHaveAttribute ( 'aria-selected' , 'true' )
115
+ expect ( getByRole ( 'tabpanel' , { name : heading } ) ) . toBeVisible ( )
116
+ } else {
117
+ expect ( getByRole ( 'tab' , { name : heading } ) ) . toHaveAttribute ( 'aria-selected' , 'false' )
118
+ expect ( queryByRole ( 'tabpanel' , { name : heading } ) ) . not . toBeInTheDocument ( )
119
+ }
120
+ }
121
+ }
105
122
106
- expect ( buttonOneEl ) . toHaveAttribute ( 'aria-selected' , 'false' )
107
- expect ( buttonTwoEl ) . toHaveAttribute ( 'aria-selected' , 'true' )
108
- expect ( panelTwo ) . toBeVisible ( )
109
- expect ( panelOne ) . not . toBeVisible ( )
110
- } )
123
+ assertSelectedTabIndex ( 0 )
111
124
112
- it ( 'has no a11y violations' , async ( ) => {
113
- const { container} = render ( < Component /> )
114
- const results = await axe ( container )
125
+ userEvent . type ( getByRole ( 'tab' , { name : 'mock heading 1' } ) , '{arrowdown}' )
126
+ assertSelectedTabIndex ( 1 )
115
127
116
- expect ( results ) . toHaveNoViolations ( )
128
+ userEvent . type ( getByRole ( 'tab' , { name : 'mock heading 2' } ) , '{arrowdown}' )
129
+ assertSelectedTabIndex ( 2 )
130
+
131
+ userEvent . type ( getByRole ( 'tab' , { name : 'mock heading 3' } ) , '{arrowup}' )
132
+ assertSelectedTabIndex ( 1 )
133
+
134
+ userEvent . type ( getByRole ( 'tab' , { name : 'mock heading 2' } ) , '{arrowup}' )
135
+ assertSelectedTabIndex ( 0 )
136
+
137
+ userEvent . type ( getByRole ( 'tab' , { name : 'mock heading 1' } ) , '{arrowup}' )
138
+ assertSelectedTabIndex ( 2 )
117
139
} )
118
140
119
- it ( 'changes selected tab on ArrowUp and ArrowDown key presses' , async ( ) => {
120
- const { getByTestId} = render ( < Component /> )
121
- const firstTabButton = getByTestId ( 'FAQGroup-tab-1' )
122
- const secondTabButton = getByTestId ( 'FAQGroup-tab-2' )
123
- const lastTabButton = getByTestId ( `FAQGroup-tab-2` )
141
+ it ( 'calls `tabAttributes` with the correct arguments' , ( ) => {
142
+ const mockTabAttributes = jest . fn ( ( _ , i ) => ( {
143
+ 'data-tab-index' : i ,
144
+ } ) )
145
+
146
+ render ( < Component tabAttributes = { mockTabAttributes } /> )
147
+ const mockCalls = mockTabAttributes . mock . calls
124
148
125
- await userEvent . type ( firstTabButton , '{arrowdown}' )
126
- expect ( secondTabButton ) . toHaveAttribute ( 'aria-selected' , 'true' )
127
- expect ( getByTestId ( 'FAQGroup-tab-panel-2' ) ) . not . toHaveAttribute ( 'hidden' )
149
+ expect ( mockTabAttributes ) . toHaveBeenCalledTimes ( 3 )
150
+ expect ( mockCalls [ 0 ] ) . toEqual ( [ 'mock heading 1' , 0 ] )
151
+ expect ( mockCalls [ 1 ] ) . toEqual ( [ 'mock heading 2' , 1 ] )
152
+ expect ( mockCalls [ 2 ] ) . toEqual ( [ 'mock heading 3' , 2 ] )
153
+ } )
128
154
129
- await userEvent . type ( secondTabButton , '{arrowup}' )
130
- expect ( firstTabButton ) . toHaveAttribute ( 'aria-selected' , 'true' )
131
- expect ( firstTabButton ) . not . toHaveAttribute ( 'hidden' )
155
+ it ( 'adds props to the tabs when `tabAttributes` is provided' , ( ) => {
156
+ const mockTabAttributes = jest . fn ( children => ( {
157
+ 'data-tab-heading' : children ,
158
+ } ) )
132
159
133
- await userEvent . type ( firstTabButton , '{arrowup}' )
134
- expect ( lastTabButton ) . toHaveAttribute ( 'aria-selected' , 'true' )
135
- expect ( lastTabButton ) . not . toHaveAttribute ( 'hidden' )
160
+ const { getByRole} = render ( < Component tabAttributes = { mockTabAttributes } /> )
136
161
137
- await userEvent . type ( lastTabButton , '{arrowdown} ' )
138
- expect ( firstTabButton ) . toHaveAttribute ( 'aria-selected ' , 'true ' )
139
- expect ( firstTabButton ) . not . toHaveAttribute ( 'hidden ' )
162
+ expect ( getByRole ( 'tab' , { name : 'mock heading 1' } ) ) . toHaveAttribute ( 'data-tab-heading' , 'mock heading 1 ' )
163
+ expect ( getByRole ( 'tab' , { name : 'mock heading 2' } ) ) . toHaveAttribute ( 'data-tab-heading ' , 'mock heading 2 ' )
164
+ expect ( getByRole ( 'tab' , { name : 'mock heading 3' } ) ) . toHaveAttribute ( 'data-tab-heading' , 'mock heading 3 ')
140
165
} )
141
166
} )
0 commit comments