Skip to content

Commit

Permalink
Add tests to the toArray filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Parisot committed Dec 17, 2017
1 parent 1478019 commit 7179a08
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/plugins/helper/to_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ function toArray(value) {
if (_.isObject(value) && typeof value.toArray === 'function') {
return value.toArray();
}
else if (_.isArray(value)) {
return value;
}

return value;
return _.toArray(value);
}

module.exports = toArray;
54 changes: 36 additions & 18 deletions test/scripts/renderers/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ describe('nunjucks', () => {
}).should.eql('Hello world!\n');
});

it('custom filter toArray to iterate on Warehouse collections', () => {
var body = [
'{% for x in arr | toArray %}',
'{{ x }}',
'{% endfor %}'
].join('');

var data = {
arr: {
toArray() {
return [1, 2, 3];
}
}
};

r({text: body}, data).should.eql('123');
});

it('compile from text', () => {
var body = [
'Hello {{ name }}!'
Expand All @@ -62,4 +44,40 @@ describe('nunjucks', () => {
name: 'world'
}).should.eql('Hello world!\n');
});

describe('nunjucks filters', function(){
var forLoop = [
'{% for x in arr | toArray %}',
'{{ x }}',
'{% endfor %}'
].join('');

it('toArray can iterate on Warehouse collections', () => {
var data = {
arr: {
toArray() {
return [1, 2, 3];
}
}
};

r({text: forLoop}, data).should.eql('123');
});

it('toArray can iterate on plain array', () => {
var data = {
arr: [1, 2, 3]
};

r({text: forLoop}, data).should.eql('123');
});

it('toArray can iterate on string', () => {
var data = {
arr: '123'
};

r({text: forLoop}, data).should.eql('123');
});
});
});

0 comments on commit 7179a08

Please sign in to comment.