1
1
import React from 'react'
2
- import { render } from '@testing-library/react'
2
+ import { render , screen } from '@testing-library/react'
3
3
import userEvent from '@testing-library/user-event'
4
4
import type { IconProps } from '@primer/octicons-react'
5
5
import {
@@ -77,22 +77,26 @@ describe('UnderlineNav', () => {
77
77
default : undefined ,
78
78
UnderlineNav,
79
79
} )
80
+
80
81
it ( 'renders aria-current attribute to be pages when an item is selected' , ( ) => {
81
82
const { getByRole} = render ( < ResponsiveUnderlineNav /> )
82
83
const selectedNavLink = getByRole ( 'link' , { name : 'Code' } )
83
84
expect ( selectedNavLink . getAttribute ( 'aria-current' ) ) . toBe ( 'page' )
84
85
} )
86
+
85
87
it ( 'renders aria-label attribute correctly' , ( ) => {
86
88
const { container, getByRole} = render ( < ResponsiveUnderlineNav /> )
87
89
expect ( container . getElementsByTagName ( 'nav' ) . length ) . toEqual ( 1 )
88
90
const nav = getByRole ( 'navigation' )
89
91
expect ( nav . getAttribute ( 'aria-label' ) ) . toBe ( 'Repository' )
90
92
} )
93
+
91
94
it ( 'renders icons correctly' , ( ) => {
92
95
const { getByRole} = render ( < ResponsiveUnderlineNav /> )
93
96
const nav = getByRole ( 'navigation' )
94
97
expect ( nav . getElementsByTagName ( 'svg' ) . length ) . toEqual ( 7 )
95
98
} )
99
+
96
100
it ( 'fires onSelect on click' , async ( ) => {
97
101
const onSelect = jest . fn ( )
98
102
const { getByRole} = render (
@@ -107,6 +111,7 @@ describe('UnderlineNav', () => {
107
111
await user . click ( item )
108
112
expect ( onSelect ) . toHaveBeenCalledTimes ( 1 )
109
113
} )
114
+
110
115
it ( 'fires onSelect on keypress' , async ( ) => {
111
116
const onSelect = jest . fn ( )
112
117
const { getByRole} = render (
@@ -128,27 +133,31 @@ describe('UnderlineNav', () => {
128
133
await user . keyboard ( ' ' ) // space
129
134
expect ( onSelect ) . toHaveBeenCalledTimes ( 3 )
130
135
} )
136
+
131
137
it ( 'respects counter prop' , ( ) => {
132
138
const { getByRole} = render ( < ResponsiveUnderlineNav /> )
133
139
const item = getByRole ( 'link' , { name : 'Issues (120)' } )
134
140
const counter = item . getElementsByTagName ( 'span' ) [ 3 ]
135
141
expect ( counter . textContent ) . toBe ( '120' )
136
142
expect ( counter ) . toHaveAttribute ( 'aria-hidden' , 'true' )
137
143
} )
144
+
138
145
it ( 'renders the content of visually hidden span properly for screen readers' , ( ) => {
139
146
const { getByRole} = render ( < ResponsiveUnderlineNav /> )
140
147
const item = getByRole ( 'link' , { name : 'Issues (120)' } )
141
148
const counter = item . getElementsByTagName ( 'span' ) [ 4 ]
142
149
// non breaking space unified code
143
150
expect ( counter . textContent ) . toBe ( '\u00A0(120)' )
144
151
} )
152
+
145
153
it ( 'respects loadingCounters prop' , ( ) => {
146
154
const { getByRole} = render ( < ResponsiveUnderlineNav loadingCounters = { true } /> )
147
155
const item = getByRole ( 'link' , { name : 'Actions' } )
148
156
const loadingCounter = item . getElementsByTagName ( 'span' ) [ 2 ]
149
157
expect ( loadingCounter . className ) . toContain ( 'LoadingCounter' )
150
158
expect ( loadingCounter . textContent ) . toBe ( '' )
151
159
} )
160
+
152
161
it ( 'renders a visually hidden h2 heading for screen readers when aria-label is present' , ( ) => {
153
162
const { getByRole} = render ( < ResponsiveUnderlineNav /> )
154
163
const heading = getByRole ( 'heading' , { name : 'Repository navigation' } )
@@ -157,6 +166,7 @@ describe('UnderlineNav', () => {
157
166
expect ( heading . className ) . toContain ( 'VisuallyHidden' )
158
167
expect ( heading . textContent ) . toBe ( 'Repository navigation' )
159
168
} )
169
+
160
170
it ( 'throws an error when there are multiple items that have aria-current' , ( ) => {
161
171
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( )
162
172
expect ( ( ) => {
@@ -186,6 +196,22 @@ describe('UnderlineNav', () => {
186
196
// We are expecting a left value back, that way we know the `getAnchoredPosition` ran.
187
197
expect ( results ) . toEqual ( expect . objectContaining ( { left : 0 } ) )
188
198
} )
199
+
200
+ it ( 'should support icons passed in as an element' , ( ) => {
201
+ render (
202
+ < UnderlineNav aria-label = "Repository" >
203
+ < UnderlineNav . Item aria-current = "page" icon = { < CodeIcon aria-label = "Page one icon" /> } >
204
+ Page one
205
+ </ UnderlineNav . Item >
206
+ < UnderlineNav . Item icon = { < IssueOpenedIcon aria-label = "Page two icon" /> } > Page two</ UnderlineNav . Item >
207
+ < UnderlineNav . Item icon = { < GitPullRequestIcon aria-label = "Page three icon" /> } > Page three</ UnderlineNav . Item >
208
+ </ UnderlineNav > ,
209
+ )
210
+
211
+ expect ( screen . getByLabelText ( 'Page one icon' ) ) . toBeInTheDocument ( )
212
+ expect ( screen . getByLabelText ( 'Page two icon' ) ) . toBeInTheDocument ( )
213
+ expect ( screen . getByLabelText ( 'Page three icon' ) ) . toBeInTheDocument ( )
214
+ } )
189
215
} )
190
216
191
217
describe ( 'Keyboard Navigation' , ( ) => {
0 commit comments