1
- import {
2
- FireFunction ,
3
- FireObject ,
4
- Matcher ,
5
- getByText ,
6
- SelectorMatcherOptions ,
7
- queryByText ,
8
- } from '@testing-library/dom' ;
1
+ import { FireFunction , FireObject , Matcher , screen , ByRoleOptions } from '@testing-library/dom' ;
9
2
10
3
// implementation from https://github.com/testing-library/user-event
11
4
export function createSelectOptions ( fireEvent : FireFunction & FireObject ) {
@@ -18,10 +11,16 @@ export function createSelectOptions(fireEvent: FireFunction & FireObject) {
18
11
fireEvent . click ( element ) ;
19
12
}
20
13
21
- function selectOption ( select : HTMLSelectElement , index : number , matcher : Matcher , options ?: SelectorMatcherOptions ) {
22
- // fallback to document.body, because libraries as Angular Material will have their custom select component
23
- const option = ( queryByText ( select , matcher , options ) ||
24
- getByText ( document . body , matcher , options ) ) as HTMLOptionElement ;
14
+ function selectOption ( select : HTMLSelectElement , index : number , options : Matcher | ByRoleOptions ) {
15
+ const query =
16
+ typeof options === 'string'
17
+ ? ( ( { name : new RegExp ( options , 'i' ) } as unknown ) as ByRoleOptions )
18
+ : options instanceof RegExp
19
+ ? ( ( { name : options } as unknown ) as ByRoleOptions )
20
+ : typeof options === 'function'
21
+ ? ( ( { name : options } as unknown ) as ByRoleOptions )
22
+ : options ;
23
+ const option = screen . getByRole ( 'option' , query ) as HTMLOptionElement ;
25
24
26
25
fireEvent . mouseOver ( option ) ;
27
26
fireEvent . mouseMove ( option ) ;
@@ -36,8 +35,7 @@ export function createSelectOptions(fireEvent: FireFunction & FireObject) {
36
35
37
36
return async function selectOptions (
38
37
element : HTMLElement ,
39
- matcher : Matcher | Matcher [ ] ,
40
- matcherOptions ?: SelectorMatcherOptions ,
38
+ options : Matcher | ByRoleOptions | ( ( Matcher | ByRoleOptions ) [ ] ) ,
41
39
) {
42
40
const selectElement = element as HTMLSelectElement ;
43
41
@@ -55,10 +53,10 @@ export function createSelectOptions(fireEvent: FireFunction & FireObject) {
55
53
56
54
clickElement ( selectElement ) ;
57
55
58
- const values = Array . isArray ( matcher ) ? matcher : [ matcher ] ;
56
+ const values = Array . isArray ( options ) ? options : [ options ] ;
59
57
values
60
58
. filter ( ( _ , index ) => index === 0 || selectElement . multiple )
61
- . forEach ( ( val , index ) => selectOption ( selectElement , index , val , matcherOptions ) ) ;
59
+ . forEach ( ( val , index ) => selectOption ( selectElement , index , val ) ) ;
62
60
63
61
if ( wasAnotherElementFocused ) {
64
62
fireEvent . blur ( focusedElement ) ;
0 commit comments