forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-mock.d.ts
194 lines (168 loc) · 6.02 KB
/
simple-mock.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Type definitions for simple-mock
// Project: https://github.com/jupiter/simple-mock
// Definitions by: Leon Yu <https://github.com/leonyu>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare namespace Simple {
type Fn<T> = {
(...args: any[]): T
}
export interface Static {
/**
* Restores all current mocks.
*/
restore(): void;
/**
* Wraps fn in a spy and sets this on the obj, restorable with all mocks.
*/
mock<T>(obj: any, key: string, fn: Fn<T>): Stub<T>;
/**
* Sets the value on this object. E.g. mock(config, 'title', 'test') is the same as config.title = 'test', but restorable with all mocks.
*/
mock<T>(obj: any, key: string, mockValue: T): T;
/**
* If obj has already has this function, it is wrapped in a spy. The resulting spy can be turned into a stub by further configuration. Restores with all mocks.
*/
mock(obj: any, key: string): Stub<any>;
mock<T>(obj: any, key: string): Stub<T>;
/**
* Wraps fn in a spy.
*/
spy<T>(fn: Fn<T>): Spy<T>;
/**
* Wraps fn in a spy.
*/
mock<T>(fn: Fn<T>): Spy<T>;
/**
* Returns a stub function that is also a spy.
*/
stub(): Stub<any>;
stub<T>(): Stub<T>;
/**
* Returns a stub function that is also a spy.
*/
mock(): Stub<any>;
mock<T>(): Stub<T>;
Promise?: PromiseConstructorLike;
}
interface Calls<T> {
/**
* an array of arguments received on the call
*/
args: any[];
/**
* first argument
*/
arg: any;
/**
* the context (this) of the call
*/
context: any;
/**
* the value returned by the wrapped function
*/
returned: T;
/**
* the error thrown by the wrapped function
*/
threw: Error;
/**
* autoincrementing number, can be compared to evaluate call order
*/
k: number;
}
export interface Spy<T>{
(...args: any[]): T;
called: boolean;
/**
* Number of times the function was called.
*/
callCount: number;
calls: Calls<T>[];
firstCall: Calls<T>;
/**
* The last call object. (This is often also the first and only call.)
*/
lastCall: Calls<T>;
/**
* Resets all counts and properties to the original state.
*/
reset(): void;
}
interface Action<T> {
/**
* arguments to call back with
*/
cbArgs: ArrayLike<any>;
returnValue: T;
throwError: Error;
}
export interface Stub<T> extends Spy<T> {
/**
* Configures this stub to call this function, returning its return value.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
callFn<R>(fn: Fn<R>): Stub<R>;
/**
* Configures this stub to call the original, unstubbed function, returning its return value.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
callOriginal(): Stub<T>;
/**
* Configures this stub to return with this value.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
returnWith<R>(val: R): Stub<R>;
/**
* Configures this stub to throw this error.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
throwWith(err: Error): Stub<T>;
/**
* Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
callback(...args: any[]): Stub<T>;
/**
* Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
callbackWith(...args: any[]): Stub<T>;
/**
* Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
callbackAtIndex(cbArgumentIndex: number, ...args: any[]): Stub<T>;
/**
* Configures this stub to call back with the arguments passed. It will use either the last argument as callback, or the argument at cbArgumentIndex.
* Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub.
*/
callbackArgWith(cbArgumentIndex: number, ...args: any[]): Stub<T>;
/**
* Configures the last configured function or callback to be called in this context, i.e. this will be obj.
*/
inThisContext(obj: any): Stub<T>;
/**
* Configures the stub to return a Promise (where available] resolving to this value. Same as stub.returnWith(Promise.resolve(val)).
* You can use a custom Promise-conforming library, i.e. simple.Promise = require('bluebird') or simple.Promise = $q.
*/
resolveWith<V>(val: V): Stub<PromiseLike<V>>;
/**
* Configures the stub to return a Promise (where available) rejecting with this error. Same as stub.returnWith(Promise.reject(val)).
* You can use a custom Promise-conforming library, i.e. simple.Promise = require('bluebird') or simple.Promise = $q.
*/
rejectWith<V>(val: V): Stub<PromiseLike<V>>;
/**
* An array of behaviours, each having one of these properties:
*/
actions: Action<T>[];
/**
* setting whether the queue of actions for this stub should repeat.
* @default true
*/
loop: boolean;
}
}
declare module "simple-mock" {
var simple: Simple.Static;
export = simple;
}