Skip to content

Commit ae5ca48

Browse files
committed
implement top-level satisfy/invoke-callbacks
1 parent 6c8cd4f commit ae5ca48

File tree

3 files changed

+103
-28
lines changed

3 files changed

+103
-28
lines changed

src/satisfy/invoke-callbacks.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1+
import _ from '../wrap/lodash'
2+
3+
import isCallback from '../matchers/is-callback'
4+
import callLater from '../share/call-later'
5+
16
export default function invokeCallbacks (stubbing, call) {
2-
}
3-
/*
4-
var invokeCallbackFor = (stubbing, actualArgs) => {
5-
if (_.some(stubbing.args, isCallback)) {
6-
_.each(stubbing.args, (expectedArg, i) => {
7-
if (isCallback(expectedArg)) {
8-
callCallback(stubbing, actualArgs[i], callbackArgs(stubbing, expectedArg))
9-
}
10-
})
11-
}
7+
_.each(stubbing.args, (stubbingArg, i) => {
8+
if (isCallback(stubbingArg)) {
9+
const actualCallback = call.args[i]
10+
callLater(
11+
actualCallback,
12+
callbackArgs(stubbing, stubbingArg),
13+
stubbing.options.delay,
14+
stubbing.options.defer
15+
)
16+
}
17+
})
1218
}
1319

14-
var callbackArgs = (stubbing, expectedArg) => {
15-
if (expectedArg.args != null) {
16-
return expectedArg.args
17-
} else if (stubbing.config.plan === 'thenCallback') {
18-
return stubbing.stubbedValues
20+
function callbackArgs (stubbing, callbackMatcher) {
21+
if (callbackMatcher.args != null) {
22+
return callbackMatcher.args
23+
} else if (stubbing.type === 'thenCallback') {
24+
return stubbing.outcomes
1925
} else {
2026
return []
2127
}
2228
}
23-
24-
// stick this in a shared place?
25-
// callLater(func, delay, defer)
26-
var callCallback = (stubbing, callback, args) => {
27-
if (stubbing.config.delay) {
28-
return _.delay(callback, stubbing.config.delay, ...args)
29-
} else if (stubbing.config.defer) {
30-
return _.defer(callback, ...args)
31-
} else {
32-
return callback(...args) // eslint-disable-line
33-
}
34-
}
35-
*/

src/share/call-later.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default function callLater () {
2+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import Call from '../../../src/value/call'
2+
import Stubbing from '../../../src/value/stubbing'
3+
4+
let isCallback, callLater, subject
5+
module.exports = {
6+
beforeEach: () => {
7+
isCallback = td.replace('../../../src/matchers/is-callback').default
8+
callLater = td.replace('../../../src/share/call-later').default
9+
10+
subject = require('../../../src/satisfy/invoke-callbacks').default
11+
},
12+
'invokes a basic callback matcher with args': () => {
13+
// TODO: once matchers & callbacks are ported to value types, replace w/that
14+
const callbackMatcher = {args: ['pants']}
15+
const stubbing = new Stubbing('thenBlah', [42, callbackMatcher])
16+
const realCallback = () => {}
17+
const call = new Call(null, [42, realCallback])
18+
td.when(isCallback(callbackMatcher)).thenReturn(true)
19+
20+
subject(stubbing, call)
21+
22+
td.verify(callLater(realCallback, ['pants'], undefined, undefined))
23+
},
24+
'invokes an arg-less callback matcher marker': () => {
25+
const callbackMatcher = {}
26+
const stubbing = new Stubbing('thenBlah', [callbackMatcher, 12])
27+
const realCallback = () => {}
28+
const call = new Call(null, [realCallback, 12])
29+
td.when(isCallback(callbackMatcher)).thenReturn(true)
30+
31+
subject(stubbing, call)
32+
33+
td.verify(callLater(realCallback, [], undefined, undefined))
34+
},
35+
'invoked a thenCallback': () => {
36+
const callbackMatcher = {}
37+
const stubbing = new Stubbing('thenCallback', [callbackMatcher], ['kaka', 2])
38+
const realCallback = () => {}
39+
const call = new Call(null, [realCallback])
40+
td.when(isCallback(callbackMatcher)).thenReturn(true)
41+
42+
subject(stubbing, call)
43+
44+
td.verify(callLater(realCallback, ['kaka', 2], undefined, undefined))
45+
},
46+
'invokes two callback matchers': () => {
47+
const callbackMatcher1 = 'a'
48+
const callbackMatcher2 = 'b'
49+
const stubbing = new Stubbing('thenBlah', [callbackMatcher1, callbackMatcher2])
50+
const realCallback1 = () => {}
51+
const realCallback2 = () => {}
52+
const call = new Call(null, [realCallback1, realCallback2])
53+
td.when(isCallback(callbackMatcher1)).thenReturn(true)
54+
td.when(isCallback(callbackMatcher2)).thenReturn(true)
55+
56+
subject(stubbing, call)
57+
58+
td.verify(callLater(realCallback1, [], undefined, undefined))
59+
td.verify(callLater(realCallback2, [], undefined, undefined))
60+
},
61+
'calls nothing if nothing is a callback': () => {
62+
const stubbing = new Stubbing('thenBlah', ['foo', 'bar'], ['pants'])
63+
const call = new Call(null, ['foo', 'bar'])
64+
65+
subject(stubbing, call)
66+
67+
assert.equal(td.explain(callLater).callCount, 0)
68+
},
69+
'passes delay & defer settings to callLater': () => {
70+
const callbackMatcher = {}
71+
const stubbing = new Stubbing('thenBlah', [callbackMatcher], [], {delay: 42, defer: true})
72+
const realCallback = () => {}
73+
const call = new Call(null, [realCallback])
74+
td.when(isCallback(callbackMatcher)).thenReturn(true)
75+
76+
subject(stubbing, call)
77+
78+
td.verify(callLater(realCallback, [], 42, true))
79+
}
80+
}

0 commit comments

Comments
 (0)