@@ -30,127 +30,150 @@ function throwsException(fake, error, message) {
30
30
}
31
31
}
32
32
33
+ const SKIP_OPTIONS_FOR_YIELDS = {
34
+ skipReturn : true ,
35
+ skipThrows : true ,
36
+ } ;
37
+
38
+ function clear ( fake , options ) {
39
+ fake . fakeFn = undefined ;
40
+
41
+ fake . callsThrough = undefined ;
42
+ fake . callsThroughWithNew = undefined ;
43
+
44
+ if ( ! options || ! options . skipThrows ) {
45
+ fake . exception = undefined ;
46
+ fake . exceptionCreator = undefined ;
47
+ fake . throwArgAt = undefined ;
48
+ }
49
+
50
+ fake . callArgAt = undefined ;
51
+ fake . callbackArguments = undefined ;
52
+ fake . callbackContext = undefined ;
53
+ fake . callArgProp = undefined ;
54
+ fake . callbackAsync = undefined ;
55
+
56
+ if ( ! options || ! options . skipReturn ) {
57
+ fake . returnValue = undefined ;
58
+ fake . returnValueDefined = undefined ;
59
+ fake . returnArgAt = undefined ;
60
+ fake . returnThis = undefined ;
61
+ }
62
+
63
+ fake . resolve = undefined ;
64
+ fake . resolveThis = undefined ;
65
+ fake . resolveArgAt = undefined ;
66
+
67
+ fake . reject = undefined ;
68
+ }
69
+
33
70
const defaultBehaviors = {
34
71
callsFake : function callsFake ( fake , fn ) {
72
+ clear ( fake ) ;
73
+
35
74
fake . fakeFn = fn ;
36
- fake . exception = undefined ;
37
- fake . exceptionCreator = undefined ;
38
75
} ,
39
76
40
77
callsArg : function callsArg ( fake , index ) {
41
78
if ( typeof index !== "number" ) {
42
79
throw new TypeError ( "argument index is not number" ) ;
43
80
}
81
+ clear ( fake ) ;
44
82
45
83
fake . callArgAt = index ;
46
84
fake . callbackArguments = [ ] ;
47
- fake . callbackContext = undefined ;
48
- fake . callArgProp = undefined ;
49
- fake . callbackAsync = false ;
50
85
} ,
51
86
52
87
callsArgOn : function callsArgOn ( fake , index , context ) {
53
88
if ( typeof index !== "number" ) {
54
89
throw new TypeError ( "argument index is not number" ) ;
55
90
}
91
+ clear ( fake ) ;
56
92
57
93
fake . callArgAt = index ;
58
94
fake . callbackArguments = [ ] ;
59
95
fake . callbackContext = context ;
60
- fake . callArgProp = undefined ;
61
- fake . callbackAsync = false ;
62
96
} ,
63
97
64
98
callsArgWith : function callsArgWith ( fake , index ) {
65
99
if ( typeof index !== "number" ) {
66
100
throw new TypeError ( "argument index is not number" ) ;
67
101
}
102
+ clear ( fake ) ;
68
103
69
104
fake . callArgAt = index ;
70
105
fake . callbackArguments = slice ( arguments , 2 ) ;
71
- fake . callbackContext = undefined ;
72
- fake . callArgProp = undefined ;
73
- fake . callbackAsync = false ;
74
106
} ,
75
107
76
108
callsArgOnWith : function callsArgWith ( fake , index , context ) {
77
109
if ( typeof index !== "number" ) {
78
110
throw new TypeError ( "argument index is not number" ) ;
79
111
}
112
+ clear ( fake ) ;
80
113
81
114
fake . callArgAt = index ;
82
115
fake . callbackArguments = slice ( arguments , 3 ) ;
83
116
fake . callbackContext = context ;
84
- fake . callArgProp = undefined ;
85
- fake . callbackAsync = false ;
86
117
} ,
87
118
88
119
usingPromise : function usingPromise ( fake , promiseLibrary ) {
89
120
fake . promiseLibrary = promiseLibrary ;
90
121
} ,
91
122
92
123
yields : function ( fake ) {
124
+ clear ( fake , SKIP_OPTIONS_FOR_YIELDS ) ;
125
+
93
126
fake . callArgAt = useLeftMostCallback ;
94
127
fake . callbackArguments = slice ( arguments , 1 ) ;
95
- fake . callbackContext = undefined ;
96
- fake . callArgProp = undefined ;
97
- fake . callbackAsync = false ;
98
- fake . fakeFn = undefined ;
99
128
} ,
100
129
101
130
yieldsRight : function ( fake ) {
131
+ clear ( fake , SKIP_OPTIONS_FOR_YIELDS ) ;
132
+
102
133
fake . callArgAt = useRightMostCallback ;
103
134
fake . callbackArguments = slice ( arguments , 1 ) ;
104
- fake . callbackContext = undefined ;
105
- fake . callArgProp = undefined ;
106
- fake . callbackAsync = false ;
107
- fake . fakeFn = undefined ;
108
135
} ,
109
136
110
137
yieldsOn : function ( fake , context ) {
138
+ clear ( fake , SKIP_OPTIONS_FOR_YIELDS ) ;
139
+
111
140
fake . callArgAt = useLeftMostCallback ;
112
141
fake . callbackArguments = slice ( arguments , 2 ) ;
113
142
fake . callbackContext = context ;
114
- fake . callArgProp = undefined ;
115
- fake . callbackAsync = false ;
116
- fake . fakeFn = undefined ;
117
143
} ,
118
144
119
145
yieldsTo : function ( fake , prop ) {
146
+ clear ( fake , SKIP_OPTIONS_FOR_YIELDS ) ;
147
+
120
148
fake . callArgAt = useLeftMostCallback ;
121
149
fake . callbackArguments = slice ( arguments , 2 ) ;
122
- fake . callbackContext = undefined ;
123
150
fake . callArgProp = prop ;
124
- fake . callbackAsync = false ;
125
- fake . fakeFn = undefined ;
126
151
} ,
127
152
128
153
yieldsToOn : function ( fake , prop , context ) {
154
+ clear ( fake , SKIP_OPTIONS_FOR_YIELDS ) ;
155
+
129
156
fake . callArgAt = useLeftMostCallback ;
130
157
fake . callbackArguments = slice ( arguments , 3 ) ;
131
158
fake . callbackContext = context ;
132
159
fake . callArgProp = prop ;
133
- fake . callbackAsync = false ;
134
- fake . fakeFn = undefined ;
135
160
} ,
136
161
137
162
throws : throwsException ,
138
163
throwsException : throwsException ,
139
164
140
165
returns : function returns ( fake , value ) {
166
+ clear ( fake ) ;
167
+
141
168
fake . returnValue = value ;
142
- fake . resolve = false ;
143
- fake . reject = false ;
144
169
fake . returnValueDefined = true ;
145
- fake . exception = undefined ;
146
- fake . exceptionCreator = undefined ;
147
- fake . fakeFn = undefined ;
148
170
} ,
149
171
150
172
returnsArg : function returnsArg ( fake , index ) {
151
173
if ( typeof index !== "number" ) {
152
174
throw new TypeError ( "argument index is not number" ) ;
153
175
}
176
+ clear ( fake ) ;
154
177
155
178
fake . returnArgAt = index ;
156
179
} ,
@@ -159,38 +182,33 @@ const defaultBehaviors = {
159
182
if ( typeof index !== "number" ) {
160
183
throw new TypeError ( "argument index is not number" ) ;
161
184
}
185
+ clear ( fake ) ;
162
186
163
187
fake . throwArgAt = index ;
164
188
} ,
165
189
166
190
returnsThis : function returnsThis ( fake ) {
191
+ clear ( fake ) ;
192
+
167
193
fake . returnThis = true ;
168
194
} ,
169
195
170
196
resolves : function resolves ( fake , value ) {
197
+ clear ( fake ) ;
198
+
171
199
fake . returnValue = value ;
172
200
fake . resolve = true ;
173
- fake . resolveThis = false ;
174
- fake . reject = false ;
175
201
fake . returnValueDefined = true ;
176
- fake . exception = undefined ;
177
- fake . exceptionCreator = undefined ;
178
- fake . fakeFn = undefined ;
179
202
} ,
180
203
181
204
resolvesArg : function resolvesArg ( fake , index ) {
182
205
if ( typeof index !== "number" ) {
183
206
throw new TypeError ( "argument index is not number" ) ;
184
207
}
208
+ clear ( fake ) ;
209
+
185
210
fake . resolveArgAt = index ;
186
- fake . returnValue = undefined ;
187
211
fake . resolve = true ;
188
- fake . resolveThis = false ;
189
- fake . reject = false ;
190
- fake . returnValueDefined = false ;
191
- fake . exception = undefined ;
192
- fake . exceptionCreator = undefined ;
193
- fake . fakeFn = undefined ;
194
212
} ,
195
213
196
214
rejects : function rejects ( fake , error , message ) {
@@ -203,34 +221,30 @@ const defaultBehaviors = {
203
221
} else {
204
222
reason = error ;
205
223
}
224
+ clear ( fake ) ;
225
+
206
226
fake . returnValue = reason ;
207
- fake . resolve = false ;
208
- fake . resolveThis = false ;
209
227
fake . reject = true ;
210
228
fake . returnValueDefined = true ;
211
- fake . exception = undefined ;
212
- fake . exceptionCreator = undefined ;
213
- fake . fakeFn = undefined ;
214
229
215
230
return fake ;
216
231
} ,
217
232
218
233
resolvesThis : function resolvesThis ( fake ) {
219
- fake . returnValue = undefined ;
220
- fake . resolve = false ;
234
+ clear ( fake ) ;
235
+
221
236
fake . resolveThis = true ;
222
- fake . reject = false ;
223
- fake . returnValueDefined = false ;
224
- fake . exception = undefined ;
225
- fake . exceptionCreator = undefined ;
226
- fake . fakeFn = undefined ;
227
237
} ,
228
238
229
239
callThrough : function callThrough ( fake ) {
240
+ clear ( fake ) ;
241
+
230
242
fake . callsThrough = true ;
231
243
} ,
232
244
233
245
callThroughWithNew : function callThroughWithNew ( fake ) {
246
+ clear ( fake ) ;
247
+
234
248
fake . callsThroughWithNew = true ;
235
249
} ,
236
250
0 commit comments