11import { DEFAULT_OPTIONS } from '../../constants.js'
2- import type { WdioElementMaybePromise } from '../../types.js'
2+ import type { WdioElementOrArrayMaybePromise } from '../../types.js'
3+ import { map , wrapExpectedWithArray } from '../../util/elementsUtil.js'
4+ import { executeCommandWithArray } from '../../util/executeCommand.js'
35import {
46 compareNumbers ,
57 enhanceError ,
6- executeCommand ,
78 numberError ,
89 waitUntil ,
910} from '../../utils.js'
1011
11- async function condition ( el : WebdriverIO . Element , width : number , options : ExpectWebdriverIO . NumberOptions ) {
12+ async function condition ( el : WebdriverIO . Element , options : ExpectWebdriverIO . NumberOptions ) {
1213 const actualWidth = await el . getSize ( 'width' )
1314
1415 return {
@@ -18,49 +19,51 @@ async function condition(el: WebdriverIO.Element, width: number, options: Expect
1819}
1920
2021export async function toHaveWidth (
21- received : WdioElementMaybePromise ,
22+ received : WdioElementOrArrayMaybePromise ,
2223 expectedValue : number | ExpectWebdriverIO . NumberOptions ,
2324 options : ExpectWebdriverIO . CommandOptions = DEFAULT_OPTIONS
2425) {
2526 const isNot = this . isNot
2627 const { expectation = 'width' , verb = 'have' } = this
28+ this . matcherName = 'toHaveWidth'
2729
2830 await options . beforeAssertion ?.( {
29- matcherName : 'toHaveWidth' ,
31+ matcherName : this . matcherName ,
3032 expectedValue,
3133 options,
3234 } )
3335
34- // type check
36+ // validate expectedValue
3537 let numberOptions : ExpectWebdriverIO . NumberOptions
3638 if ( typeof expectedValue === 'number' ) {
3739 numberOptions = { eq : expectedValue } as ExpectWebdriverIO . NumberOptions
3840 } else if ( ! expectedValue || ( typeof expectedValue . eq !== 'number' && typeof expectedValue . gte !== 'number' && typeof expectedValue . lte !== 'number' ) ) {
39- throw new Error ( ' Invalid params passed to toHaveHeight.' )
41+ throw new Error ( ` Invalid params passed to ${ this . matcherName } .` )
4042 } else {
4143 numberOptions = expectedValue
4244 }
4345
44- let el = await received ?. getElement ( )
46+ let elements
4547 let actualWidth
4648
4749 const pass = await waitUntil (
4850 async ( ) => {
49- const result = await executeCommand . call ( this , el , condition , numberOptions , [ expectedValue , numberOptions ] )
51+ const result = await executeCommandWithArray . call ( this , received , ( elements ) => map ( elements , ( el ) => condition ( el , numberOptions ) ) )
5052
51- el = result . el as WebdriverIO . Element
52- actualWidth = result . values
53+ elements = result . elementOrElements
54+ actualWidth = result . valueOrValues
5355
5456 return result . success
5557 } ,
5658 isNot ,
57- { ... numberOptions , ... options }
59+ { wait : options . wait , interval : options . interval }
5860 )
5961
60- const error = numberError ( numberOptions )
62+ const expextedFailureMessage = numberError ( numberOptions )
63+ const expectedValues = wrapExpectedWithArray ( elements , actualWidth , expextedFailureMessage )
6164 const message = enhanceError (
62- el ,
63- error ,
65+ elements ,
66+ expectedValues ,
6467 actualWidth ,
6568 this ,
6669 verb ,
@@ -75,7 +78,7 @@ export async function toHaveWidth(
7578 }
7679
7780 await options . afterAssertion ?.( {
78- matcherName : 'toHaveWidth' ,
81+ matcherName : this . matcherName ,
7982 expectedValue,
8083 options,
8184 result
0 commit comments