@@ -6,7 +6,7 @@ import {Banner} from '../Banner'
66describe ( 'Banner' , ( ) => {
77 it ( 'should render as a region element' , ( ) => {
88 render ( < Banner title = "test" /> )
9- expect ( screen . getByRole ( 'region' , { name : 'Information ' } ) ) . toBeInTheDocument ( )
9+ expect ( screen . getByRole ( 'region' , { name : 'test ' } ) ) . toBeInTheDocument ( )
1010 expect ( screen . getByRole ( 'heading' , { name : 'test' } ) ) . toBeInTheDocument ( )
1111 } )
1212
@@ -15,34 +15,41 @@ describe('Banner', () => {
1515 expect ( render ( < Element /> ) . container . firstChild ) . toHaveClass ( 'test-class-name' )
1616 } )
1717
18- it ( 'should label the landmark element with the corresponding variant label text' , ( ) => {
19- render ( < Banner title = "test" /> )
20- expect ( screen . getByRole ( 'region' ) ) . toEqual ( screen . getByLabelText ( 'Information' ) )
18+ it ( 'should label the landmark element with the title by default' , ( ) => {
19+ render ( < Banner title = "My Banner Title" /> )
20+ const region = screen . getByRole ( 'region' , { name : 'My Banner Title' } )
21+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' )
22+ expect ( region ) . not . toHaveAttribute ( 'aria-label' )
2123 } )
2224
23- it ( 'should label the landmark element with the label for the critical variant' , ( ) => {
24- render ( < Banner title = "test" variant = "critical" /> )
25- expect ( screen . getByRole ( 'region' ) ) . toEqual ( screen . getByLabelText ( 'Critical' ) )
25+ it ( 'should use aria-labelledby to reference the title for the critical variant' , ( ) => {
26+ render ( < Banner title = "Critical Issue" variant = "critical" /> )
27+ const region = screen . getByRole ( 'region' , { name : 'Critical Issue' } )
28+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' )
2629 } )
2730
28- it ( 'should label the landmark element with the label for the info variant' , ( ) => {
29- render ( < Banner title = "test" variant = "info" /> )
30- expect ( screen . getByRole ( 'region' ) ) . toEqual ( screen . getByLabelText ( 'Information' ) )
31+ it ( 'should use aria-labelledby to reference the title for the info variant' , ( ) => {
32+ render ( < Banner title = "Information" variant = "info" /> )
33+ const region = screen . getByRole ( 'region' , { name : 'Information' } )
34+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' )
3135 } )
3236
33- it ( 'should label the landmark element with the label for the success variant' , ( ) => {
34- render ( < Banner title = "test" variant = "success" /> )
35- expect ( screen . getByRole ( 'region' ) ) . toEqual ( screen . getByLabelText ( 'Success' ) )
37+ it ( 'should use aria-labelledby to reference the title for the success variant' , ( ) => {
38+ render ( < Banner title = "Success Message" variant = "success" /> )
39+ const region = screen . getByRole ( 'region' , { name : 'Success Message' } )
40+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' )
3641 } )
3742
38- it ( 'should label the landmark element with the label for the upsell variant' , ( ) => {
39- render ( < Banner title = "test" variant = "upsell" /> )
40- expect ( screen . getByRole ( 'region' ) ) . toEqual ( screen . getByLabelText ( 'Recommendation' ) )
43+ it ( 'should use aria-labelledby to reference the title for the upsell variant' , ( ) => {
44+ render ( < Banner title = "Recommendation" variant = "upsell" /> )
45+ const region = screen . getByRole ( 'region' , { name : 'Recommendation' } )
46+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' )
4147 } )
4248
43- it ( 'should label the landmark element with the label for the warning variant' , ( ) => {
44- render ( < Banner title = "test" variant = "warning" /> )
45- expect ( screen . getByRole ( 'region' ) ) . toEqual ( screen . getByLabelText ( 'Warning' ) )
49+ it ( 'should use aria-labelledby to reference the title for the warning variant' , ( ) => {
50+ render ( < Banner title = "Warning" variant = "warning" /> )
51+ const region = screen . getByRole ( 'region' , { name : 'Warning' } )
52+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' )
4653 } )
4754
4855 it ( 'should support the `aria-label` prop to override the default label for the landmark' , ( ) => {
@@ -69,6 +76,30 @@ describe('Banner', () => {
6976 expect ( region ) . not . toHaveAttribute ( 'aria-labelledby' )
7077 } )
7178
79+ it ( 'should use aria-labelledby to reference Banner.Title when provided as a child' , ( ) => {
80+ render (
81+ < Banner >
82+ < Banner . Title > Custom Title Component</ Banner . Title >
83+ </ Banner > ,
84+ )
85+ const region = screen . getByRole ( 'region' , { name : 'Custom Title Component' } )
86+ const heading = screen . getByRole ( 'heading' , { name : 'Custom Title Component' } )
87+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' , heading . id )
88+ expect ( heading ) . toHaveAttribute ( 'id' )
89+ expect ( region ) . not . toHaveAttribute ( 'aria-label' )
90+ } )
91+
92+ it ( 'should use aria-labelledby to reference Banner.Title with custom id' , ( ) => {
93+ render (
94+ < Banner aria-labelledby = "custom-title-id" >
95+ < Banner . Title id = "custom-title-id" > Title with Custom ID</ Banner . Title >
96+ </ Banner > ,
97+ )
98+ const region = screen . getByRole ( 'region' , { name : 'Title with Custom ID' } )
99+ expect ( region ) . toHaveAttribute ( 'aria-labelledby' , 'custom-title-id' )
100+ expect ( screen . getByRole ( 'heading' ) ) . toHaveAttribute ( 'id' , 'custom-title-id' )
101+ } )
102+
72103 it ( 'should default the title to a h2' , ( ) => {
73104 render ( < Banner title = "test" /> )
74105 expect ( screen . getByRole ( 'heading' , { level : 2 } ) ) . toBeInTheDocument ( )
@@ -86,7 +117,7 @@ describe('Banner', () => {
86117 it ( 'should rendering a description with the `description` prop' , ( ) => {
87118 render ( < Banner title = "test" description = "test-description" /> )
88119 expect ( screen . getByText ( 'test-description' ) ) . toBeInTheDocument ( )
89- expect ( screen . getByRole ( 'region' , { name : 'Information ' } ) ) . toContainElement ( screen . getByText ( 'test-description' ) )
120+ expect ( screen . getByRole ( 'region' , { name : 'test ' } ) ) . toContainElement ( screen . getByText ( 'test-description' ) )
90121 } )
91122
92123 it ( 'should support a primary action' , async ( ) => {
0 commit comments