Skip to content

Commit 2aab416

Browse files
committed
#rewrite implement top-level satisfy()
1 parent 7c05731 commit 2aab416

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

src/satisfy/execute-plan.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function executePlan () {
2+
}
3+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default function findLastStubbingMatch () {
2+
}

src/satisfy/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
// import _ from '../wrap/lodash'
1+
import findLastStubbingMatch from './find-last-stubbing-match'
2+
import executePlan from './execute-plan'
23

34
export default function satisfy (double, call) {
4-
// TODO: dummy impl to drive create feature first
5-
6-
// 1. find the last matching stubbing (args+conditions match + has values left)
7-
// 2. six-way switch to execute the correct plan switching on the stubbing's `type`
8-
9-
// see src/store/stubbings.isSatisfied for a full solution (arg matchers + multi-plan, etc)
10-
// const stubbing _.findLast(stubbings, (stubbing) =>
11-
// stubbing.isSatisfiedBy(call)
12-
// )
5+
const stubbing = findLastStubbingMatch(double, call)
6+
if (stubbing) {
7+
return executePlan(double, call, stubbing)
8+
}
139
}

test/unit/satisfy/index.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Double from '../../../src/value/double'
2+
import Call from '../../../src/value/call'
3+
import Stubbing from '../../../src/value/stubbing'
4+
5+
let findLastStubbingMatch, executePlan, subject
6+
module.exports = {
7+
beforeEach: () => {
8+
findLastStubbingMatch = td.replace('../../../src/satisfy/find-last-stubbing-match').default
9+
executePlan = td.replace('../../../src/satisfy/execute-plan').default
10+
11+
subject = require('../../../src/satisfy').default
12+
},
13+
'finds the last stubbing & returns the executed plan': () => {
14+
const double = Double.create()
15+
const call = new Call()
16+
const stubbing = new Stubbing()
17+
td.when(findLastStubbingMatch(double, call)).thenReturn(stubbing)
18+
td.when(executePlan(double, call, stubbing)).thenReturn('huzzah')
19+
20+
const result = subject(double, call)
21+
22+
assert.equal(result, 'huzzah')
23+
},
24+
'does nothing if no matching stubbing found': () => {
25+
const double = Double.create()
26+
const call = new Call()
27+
td.when(findLastStubbingMatch(double, call)).thenReturn(undefined)
28+
29+
const result = subject(double, call)
30+
31+
assert.equal(result, undefined)
32+
assert.equal(td.explain(executePlan).callCount, 0)
33+
}
34+
}

0 commit comments

Comments
 (0)