@@ -23,7 +23,7 @@ describe('DsfrTabs', () => {
2323 const tabContents = [ 'Contenu1' , 'Contenu2' , 'Contenu3' , 'Contenu4' ]
2424
2525 // When
26- const { getByText, getByTestId, getAllByRole } = render ( DsfrTabs , {
26+ const { getByText, getByTestId, getAllByRole, getByRole } = render ( DsfrTabs , {
2727 global : {
2828 components : {
2929 VIcon,
@@ -36,17 +36,92 @@ describe('DsfrTabs', () => {
3636 } ,
3737 } )
3838
39+ const tabListEl = getByRole ( 'tablist' )
40+
3941 const firstTabEl = getByTestId ( 'test-tab1' )
4042 const secondTabEl = getByText ( title2 )
4143 const thirdTabEl = getByText ( title3 )
4244 const tabTitleEls = getAllByRole ( 'presentation' )
45+ const tabItemEls = getAllByRole ( 'tab' )
46+
47+ let i = 0
48+ for ( const tabItemEl of tabItemEls ) {
49+ if ( i === 0 ) {
50+ expect ( tabItemEl ) . toHaveAttribute ( 'aria-selected' , 'true' )
51+ } else {
52+ expect ( tabItemEl ) . toHaveAttribute ( 'aria-selected' , 'false' )
53+ }
54+ expect ( tabItemEl ) . toHaveClass ( 'fr-tabs__tab' )
55+ i ++
56+ }
57+
4358 await fireEvent . click ( secondTabEl )
4459 await fireEvent . click ( thirdTabEl )
4560 await fireEvent . click ( secondTabEl )
4661
62+ i = 0
63+ for ( const tabItemEl of tabItemEls ) {
64+ if ( i === 1 ) {
65+ expect ( tabItemEl ) . toHaveAttribute ( 'aria-selected' , 'true' )
66+ } else {
67+ expect ( tabItemEl ) . toHaveAttribute ( 'aria-selected' , 'false' )
68+ }
69+ i ++
70+ }
71+
4772 // Then
4873 expect ( tabTitleEls [ 0 ] ) . toContainElement ( firstTabEl )
4974 expect ( tabTitleEls [ 0 ] . textContent ) . toContain ( title1 )
5075 expect ( tabTitleEls [ 0 ] . textContent ) . toContain ( title1 )
76+ expect ( tabListEl ) . toHaveClass ( 'fr-tabs__list' )
77+ expect ( tabItemEls ) . toHaveLength ( 4 )
78+ } )
79+
80+ it ( 'should render tabs with proper aria-* attributes' , async ( ) => {
81+ // Given
82+ const tabListName = 'Liste d’onglet'
83+ const title1 = 'Titre 1'
84+ const title2 = 'Titre 2'
85+ const title3 = 'Titre 3'
86+
87+ const tabTitles = [
88+ { title : title1 } ,
89+ { title : title2 } ,
90+ { title : title3 } ,
91+ { title : 'Titre 4' } ,
92+ ]
93+
94+ const tabContents = [ 'Contenu1' , 'Contenu2' , 'Contenu3' , 'Contenu4' ]
95+
96+ // When
97+ const { container, getAllByRole, getByRole } = render ( DsfrTabs , {
98+ global : {
99+ components : {
100+ VIcon,
101+ } ,
102+ } ,
103+ props : {
104+ tabListName,
105+ tabTitles,
106+ tabContents,
107+ } ,
108+ } )
109+
110+ getByRole ( 'tablist' )
111+
112+ const tabItemEls = getAllByRole ( 'tab' )
113+ const panelEls = container . querySelectorAll ( '.fr-tabs__panel' )
114+
115+ // Then
116+ expect ( tabItemEls ) . toHaveLength ( tabTitles . length )
117+ expect ( panelEls ) . toHaveLength ( tabTitles . length )
118+ let i = 0
119+ for ( const tabPanelEl of panelEls ) {
120+ expect ( tabPanelEl ) . toHaveAttribute ( 'role' , 'tabpanel' )
121+ expect ( tabItemEls [ i ] ) . toHaveAttribute ( 'role' , 'tab' )
122+ expect ( tabPanelEl ) . toHaveAttribute ( 'aria-labelledby' , tabItemEls [ i ] . id )
123+ expect ( tabItemEls [ i ] ) . toHaveAttribute ( 'aria-controls' , tabPanelEl . id )
124+ i ++
125+ }
51126 } )
52127} )
0 commit comments