11import type { ParsedCSSValue } from 'webdriverio'
2- import isEqual from 'lodash.isequal' ;
2+ import isEqual from 'lodash.isequal'
33
44import { executeCommand } from './util/executeCommand.js'
55import { wrapExpectedWithArray , updateElementsArray } from './util/elementsUtil.js'
66import { enhanceError , enhanceErrorBe , numberError } from './util/formatMessage.js'
77import { DEFAULT_OPTIONS } from './constants.js'
88import type { WdioElementMaybePromise } from './types.js'
9- import { Replacer } from './types/expect-webdriverio.js'
109
1110const sleep = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
1211
@@ -19,10 +18,7 @@ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
1918const waitUntil = async (
2019 condition : ( ) => Promise < boolean > ,
2120 isNot = false ,
22- {
23- wait = DEFAULT_OPTIONS . wait ,
24- interval = DEFAULT_OPTIONS . interval
25- } = { } ,
21+ { wait = DEFAULT_OPTIONS . wait , interval = DEFAULT_OPTIONS . interval } = { }
2622) : Promise < boolean > => {
2723 // single attempt
2824 if ( wait === 0 ) {
@@ -35,13 +31,13 @@ const waitUntil = async (
3531 const start = Date . now ( )
3632 // eslint-disable-next-line no-constant-condition
3733 while ( true ) {
38- if ( ( Date . now ( ) - start ) > wait ) {
34+ if ( Date . now ( ) - start > wait ) {
3935 throw new Error ( 'timeout' )
4036 }
4137
4238 error = undefined
4339 try {
44- const result = isNot !== await condition ( )
40+ const result = isNot !== ( await condition ( ) )
4541 if ( result ) {
4642 break
4743 }
@@ -67,28 +63,35 @@ async function executeCommandBe(
6763 command : ( el : WebdriverIO . Element ) => Promise < boolean > ,
6864 options : ExpectWebdriverIO . CommandOptions
6965) : Promise < {
70- pass : boolean ,
66+ pass : boolean
7167 message : ( ) => string
7268} > {
7369 const { isNot, expectation, verb = 'be' } = this
7470
7571 received = await received
7672 let el = received
77- const pass = await waitUntil ( async ( ) => {
78- const result = await executeCommand . call ( this ,
79- el ,
80- async element => ( { result : await command ( element ) } ) , options )
81- el = result . el
82- return result . success
83- } , isNot , options )
73+ const pass = await waitUntil (
74+ async ( ) => {
75+ const result = await executeCommand . call (
76+ this ,
77+ el ,
78+ async ( element ) => ( { result : await command ( element ) } ) ,
79+ options
80+ )
81+ el = result . el
82+ return result . success
83+ } ,
84+ isNot ,
85+ options
86+ )
8487
8588 updateElementsArray ( pass , received , el )
8689
8790 const message = enhanceErrorBe ( el , pass , this , verb , expectation , options )
8891
8992 return {
9093 pass,
91- message : ( ) => message
94+ message : ( ) => message ,
9295 }
9396}
9497
@@ -116,11 +119,23 @@ const compareNumbers = (actual: number, options: ExpectWebdriverIO.NumberOptions
116119 return false
117120}
118121
119- export const compareText = ( actual : string , expected : string | RegExp , { ignoreCase = false , trim = true , containing = false , atStart = false , atEnd = false , atIndex, replace } : ExpectWebdriverIO . StringOptions ) => {
122+ export const compareText = (
123+ actual : string ,
124+ expected : string | RegExp ,
125+ {
126+ ignoreCase = false ,
127+ trim = true ,
128+ containing = false ,
129+ atStart = false ,
130+ atEnd = false ,
131+ atIndex,
132+ replace,
133+ } : ExpectWebdriverIO . StringOptions
134+ ) => {
120135 if ( typeof actual !== 'string' ) {
121136 return {
122137 value : actual ,
123- result : false
138+ result : false ,
124139 }
125140 }
126141
@@ -132,56 +147,68 @@ export const compareText = (actual: string, expected: string | RegExp, { ignoreC
132147 }
133148 if ( ignoreCase ) {
134149 actual = actual . toLowerCase ( )
135- if ( ! ( expected instanceof RegExp ) ) {
150+ if ( ! ( expected instanceof RegExp ) ) {
136151 expected = expected . toLowerCase ( )
137152 }
138153 }
139154
140155 if ( expected instanceof RegExp ) {
141156 return {
142157 value : actual ,
143- result : ! ! actual . match ( expected )
158+ result : ! ! actual . match ( expected ) ,
144159 }
145160 }
146161 if ( containing ) {
147162 return {
148163 value : actual ,
149- result : actual . includes ( expected )
164+ result : actual . includes ( expected ) ,
150165 }
151166 }
152167
153168 if ( atStart ) {
154169 return {
155170 value : actual ,
156- result : actual . startsWith ( expected )
171+ result : actual . startsWith ( expected ) ,
157172 }
158173 }
159174
160175 if ( atEnd ) {
161176 return {
162177 value : actual ,
163- result : actual . endsWith ( expected )
178+ result : actual . endsWith ( expected ) ,
164179 }
165180 }
166181
167182 if ( atIndex ) {
168183 return {
169184 value : actual ,
170- result : actual . substring ( atIndex , actual . length ) . startsWith ( expected )
185+ result : actual . substring ( atIndex , actual . length ) . startsWith ( expected ) ,
171186 }
172187 }
173188
174189 return {
175190 value : actual ,
176- result : actual === expected
191+ result : actual === expected ,
177192 }
178193}
179194
180- export const compareTextWithArray = ( actual : string , expectedArray : Array < string | RegExp > , { ignoreCase = false , trim = false , containing = false , atStart = false , atEnd = false , atIndex, replace } : ExpectWebdriverIO . StringOptions ) => {
195+ export const compareTextWithArray = (
196+ actual : string ,
197+ expectedArray : Array < string | RegExp > ,
198+ {
199+ ignoreCase = false ,
200+ trim = false ,
201+ containing = false ,
202+ atStart = false ,
203+ atEnd = false ,
204+ atIndex,
205+ replace,
206+ } : ExpectWebdriverIO . StringOptions
207+ ) => {
181208 if ( typeof actual !== 'string' ) {
182209 return {
183210 value : actual ,
184- result : false
211+ result : false ,
185212 }
186213 }
187214
@@ -193,7 +220,7 @@ export const compareTextWithArray = (actual: string, expectedArray: Array<string
193220 }
194221 if ( ignoreCase ) {
195222 actual = actual . toLowerCase ( )
196- expectedArray = expectedArray . map ( item => ( item instanceof RegExp ) ? item : item . toLowerCase ( ) )
223+ expectedArray = expectedArray . map ( ( item ) => ( item instanceof RegExp ? item : item . toLowerCase ( ) ) )
197224 }
198225
199226 const textInArray = expectedArray . some ( ( expected ) => {
@@ -216,25 +243,29 @@ export const compareTextWithArray = (actual: string, expectedArray: Array<string
216243 } )
217244 return {
218245 value : actual ,
219- result : textInArray
246+ result : textInArray ,
220247 }
221248}
222249
223250export const compareObject = ( actual : object | number , expected : string | number | object ) => {
224251 if ( typeof actual !== 'object' || Array . isArray ( actual ) ) {
225252 return {
226253 value : actual ,
227- result : false
254+ result : false ,
228255 }
229256 }
230257
231258 return {
232259 value : actual ,
233- result : isEqual ( actual , expected )
260+ result : isEqual ( actual , expected ) ,
234261 }
235262}
236263
237- export const compareStyle = async ( actualEl : WebdriverIO . Element , style : { [ key : string ] : string ; } , { ignoreCase = true , trim = false } ) => {
264+ export const compareStyle = async (
265+ actualEl : WebdriverIO . Element ,
266+ style : { [ key : string ] : string } ,
267+ { ignoreCase = true , trim = false }
268+ ) => {
238269 let result = true
239270 const actual : any = { }
240271
@@ -259,15 +290,18 @@ export const compareStyle = async (actualEl: WebdriverIO.Element, style: { [key:
259290
260291 return {
261292 value : actual ,
262- result
293+ result,
263294 }
264295}
265296
266297function aliasFn (
267298 fn : ( ...args : any ) => void ,
268- { verb, expectation } : {
269- verb ?: string ;
270- expectation ?: string ;
299+ {
300+ verb,
301+ expectation,
302+ } : {
303+ verb ?: string
304+ expectation ?: string
271305 } = { } ,
272306 ...args : any [ ]
273307) : any {
@@ -285,17 +319,22 @@ export {
285319 executeCommandBe ,
286320 waitUntil ,
287321 compareNumbers ,
288- aliasFn
322+ aliasFn ,
289323}
290324
291- function replaceActual ( replace : Replacer | Replacer [ ] , actual : string ) {
292- const hasMultipleReplacers = ( replace as Replacer [ ] ) . every ( r => Array . isArray ( r ) ) ;
325+ function replaceActual (
326+ replace : [ string | RegExp , string | Function ] | Array < [ string | RegExp , string | Function ] > ,
327+ actual : string
328+ ) {
329+ const hasMultipleReplacers = ( replace as [ string | RegExp , string | Function ] [ ] ) . every ( ( r ) =>
330+ Array . isArray ( r )
331+ )
293332 const replacers = hasMultipleReplacers
294- ? replace as Replacer [ ]
295- : [ replace as Replacer ]
333+ ? ( replace as [ string | RegExp , string | Function ] [ ] )
334+ : [ replace as [ string | RegExp , string | Function ] ]
296335
297- if ( replacers . some ( r => Array . isArray ( r ) && r . length !== 2 ) ) {
298- throw new Error ( 'Replacers need to have a searchValue and a replaceValue' ) ;
336+ if ( replacers . some ( ( r ) => Array . isArray ( r ) && r . length !== 2 ) ) {
337+ throw new Error ( 'Replacers need to have a searchValue and a replaceValue' )
299338 }
300339
301340 for ( const replacer of replacers ) {
@@ -304,4 +343,4 @@ function replaceActual(replace: Replacer | Replacer[], actual: string) {
304343 }
305344
306345 return actual
307- }
346+ }
0 commit comments