UNPKG

882 BJavaScriptView Raw
1import _ from './wrap/lodash'
2import calls from './store/calls'
3import store from './store'
4import stubbings from './store/stubbings'
5import imitate from './imitate'
6
7export default function func (nameOrFunc, __optionalName) {
8 return _.isFunction(nameOrFunc)
9 ? imitate(nameOrFunc)
10 : createTestDoubleNamed(nameOrFunc || __optionalName)
11}
12
13var createTestDoubleNamed = function (name) {
14 return _.tap(createTestDoubleFunction(), (testDouble) => {
15 const entry = store.for(testDouble, true)
16 if (name != null) {
17 entry.name = name
18 testDouble.toString = () => `[test double for "${name}"]`
19 } else {
20 testDouble.toString = () => '[test double (unnamed)]'
21 }
22 })
23}
24
25var createTestDoubleFunction = function () {
26 return function testDouble (...args) {
27 calls.log(testDouble, args, this)
28 return stubbings.invoke(testDouble, args, this)
29 }
30}