Skip to content

Commit

Permalink
Merge pull request #56 from sarchila/SA-forEach-else
Browse files Browse the repository at this point in the history
check if collection is a collection object before checking whether it is empty
  • Loading branch information
jmerrifield committed Oct 8, 2015
2 parents cbca1b1 + a6bdf02 commit 7b3b7a6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
10 changes: 5 additions & 5 deletions shared/helpers/forEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module.exports = function (collection, opts) {
isCollection = app.modelUtils.isCollection(collection),
buffer = '';

// iterate the models on a collection
if (isCollection) {
collection = collection.models
}

if (_.isEmpty(collection)) {
return opts.inverse(_.extend({}, this, {
_app: app,
Expand All @@ -19,11 +24,6 @@ module.exports = function (collection, opts) {
}));
}

// iterate the models on a collection
if (isCollection) {
collection = collection.models
}

_.each(collection, function (value, key) {
if (isCollection && opts.hash.toJSON) {
value = value.toJSON();
Expand Down
99 changes: 60 additions & 39 deletions test/shared/helpers/forEach.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('forEach', function () {
});

context('empty array', function () {
it('should call teh inverse function', function () {
it('should call the inverse function', function () {
var inverseSpy = sinon.spy(opts(), 'inverse');
subject.call(scope, [], opts());

Expand All @@ -144,65 +144,86 @@ describe('forEach', function () {
})

context('is a collection', function () {
data = { 'a': 'b', 'c': 'd' };
var currentCollection;
var currentCollection,
collection = memo().is(function () {});

var collection = memo().is(function () {
var retVal = new Collection();
for (var i = 0; i < 3; i++) {
data.id = i;
retVal.add(new Model(data));
}

return retVal;
});
isCollection.is(function () {
return function () {
return true;
};
});

context('default', function () {
beforeEach(function () {
currentCollection = collection();
context('collection is not empty', function () {
data = { 'a': 'b', 'c': 'd' };
collection.is(function () {
var retVal = new Collection();
for (var i = 0; i < 3; i++) {
data.id = i;
retVal.add(new Model(data));
}

return retVal;
});

context('default', function () {
beforeEach(function () {
currentCollection = collection();
})

it('will set the _ array properties', function () {
subject.call(scope, currentCollection, opts());
expect(spy).to.have.been.calledThrice;

var thisCall = spy.getCall(0),
args = thisCall.args[0];

expect(args).to.have.property('_isFirst', true);
expect(args).to.have.property('_isLast', false);
expect(args).to.have.property('_total', 3);
})

it('should pass a model as the value', function () {
subject.call(scope, currentCollection, opts());
expect(spy).to.have.been.calledThrice;

var thisCall = spy.getCall(0);
expect(thisCall.args[0].value).to.deep.equal(currentCollection.first());
})
})

it('will set the _ array properties', function () {
subject.call(scope, currentCollection, opts());
expect(spy).to.have.been.calledThrice;
context('is toJSON', function () {
var jsonOpts;

var thisCall = spy.getCall(0),
args = thisCall.args[0];
beforeEach(function () {
currentCollection = collection();
jsonOpts = opts();
jsonOpts.hash.toJSON = true;
})

expect(args).to.have.property('_isFirst', true);
expect(args).to.have.property('_isLast', false);
expect(args).to.have.property('_total', 3);
})
it('should pass a jsonified version of the model', function () {
subject.call(scope, currentCollection, jsonOpts);
expect(spy).to.have.been.calledThrice;

it('should pass a model as the value', function () {
subject.call(scope, currentCollection, opts());
expect(spy).to.have.been.calledThrice;

var thisCall = spy.getCall(0);
expect(thisCall.args[0].value).to.deep.equal(currentCollection.first());
var thisCall = spy.getCall(0);
expect(thisCall.args[0].value).to.deep.equal(currentCollection.first().toJSON());
})
})
})

context('is toJSON', function () {
var jsonOpts;
context('collection is empty', function () {
collection.is(function () {
return new Collection();
});

beforeEach(function () {
currentCollection = collection();
jsonOpts = opts();
jsonOpts.hash.toJSON = true;
})

it('should pass a jsonified version of the model', function () {
subject.call(scope, currentCollection, jsonOpts);
expect(spy).to.have.been.calledThrice;
it('should call the inverse function', function () {
var inverseSpy = sinon.spy(opts(), 'inverse');
subject.call(scope, currentCollection, opts());

var thisCall = spy.getCall(0);
expect(thisCall.args[0].value).to.deep.equal(currentCollection.first().toJSON());
expect(inverseSpy).to.have.been.called;
})
})
})
Expand Down

0 comments on commit 7b3b7a6

Please sign in to comment.