@@ -6,10 +6,10 @@ import type {
66 AsFunction ,
77 ParametersOf ,
88 ReturnTypeOf ,
9- WithMatchers ,
109} from './types.ts'
1110
1211export interface WhenOptions {
12+ ignoreExtraArgs ?: boolean
1313 times ?: number
1414}
1515
@@ -22,10 +22,7 @@ export interface BehaviorStack<TFunc extends AnyMockable> {
2222
2323 getUnmatchedCalls : ( ) => readonly ParametersOf < TFunc > [ ]
2424
25- bindArgs : (
26- args : WithMatchers < ParametersOf < TFunc > > ,
27- options : WhenOptions ,
28- ) => BoundBehaviorStack < TFunc >
25+ bindArgs : ( args : unknown [ ] , options : WhenOptions ) => BoundBehaviorStack < TFunc >
2926}
3027
3128export interface BoundBehaviorStack < TFunc extends AnyMockable > {
@@ -37,9 +34,10 @@ export interface BoundBehaviorStack<TFunc extends AnyMockable> {
3734}
3835
3936export interface BehaviorEntry < TArgs extends unknown [ ] > {
40- args : WithMatchers < TArgs >
37+ args : unknown [ ]
4138 behavior : Behavior
4239 calls : TArgs [ ]
40+ ignoreExtraArgs : boolean
4341 maxCallCount ?: number | undefined
4442}
4543
@@ -60,6 +58,7 @@ export type Behavior =
6058
6159export interface BehaviorOptions < TValue > {
6260 value : TValue
61+ ignoreExtraArgs : boolean
6362 maxCallCount : number | undefined
6463}
6564
@@ -76,7 +75,7 @@ export const createBehaviorStack = <
7675
7776 use : ( args ) => {
7877 const behavior = behaviors
79- . filter ( ( b ) => behaviorAvailable ( b ) )
78+ . filter ( behaviorAvailable )
8079 . find ( behaviorMatches ( args ) )
8180
8281 if ( ! behavior ) {
@@ -92,8 +91,9 @@ export const createBehaviorStack = <
9291 addReturn : ( values ) => {
9392 behaviors . unshift (
9493 ...getBehaviorOptions ( values , options ) . map (
95- ( { value, maxCallCount } ) => ( {
94+ ( { value, ignoreExtraArgs , maxCallCount } ) => ( {
9695 args,
96+ ignoreExtraArgs,
9797 maxCallCount,
9898 behavior : { type : BehaviorType . RETURN , value } ,
9999 calls : [ ] ,
@@ -104,8 +104,9 @@ export const createBehaviorStack = <
104104 addResolve : ( values ) => {
105105 behaviors . unshift (
106106 ...getBehaviorOptions ( values , options ) . map (
107- ( { value, maxCallCount } ) => ( {
107+ ( { value, ignoreExtraArgs , maxCallCount } ) => ( {
108108 args,
109+ ignoreExtraArgs,
109110 maxCallCount,
110111 behavior : { type : BehaviorType . RESOLVE , value } ,
111112 calls : [ ] ,
@@ -116,8 +117,9 @@ export const createBehaviorStack = <
116117 addThrow : ( values ) => {
117118 behaviors . unshift (
118119 ...getBehaviorOptions ( values , options ) . map (
119- ( { value, maxCallCount } ) => ( {
120+ ( { value, ignoreExtraArgs , maxCallCount } ) => ( {
120121 args,
122+ ignoreExtraArgs,
121123 maxCallCount,
122124 behavior : { type : BehaviorType . THROW , error : value } ,
123125 calls : [ ] ,
@@ -128,8 +130,9 @@ export const createBehaviorStack = <
128130 addReject : ( values ) => {
129131 behaviors . unshift (
130132 ...getBehaviorOptions ( values , options ) . map (
131- ( { value, maxCallCount } ) => ( {
133+ ( { value, ignoreExtraArgs , maxCallCount } ) => ( {
132134 args,
135+ ignoreExtraArgs,
133136 maxCallCount,
134137 behavior : { type : BehaviorType . REJECT , error : value } ,
135138 calls : [ ] ,
@@ -140,8 +143,9 @@ export const createBehaviorStack = <
140143 addDo : ( values ) => {
141144 behaviors . unshift (
142145 ...getBehaviorOptions ( values , options ) . map (
143- ( { value, maxCallCount } ) => ( {
146+ ( { value, ignoreExtraArgs , maxCallCount } ) => ( {
144147 args,
148+ ignoreExtraArgs,
145149 maxCallCount,
146150 behavior : {
147151 type : BehaviorType . DO ,
@@ -158,14 +162,15 @@ export const createBehaviorStack = <
158162
159163const getBehaviorOptions = < TValue > (
160164 values : TValue [ ] ,
161- { times } : WhenOptions ,
165+ { ignoreExtraArgs , times } : WhenOptions ,
162166) : BehaviorOptions < TValue > [ ] => {
163167 if ( values . length === 0 ) {
164168 values = [ undefined as TValue ]
165169 }
166170
167171 return values . map ( ( value , index ) => ( {
168172 value,
173+ ignoreExtraArgs : ignoreExtraArgs ?? false ,
169174 maxCallCount : times ?? ( index < values . length - 1 ? 1 : undefined ) ,
170175 } ) )
171176}
@@ -179,18 +184,19 @@ const behaviorAvailable = <TArgs extends unknown[]>(
179184 )
180185}
181186
182- const behaviorMatches = < TArgs extends unknown [ ] > ( args : TArgs ) => {
183- return ( behavior : BehaviorEntry < TArgs > ) : boolean => {
184- let index = 0
187+ const behaviorMatches = < TArgs extends unknown [ ] > ( actualArgs : TArgs ) => {
188+ return ( behaviorEntry : BehaviorEntry < TArgs > ) : boolean => {
189+ const { args : expectedArgs , ignoreExtraArgs } = behaviorEntry
190+ const isArgsLengthMatch = ignoreExtraArgs
191+ ? expectedArgs . length <= actualArgs . length
192+ : expectedArgs . length === actualArgs . length
185193
186- while ( index < args . length || index < behavior . args . length ) {
187- if ( ! equals ( args [ index ] , behavior . args [ index ] ) ) {
188- return false
189- }
190-
191- index += 1
194+ if ( ! isArgsLengthMatch ) {
195+ return false
192196 }
193197
194- return true
198+ return expectedArgs . every ( ( expected , index ) =>
199+ equals ( actualArgs [ index ] , expected ) ,
200+ )
195201 }
196202}
0 commit comments