Skip to content

Commit 64e4fe9

Browse files
committed
#rewrite implement satisfy/deliver-outcome
1 parent 8d188ed commit 64e4fe9

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

src/satisfy/deliver-outcome.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
export default function executePlan (stubbing, call) {
2-
// switch stubbing.type
3-
// case 'thenReturn': return value
4-
// case 'thenDo': return value.apply(actualContext, actualArgs)
5-
// case 'thenThrow': throw value
6-
// case 'thenResolve': return createPromise(stubbing, value, true)
7-
// case 'thenReject': return createPromise(stubbing, value, false)
1+
import createPromise from '../share/create-promise'
2+
3+
export default function deliverOutcome (stubbing, call) {
4+
switch (stubbing.type) {
5+
case 'thenReturn': return stubbing.currentOutcome
6+
case 'thenDo': return stubbing.currentOutcome.apply(call.context, call.args)
7+
case 'thenThrow': throw stubbing.currentOutcome
8+
case 'thenResolve': return createPromise(stubbing, true)
9+
case 'thenReject': return createPromise(stubbing, false)
10+
}
811
}

src/share/create-promise.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default function createPromise () {
2+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import Stubbing from '../../../src/value/stubbing'
2+
import Call from '../../../src/value/call'
3+
4+
let createPromise, subject
5+
module.exports = {
6+
beforeEach: () => {
7+
createPromise = td.replace('../../../src/share/create-promise').default
8+
9+
subject = require('../../../src/satisfy/deliver-outcome').default
10+
},
11+
'thenReturn returns current outcome': () => {
12+
const stubbing = new Stubbing('thenReturn', null, ['pants'])
13+
14+
const result = subject(stubbing, new Call())
15+
16+
assert.equal(result, 'pants')
17+
},
18+
'thenDo calls current outcome': () => {
19+
const call = new Call({a: 1}, ['sauce', 'nice'])
20+
let context, args
21+
const userFunc = function (...someArgs) {
22+
context = this
23+
args = someArgs
24+
return 'nailed it'
25+
}
26+
const stubbing = new Stubbing('thenDo', null, [userFunc])
27+
28+
const result = subject(stubbing, call)
29+
30+
assert.equal(result, 'nailed it')
31+
assert.deepEqual(context, {a: 1})
32+
assert.deepEqual(args, ['sauce', 'nice'])
33+
},
34+
'thenThrow throws current outcome': () => {
35+
const error = new Error('woah')
36+
const stubbing = new Stubbing('thenThrow', null, [error])
37+
38+
assert.throws(() => {
39+
subject(stubbing, new Call())
40+
}, /woah/)
41+
},
42+
'thenResolve builds a resolving promise': () => {
43+
const stubbing = new Stubbing('thenResolve')
44+
td.when(createPromise(stubbing, true)).thenReturn('nice')
45+
46+
const result = subject(stubbing, new Call())
47+
48+
assert.equal(result, 'nice')
49+
},
50+
'thenReject builds a rejecting promise': () => {
51+
const stubbing = new Stubbing('thenReject')
52+
td.when(createPromise(stubbing, false)).thenReturn('a reject')
53+
54+
const result = subject(stubbing, new Call())
55+
56+
assert.equal(result, 'a reject')
57+
}
58+
}

0 commit comments

Comments
 (0)