Last active
May 12, 2017 09:41
-
-
Save brunops/f8987dc59f7614765c2b to your computer and use it in GitHub Desktop.
Promise chain test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var q = require('q'), | |
assert = require('chai').assert, | |
sinon = require('sinon'); | |
var obj = { | |
load: function () { | |
console.log('-- `load` called with: %s', JSON.stringify(arguments)); | |
var defer = q.defer(); | |
setTimeout(function () { | |
console.log('---- `load` resolved'); | |
defer.resolve(5); | |
}, 500); | |
return defer.promise; | |
}, | |
transform1: function (data) { | |
console.log('-- `transform1` called with: %s', JSON.stringify(arguments)); | |
return data + 1; | |
}, | |
transform2: function (data) { | |
console.log('-- `transform2` called with: %s', JSON.stringify(arguments)); | |
return data + 2; | |
}, | |
transform3: function (data) { | |
console.log('-- `transform3` called with: %s', JSON.stringify(arguments)); | |
return data + 3; | |
}, | |
execute: function () { | |
console.log('executing promise chain'); | |
obj.load() | |
.then(obj.transform1) | |
.then(obj.transform2) | |
.then(obj.transform3) | |
.done(); | |
} | |
}; | |
describe('`obj#execute` promise chain test', function () { | |
var loadDefer, | |
loadStub, | |
transform1Defer, | |
transform1Stub, | |
transform2Defer, | |
transform2Stub, | |
transform3Defer, | |
transform3Stub; | |
beforeEach(function () { | |
loadDefer = q.defer(); | |
loadStub = sinon.stub(obj, 'load'); | |
loadStub.returns(loadDefer.promise); | |
transform1Defer = q.defer(); | |
transform1Stub = sinon.stub(obj, 'transform1'); | |
transform1Stub.returns(transform1Defer.promise); | |
transform2Defer = q.defer(); | |
transform2Stub = sinon.stub(obj, 'transform2'); | |
transform2Stub.returns(transform2Defer.promise); | |
transform3Defer = q.defer(); | |
transform3Stub = sinon.stub(obj, 'transform3'); | |
transform3Stub.returns(transform3Defer.promise); | |
}); | |
afterEach(function () { | |
loadStub.restore(); | |
transform1Stub.restore(); | |
transform2Stub.restore(); | |
transform3Stub.restore(); | |
}); | |
it('`load` is called', function () { | |
obj.execute(); | |
assert.isTrue(loadStub.called); | |
}); | |
it('`transform1` is called with the resolution of `load`', function (done) { | |
var result = { foo: 'bar' }; | |
obj.execute(); | |
loadStub() | |
.then(function (data) { | |
console.log('load .then'); | |
console.log(data); | |
assert.isTrue(transform1Stub.calledWith(result)); | |
done(); | |
}) | |
.catch(function (err) { | |
done(err); | |
}); | |
loadDefer.resolve(result); | |
}); | |
// this test is a bit verbose, the following tests show a pattern | |
it('`transform2` is called with the resolution of `transform1`', function (done) { | |
var result = { foo: 'bar' }; | |
obj.execute(); | |
loadStub() | |
.then(function () { | |
transform1Stub() | |
.then(function (data) { | |
console.log('transform1 .then'); | |
console.log(data); | |
assert.isTrue(transform2Stub.calledWith(result)); | |
done(); | |
}) | |
.catch(function (err) { | |
done(err); | |
}); | |
transform1Defer.resolve(result); | |
}) | |
.catch(function (err) { | |
done(err); | |
}); | |
loadDefer.resolve(); | |
}); | |
it('`transform2` is called with the resolution of `transform1`', function (done) { | |
var result = { foo: 'bar' }; | |
obj.execute(); | |
loadStub() | |
.then(transform1Stub) | |
.then(function (data) { | |
console.log('transform1 .then'); | |
console.log(data); | |
assert.isTrue(transform2Stub.calledWith(result)); | |
done(); | |
}) | |
.catch(function (err) { | |
done(err); | |
}); | |
loadDefer.resolve(); | |
transform1Defer.resolve(result); | |
}); | |
it('`transform3` is called with the resolution of `transform2`', function (done) { | |
var result = { foo: 'bar' }; | |
obj.execute(); | |
loadStub() | |
.then(transform1Stub) | |
.then(transform2Stub) | |
.then(function (data) { | |
console.log('transform2 .then'); | |
console.log(data); | |
assert.isTrue(transform3Stub.calledWith(result)); | |
done(); | |
}) | |
.catch(function (err) { | |
done(err); | |
}); | |
loadDefer.resolve(); | |
transform1Defer.resolve(); | |
transform2Defer.resolve(result); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment