1
1
import { render , screen } from "@testing-library/react" ;
2
- import { Abbr , type Pronunciation } from "./Abbr" ;
2
+ import { Abbr } from "./Abbr" ;
3
3
4
4
type AbbreviationData = {
5
5
abbreviation : string ;
6
6
title : string ;
7
7
expectedLabel : string ;
8
8
} ;
9
9
10
- type AssertionInput = AbbreviationData & {
11
- pronunciation : Pronunciation ;
12
- } ;
13
-
14
- function assertAccessibleLabel ( {
15
- abbreviation,
16
- title,
17
- expectedLabel,
18
- pronunciation,
19
- } : AssertionInput ) {
20
- const { unmount } = render (
21
- < Abbr title = { title } pronunciation = { pronunciation } >
22
- { abbreviation }
23
- </ Abbr > ,
24
- ) ;
25
-
26
- screen . getByLabelText ( expectedLabel , { selector : "abbr" } ) ;
27
- unmount ( ) ;
28
- }
29
-
30
10
describe ( Abbr . name , ( ) => {
31
11
it ( "Has an aria-label that equals the title if the abbreviation is shorthand" , ( ) => {
32
12
const sampleShorthands : AbbreviationData [ ] = [
@@ -43,7 +23,18 @@ describe(Abbr.name, () => {
43
23
] ;
44
24
45
25
for ( const shorthand of sampleShorthands ) {
46
- assertAccessibleLabel ( { ...shorthand , pronunciation : "shorthand" } ) ;
26
+ const { unmount } = render (
27
+ < Abbr title = { shorthand . title } pronunciation = "shorthand" >
28
+ { shorthand . abbreviation }
29
+ </ Abbr > ,
30
+ ) ;
31
+
32
+ // The <abbr> element doesn't have any ARIA role semantics baked in,
33
+ // so we have to get a little bit more creative with making sure the
34
+ // expected content is on screen in an accessible way
35
+ const element = screen . getByTitle ( shorthand . title ) ;
36
+ expect ( element ) . toHaveTextContent ( shorthand . expectedLabel ) ;
37
+ unmount ( ) ;
47
38
}
48
39
} ) ;
49
40
@@ -67,7 +58,15 @@ describe(Abbr.name, () => {
67
58
] ;
68
59
69
60
for ( const acronym of sampleAcronyms ) {
70
- assertAccessibleLabel ( { ...acronym , pronunciation : "acronym" } ) ;
61
+ const { unmount } = render (
62
+ < Abbr title = { acronym . title } pronunciation = "acronym" >
63
+ { acronym . abbreviation }
64
+ </ Abbr > ,
65
+ ) ;
66
+
67
+ const element = screen . getByTitle ( acronym . title ) ;
68
+ expect ( element ) . toHaveTextContent ( acronym . expectedLabel ) ;
69
+ unmount ( ) ;
71
70
}
72
71
} ) ;
73
72
@@ -91,7 +90,15 @@ describe(Abbr.name, () => {
91
90
] ;
92
91
93
92
for ( const initialism of sampleInitialisms ) {
94
- assertAccessibleLabel ( { ...initialism , pronunciation : "initialism" } ) ;
93
+ const { unmount } = render (
94
+ < Abbr title = { initialism . title } pronunciation = "initialism" >
95
+ { initialism . abbreviation }
96
+ </ Abbr > ,
97
+ ) ;
98
+
99
+ const element = screen . getByTitle ( initialism . title ) ;
100
+ expect ( element ) . toHaveTextContent ( initialism . expectedLabel ) ;
101
+ unmount ( ) ;
95
102
}
96
103
} ) ;
97
104
} ) ;
0 commit comments