Skip to content

Instantly share code, notes, and snippets.

@brunops
Last active May 12, 2017 09:41

Revisions

  1. brunops revised this gist Sep 14, 2015. 1 changed file with 72 additions and 3 deletions.
    75 changes: 72 additions & 3 deletions promise-chain-test.js
    Original file line number Diff line number Diff line change
    @@ -17,11 +17,16 @@ var obj = {

    transform1: function (data) {
    console.log('-- `transform1` called with: %s', JSON.stringify(arguments));
    return data + 5;
    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;
    },

    @@ -31,6 +36,7 @@ var obj = {
    obj.load()
    .then(obj.transform1)
    .then(obj.transform2)
    .then(obj.transform3)
    .done();
    }
    };
    @@ -44,7 +50,10 @@ describe('`obj#execute` promise chain test', function () {
    transform1Stub,

    transform2Defer,
    transform2Stub;;
    transform2Stub,

    transform3Defer,
    transform3Stub;

    beforeEach(function () {
    loadDefer = q.defer();
    @@ -58,12 +67,17 @@ describe('`obj#execute` promise chain test', function () {
    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 () {
    @@ -92,6 +106,8 @@ describe('`obj#execute` promise chain test', function () {
    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();
    @@ -118,7 +134,60 @@ describe('`obj#execute` promise chain test', function () {
    done(err);
    });

    loadDefer.resolve(1);
    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);
    });
    });


  2. brunops created this gist Sep 14, 2015.
    124 changes: 124 additions & 0 deletions promise-chain-test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    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 + 5;
    },

    transform2: function (data) {
    console.log('-- `transform2` called with: %s', JSON.stringify(arguments));
    return data + 3;
    },

    execute: function () {
    console.log('executing promise chain');

    obj.load()
    .then(obj.transform1)
    .then(obj.transform2)
    .done();
    }
    };


    describe('`obj#execute` promise chain test', function () {
    var loadDefer,
    loadStub,

    transform1Defer,
    transform1Stub,

    transform2Defer,
    transform2Stub;;

    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);
    });

    afterEach(function () {
    loadStub.restore();
    transform1Stub.restore();
    transform2Stub.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);
    });

    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(1);
    });
    });