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