|
| 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