Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
_ensure invocations fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-5v committed May 9, 2013
1 parent a2dd825 commit 08982ce
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -52,9 +52,9 @@ logic.prototype = {
var deps = this.deps || [];

deps.forEach(function(dep) {
var provider = logic._provider(dep);
var provider = logic._provider(dep, params, options);
if (provider) {
promises.push( provider(dep, params) );
promises.push( provider(dep, params, options) );
} else {
logic.error('No provider found for ' + dep);
}
Expand Down
55 changes: 49 additions & 6 deletions test/spec/logic.js
Expand Up @@ -12,6 +12,7 @@ describe('logic', function() {

beforeEach(function() {
logic._list = {};
logic._providers.splice(1);
});

describe('define', function() {
Expand Down Expand Up @@ -69,17 +70,20 @@ describe('logic', function() {
describe('_ensure', function() {

beforeEach(function() {
var that = this;
logic.define('test', {});

this.logic = logic._list['test'];

this.provider = function(to) {
return function() {
var promise = pzero();
setTimeout(function() { promise.resolve('res' + to); }, to);
return promise;
};
this.ctor = function(to) {
var promise = pzero();
setTimeout(function() { promise.resolve('res' + to); }, to);
return promise;
};
this.provider = function(to) { return that.ctor; };

sinon.spy(this, 'ctor');
sinon.spy(this, 'provider');
});

it('should call error when no provider found', function() {
Expand Down Expand Up @@ -121,6 +125,45 @@ describe('logic', function() {
done();
});
});

it('should call provider getter once', function() {
logic.provider(this.provider);
this.logic.deps = ['10'];
this.logic._ensure();

expect(this.provider.calledOnce).to.be.ok();
});

it('should call provider ctor only once', function() {
logic.provider(this.provider);
this.logic.deps = ['10'];
this.logic._ensure();

expect(this.ctor.calledOnce).to.be.ok();
});

it('should pass arguments to provider', function() {
logic.provider(this.provider);
var params = {a: 1};
var options = {b: 2};
this.logic.deps = ['10'];
this.logic._ensure({a: 1}, {b: 2});

expect( this.provider.getCall(0).args )
.to.eql( ['10', {a: 1}, {b: 2}] );
});

it('should pass arguments to ctor', function() {
logic.provider(this.provider);
var params = {a: 1};
var options = {b: 2};
this.logic.deps = ['10'];
this.logic._ensure({a: 1}, {b: 2});

expect( this.ctor.getCall(0).args )
.to.eql( ['10', {a: 1}, {b: 2}] );
});

});

});

0 comments on commit 08982ce

Please sign in to comment.