From 7179a08b162ce6f2b2deac9b414606bef0a87535 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Sun, 17 Dec 2017 19:03:49 +0000 Subject: [PATCH] Add tests to the toArray filter --- lib/plugins/helper/to_array.js | 5 ++- test/scripts/renderers/nunjucks.js | 54 ++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/lib/plugins/helper/to_array.js b/lib/plugins/helper/to_array.js index b33c6f94a8..795b12c180 100644 --- a/lib/plugins/helper/to_array.js +++ b/lib/plugins/helper/to_array.js @@ -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; diff --git a/test/scripts/renderers/nunjucks.js b/test/scripts/renderers/nunjucks.js index 9a20ed3024..a03d389d52 100644 --- a/test/scripts/renderers/nunjucks.js +++ b/test/scripts/renderers/nunjucks.js @@ -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 }}!' @@ -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'); + }); + }); });