4
4
import * as React from 'react' ;
5
5
import { render } from '@testing-library/react' ;
6
6
7
+ import { KeyCode } from '@cloudscape-design/test-utils-core/utils' ;
8
+
7
9
import '../../__a11y__/to-validate-a11y' ;
8
10
import Autosuggest , { AutosuggestProps } from '../../../lib/components/autosuggest' ;
9
11
import createWrapper from '../../../lib/components/test-utils/dom' ;
@@ -38,9 +40,9 @@ function focusInput() {
38
40
createWrapper ( ) . findAutosuggest ( ) ! . focus ( ) ;
39
41
}
40
42
41
- function expectDropdown ( ) {
43
+ function expectDropdown ( expandToViewport = false ) {
42
44
const wrapper = createWrapper ( ) . findAutosuggest ( ) ! ;
43
- expect ( wrapper . findDropdown ( ) . findOpenDropdown ( ) ) . not . toBe ( null ) ;
45
+ expect ( wrapper . findDropdown ( { expandToViewport } ) . findOpenDropdown ( ) ) . not . toBe ( null ) ;
44
46
expect ( wrapper . findNativeInput ( ) . getElement ( ) ) . toHaveAttribute ( 'aria-expanded' , 'true' ) ;
45
47
}
46
48
@@ -53,11 +55,16 @@ function expectFooterSticky(isSticky: boolean) {
53
55
expect ( Boolean ( dropdown . findByClassName ( styles [ 'list-bottom' ] ) ) ) . toBe ( ! isSticky ) ;
54
56
}
55
57
56
- function expectFooterContent ( expectedText : string ) {
58
+ function expectFooterContent ( expectedText : string , expandToViewport = false ) {
57
59
const wrapper = createWrapper ( ) . findAutosuggest ( ) ! ;
58
- expect ( wrapper . findDropdown ( ) . findFooterRegion ( ) ! ) . not . toBe ( null ) ;
59
- expect ( wrapper . findDropdown ( ) . findFooterRegion ( ) ! . getElement ( ) ) . toHaveTextContent ( expectedText ) ;
60
- expect ( wrapper . findDropdown ( ) . find ( 'ul' ) ! . getElement ( ) ) . toHaveAccessibleDescription ( expectedText ) ;
60
+ expect ( wrapper . findDropdown ( { expandToViewport } ) . findFooterRegion ( ) ! ) . not . toBe ( null ) ;
61
+ expect ( wrapper . findDropdown ( { expandToViewport } ) . findFooterRegion ( ) ! . getElement ( ) ) . toHaveTextContent ( expectedText ) ;
62
+ expect ( wrapper . findDropdown ( { expandToViewport } ) . find ( 'ul' ) ! . getElement ( ) ) . toHaveAccessibleDescription ( expectedText ) ;
63
+ }
64
+
65
+ function expectLiveRegionText ( expectedText : string ) {
66
+ const liveRegion = createWrapper ( ) . findLiveRegion ( ) ! . getElement ( ) ;
67
+ expect ( liveRegion ) . toHaveTextContent ( expectedText ) ;
61
68
}
62
69
63
70
function expectFooterImage ( expectedText : string ) {
@@ -127,6 +134,33 @@ describe('footer types', () => {
127
134
} ) ;
128
135
} ) ;
129
136
137
+ describe . each ( [ true , false ] ) ( 'footer live announcements [expandToViewport=%s]' , ( expandToViewport : boolean ) => {
138
+ test ( 'live announces error text on initial dropdown render' , ( ) => {
139
+ renderAutosuggest ( { statusType : 'error' , expandToViewport } ) ;
140
+ focusInput ( ) ;
141
+ expectDropdown ( expandToViewport ) ;
142
+ expectFooterContent ( 'error!' , expandToViewport ) ;
143
+ expectLiveRegionText ( 'error!' ) ;
144
+ } ) ;
145
+
146
+ test ( 'live announces error text on dropdown toggle' , ( ) => {
147
+ const { wrapper } = renderAutosuggest ( { statusType : 'error' , expandToViewport } ) ;
148
+ focusInput ( ) ;
149
+ expectDropdown ( expandToViewport ) ;
150
+ expectFooterContent ( 'error!' , expandToViewport ) ;
151
+ expectLiveRegionText ( 'error!' ) ;
152
+
153
+ wrapper . findNativeInput ( ) . keydown ( KeyCode . enter ) ;
154
+ expect ( wrapper . findDropdown ( ) ! . findOpenDropdown ( ) ) . toBe ( null ) ;
155
+ expect ( createWrapper ( ) . findLiveRegion ( ) ) . toBeNull ( ) ;
156
+
157
+ wrapper . findNativeInput ( ) . keydown ( KeyCode . down ) ;
158
+ expectDropdown ( expandToViewport ) ;
159
+ expectFooterContent ( 'error!' , expandToViewport ) ;
160
+ expectLiveRegionText ( 'error!' ) ;
161
+ } ) ;
162
+ } ) ;
163
+
130
164
describe ( 'filtering results' , ( ) => {
131
165
describe ( 'with empty state' , ( ) => {
132
166
test ( 'displays empty state footer when value is empty' , ( ) => {
0 commit comments