1
1
import { HTMLAttributes } from 'react'
2
- import React , { render , cleanup , fireEvent , within } from '@testing-library/react'
2
+ import React , { render , cleanup , within } from '@testing-library/react'
3
3
import '@testing-library/jest-dom'
4
4
import { axe , toHaveNoViolations } from 'jest-axe'
5
5
@@ -57,64 +57,65 @@ describe('SubNav', () => {
57
57
afterEach ( cleanup )
58
58
59
59
it ( 'renders the root element correctly into the document' , ( ) => {
60
- const { getByTestId } = render ( < MockSubNavFixture /> )
60
+ const { getByRole } = render ( < MockSubNavFixture /> )
61
61
62
- expect ( getByTestId ( SubNav . testIds . root ) ) . toBeInTheDocument ( ) // expect the root element to be in the document
63
- expect ( getByTestId ( SubNav . testIds . root ) . tagName ) . toBe ( 'nav' . toUpperCase ( ) ) // expect root to be a <nav> element
62
+ expect ( getByRole ( 'navigation' ) ) . toBeInTheDocument ( )
64
63
} )
65
64
it ( 'renders a title as a link' , ( ) => {
66
- const { getByTestId } = render ( < MockSubNavFixture /> )
65
+ const { getByRole } = render ( < MockSubNavFixture /> )
67
66
68
- expect ( getByTestId ( SubNav . testIds . heading ) ) . toBeInTheDocument ( )
69
- expect ( getByTestId ( SubNav . testIds . heading ) . tagName ) . toBe ( 'a' . toUpperCase ( ) )
70
- expect ( getByTestId ( SubNav . testIds . heading ) ) . toHaveAttribute ( 'href' , headingLink )
67
+ expect ( getByRole ( 'link' , { name : heading } ) ) . toHaveAttribute ( 'href' , headingLink )
71
68
} )
72
69
73
70
it ( 'renders the correct number of links into the document' , ( ) => {
74
- const { getByTestId} = render ( < MockSubNavFixture /> )
75
- expect ( getByTestId ( SubNav . testIds . overlay ) . querySelectorAll ( 'a' ) . length ) . toBe ( mockLinkData . length )
71
+ const { getByRole} = render ( < MockSubNavFixture /> )
72
+
73
+ const list = getByRole ( 'list' )
74
+ const links = within ( list ) . getAllByRole ( 'link' )
75
+
76
+ expect ( links ) . toHaveLength ( mockLinkData . length )
76
77
} )
77
78
78
79
it ( 'has a button that opens the menu when clicked' , async ( ) => {
79
- const { getByTestId } = render ( < MockSubNavFixture /> )
80
+ const { getByRole } = render ( < MockSubNavFixture /> )
80
81
81
- const buttonEl = getByTestId ( 'SubNav-root-button' )
82
- const overlayEl = getByTestId ( SubNav . testIds . overlay )
83
-
84
- expect ( buttonEl ) . toBeInTheDocument ( )
82
+ const buttonEl = getByRole ( 'button' , { name : 'Navigation menu. Current page: page three' } )
83
+ const overlayEl = getByRole ( 'list' )
85
84
86
- // check aria roles are correct by default
85
+ expect ( overlayEl ) . not . toHaveClass ( 'SubNav__links-overlay--open' )
87
86
expect ( buttonEl ) . toHaveAttribute ( 'aria-expanded' , 'false' )
88
87
89
88
userEvent . click ( buttonEl )
90
89
91
90
expect ( overlayEl ) . toHaveClass ( 'SubNav__links-overlay--open' )
92
- // check aria roles have updated
93
91
expect ( buttonEl ) . toHaveAttribute ( 'aria-expanded' , 'true' )
94
92
} )
95
93
96
94
it ( 'closes the overlay when button is pressed again' , ( ) => {
97
- const { getByTestId } = render ( < MockSubNavFixture /> )
95
+ const { getByRole } = render ( < MockSubNavFixture /> )
98
96
99
- const buttonEl = getByTestId ( SubNav . testIds . button )
100
- const overlayEl = getByTestId ( SubNav . testIds . overlay )
97
+ const buttonEl = getByRole ( ' button' , { name : 'Navigation menu. Current page: page three' } )
98
+ const overlayEl = getByRole ( 'list' )
101
99
102
- fireEvent . click ( buttonEl )
100
+ userEvent . click ( buttonEl )
103
101
expect ( overlayEl ) . toHaveClass ( 'SubNav__links-overlay--open' )
104
102
105
- fireEvent . click ( buttonEl )
103
+ userEvent . click ( buttonEl )
106
104
expect ( overlayEl ) . not . toHaveClass ( 'SubNav__links-overlay--open' )
107
105
} )
108
106
109
- it ( 'shows the aria-current text next to the button by default' , ( ) => {
110
- const { getByTestId} = render ( < MockSubNavFixture /> )
107
+ it ( 'includes the aria-current text in the aria-label of the button' , ( ) => {
108
+ const { getByRole} = render ( < MockSubNavFixture /> )
109
+
110
+ const buttonEl = getByRole ( 'button' , { name : 'Navigation menu. Current page: page three' } )
111
+ expect ( buttonEl ) . toBeInTheDocument ( )
112
+ } )
111
113
112
- const buttonEl = getByTestId ( SubNav . testIds . button )
113
- const activeLink = mockLinkData . find ( link => link [ 'aria-current' ] ) as { title : string } | undefined
114
+ it ( 'shows the aria-current text next to the button by default' , ( ) => {
115
+ const { getByRole } = render ( < MockSubNavFixture /> )
114
116
115
- if ( activeLink ) {
116
- expect ( buttonEl ) . toHaveTextContent ( activeLink . title )
117
- }
117
+ const buttonEl = getByRole ( 'button' , { name : 'Navigation menu. Current page: page three' } )
118
+ expect ( buttonEl ) . toHaveTextContent ( 'page three' )
118
119
} )
119
120
120
121
it ( 'has no a11y violations on initial render' , async ( ) => {
@@ -127,7 +128,7 @@ describe('SubNav', () => {
127
128
it ( 'shows subitems when the submenu toggle is activated at large viewports' , async ( ) => {
128
129
mockUseWindowSize . mockImplementation ( ( ) => ( { isLarge : true } ) )
129
130
130
- const { getByRole, getAllByTestId } = render (
131
+ const { getByRole} = render (
131
132
< SubNav fullWidth >
132
133
< SubNav . Link href = "#" aria-current = "page" >
133
134
Copilot
@@ -157,16 +158,14 @@ describe('SubNav', () => {
157
158
expect ( toggleSubmenuButton ) . toHaveFocus ( )
158
159
expect ( toggleSubmenuButton ) . toHaveAttribute ( 'aria-expanded' , 'true' )
159
160
160
- const expanded = getAllByTestId ( SubNav . testIds . subMenu ) [ 0 ]
161
-
162
161
userEvent . tab ( )
163
- expect ( within ( expanded ) . getByRole ( 'link' , { name : 'Copilot feature page one' } ) ) . toHaveFocus ( )
162
+ expect ( getByRole ( 'link' , { name : 'Copilot feature page one' } ) ) . toHaveFocus ( )
164
163
165
164
userEvent . tab ( )
166
- expect ( within ( expanded ) . getByRole ( 'link' , { name : 'Copilot feature page two' } ) ) . toHaveFocus ( )
165
+ expect ( getByRole ( 'link' , { name : 'Copilot feature page two' } ) ) . toHaveFocus ( )
167
166
168
167
userEvent . tab ( )
169
- expect ( within ( expanded ) . getByRole ( 'link' , { name : 'Copilot feature page three' } ) ) . toHaveFocus ( )
168
+ expect ( getByRole ( 'link' , { name : 'Copilot feature page three' } ) ) . toHaveFocus ( )
170
169
171
170
userEvent . tab ( )
172
171
expect ( getByRole ( 'link' , { name : 'Code review' } ) ) . toHaveFocus ( )
0 commit comments