1
1
import { promises as fs } from 'fs'
2
2
3
- import type { Locator , Page } from '@playwright/test'
4
- import { errors } from '@playwright/test'
5
- import { queries } from '@testing-library/dom'
6
-
7
3
import { configureTestingLibraryScript } from '../../common'
8
- import { replacer , reviver } from '../helpers'
9
- import type {
10
- AllQuery ,
11
- Config ,
12
- FindQuery ,
13
- GetQuery ,
14
- LocatorQueries as Queries ,
15
- Query ,
16
- QueryQuery ,
17
- Selector ,
18
- SynchronousQuery ,
19
- } from '../types'
20
-
21
- const allQueryNames = Object . keys ( queries ) as Query [ ]
22
-
23
- const isAllQuery = ( query : Query ) : query is AllQuery => query . includes ( 'All' )
24
-
25
- const isFindQuery = ( query : Query ) : query is FindQuery => query . startsWith ( 'find' )
26
- const isNotFindQuery = ( query : Query ) : query is Exclude < Query , FindQuery > =>
27
- ! query . startsWith ( 'find' )
28
-
29
- const findQueryToGetQuery = ( query : FindQuery ) => query . replace ( / ^ f i n d / , 'get' ) as GetQuery
30
- const findQueryToQueryQuery = ( query : FindQuery ) => query . replace ( / ^ f i n d / , 'query' ) as QueryQuery
4
+ import { reviver } from '../helpers'
5
+ import type { Config , Selector , SynchronousQuery } from '../types'
31
6
32
7
const queryToSelector = ( query : SynchronousQuery ) =>
33
8
query . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1-$2' ) . toLowerCase ( ) as Selector
@@ -47,75 +22,4 @@ const buildTestingLibraryScript = async ({config}: {config: Config}) => {
47
22
`
48
23
}
49
24
50
- const synchronousQueryNames = allQueryNames . filter ( isNotFindQuery )
51
-
52
- const createFindQuery =
53
- (
54
- pageOrLocator : Page | Locator ,
55
- query : FindQuery ,
56
- { asyncUtilTimeout, asyncUtilExpectedState} : Partial < Config > = { } ,
57
- ) =>
58
- async ( ...[ id , options , waitForElementOptions ] : Parameters < Queries [ FindQuery ] > ) => {
59
- const synchronousOptions = ( [ id , options ] as const ) . filter ( Boolean )
60
-
61
- const locator = pageOrLocator . locator (
62
- `${ queryToSelector ( findQueryToQueryQuery ( query ) ) } =${ JSON . stringify (
63
- synchronousOptions ,
64
- replacer ,
65
- ) } `,
66
- )
67
-
68
- const { state = asyncUtilExpectedState , timeout = asyncUtilTimeout } = waitForElementOptions ?? { }
69
-
70
- try {
71
- await locator . first ( ) . waitFor ( { state, timeout} )
72
- } catch ( error ) {
73
- // In the case of a `waitFor` timeout from Playwright, we want to
74
- // surface the appropriate error from Testing Library, so run the
75
- // query one more time as `get*` knowing that it will fail with the
76
- // error that we want the user to see instead of the `TimeoutError`
77
- if ( error instanceof errors . TimeoutError ) {
78
- return pageOrLocator
79
- . locator (
80
- `${ queryToSelector ( findQueryToGetQuery ( query ) ) } =${ JSON . stringify (
81
- synchronousOptions ,
82
- replacer ,
83
- ) } `,
84
- )
85
- . first ( )
86
- . waitFor ( { state, timeout : 100 } )
87
- }
88
-
89
- throw error
90
- }
91
-
92
- return locator
93
- }
94
-
95
- /**
96
- * Given a `Page` or `Locator` instance, return an object of Testing Library
97
- * query methods that return a `Locator` instance for the queried element
98
- *
99
- * @internal this API is not currently intended for public usage and may be
100
- * removed or changed outside of semantic release versioning. If possible, you
101
- * should use the `locatorFixtures` with **@playwright/test** instead.
102
- * @see {@link locatorFixtures }
103
- *
104
- * @param pageOrLocator `Page` or `Locator` instance to use as the query root
105
- * @param config Testing Library configuration to apply to queries
106
- *
107
- * @returns object containing scoped Testing Library query methods
108
- */
109
- const queriesFor = ( pageOrLocator : Page | Locator , config ?: Partial < Config > ) =>
110
- allQueryNames . reduce (
111
- ( rest , query ) => ( {
112
- ...rest ,
113
- [ query ] : isFindQuery ( query )
114
- ? createFindQuery ( pageOrLocator , query , config )
115
- : ( ...args : Parameters < Queries [ SynchronousQuery ] > ) =>
116
- pageOrLocator . locator ( `${ queryToSelector ( query ) } =${ JSON . stringify ( args , replacer ) } ` ) ,
117
- } ) ,
118
- { } as Queries ,
119
- )
120
-
121
- export { buildTestingLibraryScript , isAllQuery , queriesFor , queryToSelector , synchronousQueryNames }
25
+ export { buildTestingLibraryScript , queryToSelector }
0 commit comments