2020-04-29 18:44:27 +03:00
const should = require ( 'should' ) ;
const sinon = require ( 'sinon' ) ;
const _ = require ( 'lodash' ) ;
const helpers = require ( '../../../core/frontend/helpers' ) ;
2021-04-19 17:09:35 +03:00
const handlebars = require ( '../../../core/frontend/services/theme-engine/engine' ) . handlebars ;
2014-10-10 18:54:07 +04:00
describe ( '{{#foreach}} helper' , function ( ) {
2020-04-29 18:44:27 +03:00
let options ;
let context ;
let _this ;
let resultData ;
2015-06-27 18:40:37 +03:00
afterEach ( function ( ) {
2019-01-21 19:53:44 +03:00
sinon . restore ( ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-06-27 18:40:37 +03:00
describe ( '(function call)' , function ( ) {
beforeEach ( function ( ) {
context = [ ] ;
_this = { } ;
resultData = [ ] ;
function fn ( input , data ) {
resultData . push ( _ . cloneDeep ( data ) ) ;
}
options = {
2019-01-21 19:53:44 +03:00
fn : sinon . spy ( fn ) ,
inverse : sinon . spy ( ) ,
2015-06-27 18:40:37 +03:00
data : { }
} ;
} ) ;
2020-10-19 07:45:26 +03:00
function runTest ( self , _context , _options ) {
helpers . foreach . call ( self , _context , _options ) ;
2014-10-10 18:54:07 +04:00
}
2015-06-27 18:40:37 +03:00
it ( 'should not populate data if no private data is supplied (array)' , function ( ) {
delete options . data ;
options . hash = {
columns : 0
} ;
// test with context as an array
context = 'hello world this is ghost' . split ( ' ' ) ;
runTest ( _this , context , options ) ;
2016-02-08 00:27:01 +03:00
options . fn . called . should . be . true ( ) ;
2015-06-27 18:40:37 +03:00
options . fn . getCalls ( ) . length . should . eql ( _ . size ( context ) ) ;
_ . each ( context , function ( value , index ) {
options . fn . getCall ( index ) . args [ 0 ] . should . eql ( value ) ;
2016-02-15 01:48:20 +03:00
should ( options . fn . getCall ( index ) . args [ 1 ] . data ) . be . undefined ( ) ;
2015-06-27 18:40:37 +03:00
} ) ;
} ) ;
it ( 'should not populate data if no private data is supplied (object)' , function ( ) {
delete options . data ;
options . hash = {
columns : 0
} ;
context = {
one : 'hello' ,
two : 'world' ,
three : 'this' ,
four : 'is' ,
five : 'ghost'
} ;
runTest ( _this , context , options ) ;
2016-02-08 00:27:01 +03:00
options . fn . called . should . be . true ( ) ;
2015-06-27 18:40:37 +03:00
options . fn . getCalls ( ) . length . should . eql ( _ . size ( context ) ) ;
_ . each ( _ . keys ( context ) , function ( value , index ) {
options . fn . getCall ( index ) . args [ 0 ] . should . eql ( context [ value ] ) ;
2016-02-08 00:27:01 +03:00
should ( options . fn . getCall ( index ) . args [ 1 ] . data ) . be . undefined ( ) ;
2015-06-27 18:40:37 +03:00
} ) ;
} ) ;
it ( 'should populate data when private data is supplied (array)' , function ( ) {
2020-04-29 18:44:27 +03:00
const expected = [
2015-06-27 18:40:37 +03:00
{ first : true , last : false , even : false , odd : true , rowStart : false , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : false } ,
{ first : false , last : false , even : false , odd : true , rowStart : false , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : false } ,
{ first : false , last : true , even : false , odd : true , rowStart : false , rowEnd : false }
] ;
options . hash = {
columns : 0
} ;
context = 'hello world this is ghost' . split ( ' ' ) ;
runTest ( _this , context , options ) ;
2016-02-08 00:27:01 +03:00
options . fn . called . should . be . true ( ) ;
2015-06-27 18:40:37 +03:00
options . fn . getCalls ( ) . length . should . eql ( _ . size ( context ) ) ;
_ . each ( context , function ( value , index ) {
options . fn . getCall ( index ) . args [ 0 ] . should . eql ( value ) ;
2016-02-08 00:27:01 +03:00
should ( options . fn . getCall ( index ) . args [ 1 ] . data ) . not . be . undefined ( ) ;
2015-06-27 18:40:37 +03:00
// Expected properties
resultData [ index ] . data . should . containEql ( expected [ index ] ) ;
// Incrementing properties
resultData [ index ] . data . should . have . property ( 'key' , index ) ;
resultData [ index ] . data . should . have . property ( 'index' , index ) ;
resultData [ index ] . data . should . have . property ( 'number' , index + 1 ) ;
} ) ;
resultData [ _ . size ( context ) - 1 ] . data . should . eql ( options . fn . lastCall . args [ 1 ] . data ) ;
} ) ;
it ( 'should populate data when private data is supplied (object)' , function ( ) {
2020-04-29 18:44:27 +03:00
const expected = [
2015-06-27 18:40:37 +03:00
{ first : true , last : false , even : false , odd : true , rowStart : false , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : false } ,
{ first : false , last : false , even : false , odd : true , rowStart : false , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : false } ,
{ first : false , last : true , even : false , odd : true , rowStart : false , rowEnd : false }
] ;
options . hash = {
columns : 0
} ;
context = {
one : 'hello' ,
two : 'world' ,
three : 'this' ,
four : 'is' ,
five : 'ghost'
} ;
runTest ( _this , context , options ) ;
2016-02-08 00:27:01 +03:00
options . fn . called . should . be . true ( ) ;
2015-06-27 18:40:37 +03:00
options . fn . getCalls ( ) . length . should . eql ( _ . size ( context ) ) ;
_ . each ( _ . keys ( context ) , function ( value , index ) {
options . fn . getCall ( index ) . args [ 0 ] . should . eql ( context [ value ] ) ;
2016-02-15 01:48:20 +03:00
should ( options . fn . getCall ( index ) . args [ 1 ] . data ) . not . be . undefined ( ) ;
2015-06-27 18:40:37 +03:00
// Expected properties
resultData [ index ] . data . should . containEql ( expected [ index ] ) ;
// Incrementing properties
resultData [ index ] . data . should . have . property ( 'key' , value ) ;
resultData [ index ] . data . should . have . property ( 'index' , index ) ;
resultData [ index ] . data . should . have . property ( 'number' , index + 1 ) ;
} ) ;
resultData [ _ . size ( context ) - 1 ] . data . should . eql ( options . fn . lastCall . args [ 1 ] . data ) ;
} ) ;
it ( 'should handle rowStart and rowEnd for multiple columns (array)' , function ( ) {
2020-04-29 18:44:27 +03:00
const expected = [
2015-06-27 18:40:37 +03:00
{ first : true , last : false , even : false , odd : true , rowStart : true , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : true } ,
{ first : false , last : false , even : false , odd : true , rowStart : true , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : true } ,
{ first : false , last : true , even : false , odd : true , rowStart : true , rowEnd : false }
] ;
options . hash = {
columns : 2
} ;
// test with context as an array
context = 'hello world this is ghost' . split ( ' ' ) ;
runTest ( _this , context , options ) ;
2016-02-08 00:27:01 +03:00
options . fn . called . should . be . true ( ) ;
2015-06-27 18:40:37 +03:00
options . fn . getCalls ( ) . length . should . eql ( _ . size ( context ) ) ;
_ . each ( context , function ( value , index ) {
options . fn . getCall ( index ) . args [ 0 ] . should . eql ( value ) ;
2016-02-08 00:27:01 +03:00
should ( options . fn . getCall ( index ) . args [ 1 ] . data ) . not . be . undefined ( ) ;
2015-06-27 18:40:37 +03:00
// Expected properties
resultData [ index ] . data . should . containEql ( expected [ index ] ) ;
// Incrementing properties
resultData [ index ] . data . should . have . property ( 'key' , index ) ;
resultData [ index ] . data . should . have . property ( 'index' , index ) ;
resultData [ index ] . data . should . have . property ( 'number' , index + 1 ) ;
} ) ;
resultData [ _ . size ( context ) - 1 ] . data . should . eql ( options . fn . lastCall . args [ 1 ] . data ) ;
} ) ;
it ( 'should handle rowStart and rowEnd for multiple columns (array)' , function ( ) {
2020-04-29 18:44:27 +03:00
const expected = [
2015-06-27 18:40:37 +03:00
{ first : true , last : false , even : false , odd : true , rowStart : true , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : true } ,
{ first : false , last : false , even : false , odd : true , rowStart : true , rowEnd : false } ,
{ first : false , last : false , even : true , odd : false , rowStart : false , rowEnd : true } ,
{ first : false , last : true , even : false , odd : true , rowStart : true , rowEnd : false }
] ;
options . hash = {
columns : 2
} ;
// test with context as an object
context = {
one : 'hello' ,
two : 'world' ,
three : 'this' ,
four : 'is' ,
five : 'ghost'
} ;
runTest ( _this , context , options ) ;
2016-02-08 00:27:01 +03:00
options . fn . called . should . be . true ( ) ;
2015-06-27 18:40:37 +03:00
options . fn . getCalls ( ) . length . should . eql ( _ . size ( context ) ) ;
_ . each ( _ . keys ( context ) , function ( value , index ) {
options . fn . getCall ( index ) . args [ 0 ] . should . eql ( context [ value ] ) ;
2016-02-08 00:27:01 +03:00
should ( options . fn . getCall ( index ) . args [ 1 ] . data ) . not . be . undefined ( ) ;
2015-06-27 18:40:37 +03:00
// Expected properties
resultData [ index ] . data . should . containEql ( expected [ index ] ) ;
// Incrementing properties
resultData [ index ] . data . should . have . property ( 'key' , value ) ;
resultData [ index ] . data . should . have . property ( 'index' , index ) ;
resultData [ index ] . data . should . have . property ( 'number' , index + 1 ) ;
} ) ;
resultData [ _ . size ( context ) - 1 ] . data . should . eql ( options . fn . lastCall . args [ 1 ] . data ) ;
} ) ;
it ( 'should return the correct inverse result if no context is provided' , function ( ) {
_this = 'the inverse data' ;
options . hash = {
columns : 0
} ;
runTest ( _this , context , options ) ;
2016-02-08 00:27:01 +03:00
options . fn . called . should . be . false ( ) ;
options . inverse . called . should . be . true ( ) ;
options . inverse . calledOnce . should . be . true ( ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
} ) ;
2015-06-27 18:40:37 +03:00
describe ( '(compile)' , function ( ) {
2020-04-29 18:44:27 +03:00
const objectHash = {
posts : {
first : { title : 'first' } ,
second : { title : 'second' } ,
third : { title : 'third' } ,
fourth : { title : 'fourth' } ,
fifth : { title : 'fifth' }
}
} ;
2021-03-01 11:27:39 +03:00
const objectHashWithVis = {
posts : {
first : { title : 'first' , visibility : 'members' , slug : 'first' , html : '' } ,
second : { title : 'second' } ,
third : { title : 'third' } ,
fourth : { title : 'fourth' } ,
fifth : { title : 'fifth' }
}
} ;
2020-04-29 18:44:27 +03:00
const arrayHash = {
posts : [
{ title : 'first' } , { title : 'second' } , { title : 'third' } , { title : 'fourth' } , { title : 'fifth' }
]
} ;
2021-03-01 11:27:39 +03:00
const arrayHashWithVis = {
posts : [
{ title : 'first' , visibility : 'members' , slug : 'first' , html : '' } , { title : 'second' } , { title : 'third' } , { title : 'fourth' } , { title : 'fifth' }
]
} ;
2020-04-29 18:44:27 +03:00
const arrayHash2 = { goodbyes : [ { text : 'goodbye' } , { text : 'Goodbye' } , { text : 'GOODBYE' } ] , world : 'world' } ;
const objectHash2 = {
goodbyes : { foo : { text : 'goodbye' } , bar : { text : 'Goodbye' } , baz : { text : 'GOODBYE' } } ,
world : 'world'
} ;
2015-12-11 00:54:27 +03:00
2015-06-27 18:40:37 +03:00
function shouldCompileToExpected ( templateString , hash , expected ) {
2020-04-29 18:44:27 +03:00
const template = handlebars . compile ( templateString ) ;
const result = template ( hash ) ;
2014-10-10 18:54:07 +04:00
2015-06-27 18:40:37 +03:00
result . should . eql ( expected ) ;
}
2014-10-10 18:54:07 +04:00
2017-03-23 22:00:58 +03:00
before ( function ( ) {
handlebars . registerHelper ( 'foreach' , helpers . foreach ) ;
} ) ;
2015-06-27 18:40:37 +03:00
/** Many of these are copied direct from the handlebars spec */
2015-12-11 00:54:27 +03:00
it ( 'object and @key' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts}}<li>{{@key}} {{title}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>first first</li><li>second second</li><li>third third</li><li>fourth fourth</li><li>fifth fifth</li></ul>' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , objectHash , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( '@index' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts}}<li>{{@index}} {{title}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>0 first</li><li>1 second</li><li>2 third</li><li>3 fourth</li><li>4 fifth</li></ul>' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( '@number' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts}}<li>{{@number}} {{title}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>1 first</li><li>2 second</li><li>3 third</li><li>4 fourth</li><li>5 fifth</li></ul>' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( 'nested @index' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes}}{{@index}}. {{text}}! {{#foreach ../goodbyes}}{{@index}} {{/foreach}}After {{@index}} {{/foreach}}{{@index}}cruel {{world}}!' ;
const expected = '0. goodbye! 0 1 2 After 0 1. Goodbye! 0 1 2 After 1 2. GOODBYE! 0 1 2 After 2 cruel world!' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash2 , expected ) ;
shouldCompileToExpected ( templateString , objectHash2 , expected ) ;
} ) ;
it ( 'array block params' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes as |value index|}}{{index}}. {{value.text}}! {{#foreach ../goodbyes as |childValue childIndex|}} {{index}} {{childIndex}}{{/foreach}} After {{index}} {{/foreach}}{{index}}cruel {{world}}!' ;
const expected = '0. goodbye! 0 0 0 1 0 2 After 0 1. Goodbye! 1 0 1 1 1 2 After 1 2. GOODBYE! 2 0 2 1 2 2 After 2 cruel world!' ;
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash2 , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( 'object block params' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes as |value index|}}{{index}}. {{value.text}}! {{#foreach ../goodbyes as |childValue childIndex|}} {{index}} {{childIndex}}{{/foreach}} After {{index}} {{/foreach}}{{index}}cruel {{world}}!' ;
const expected = 'foo. goodbye! foo foo foo bar foo baz After foo bar. Goodbye! bar foo bar bar bar baz After bar baz. GOODBYE! baz foo baz bar baz baz After baz cruel world!' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , objectHash2 , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( '@first' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes}}{{#if @first}}{{text}}! {{/if}}{{/foreach}}cruel {{world}}!' ;
const expected = 'goodbye! cruel world!' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash2 , expected ) ;
shouldCompileToExpected ( templateString , objectHash2 , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( 'nested @first' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes}}({{#if @first}}{{text}}! {{/if}}{{#foreach ../goodbyes}}{{#if @first}}{{text}}!{{/if}}{{/foreach}}{{#if @first}} {{text}}!{{/if}}) {{/foreach}}cruel {{world}}!' ;
const expected = '(goodbye! goodbye! goodbye!) (goodbye!) (goodbye!) cruel world!' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash2 , expected ) ;
shouldCompileToExpected ( templateString , objectHash2 , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( '@last' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes}}{{#if @last}}{{text}}! {{/if}}{{/foreach}}cruel {{world}}!' ;
const expected = 'GOODBYE! cruel world!' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash2 , expected ) ;
shouldCompileToExpected ( templateString , objectHash2 , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( 'nested @last' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes}}({{#if @last}}{{text}}! {{/if}}{{#foreach ../goodbyes}}{{#if @last}}{{text}}!{{/if}}{{/foreach}}{{#if @last}} {{text}}!{{/if}}) {{/foreach}}cruel {{world}}!' ;
const expected = '(GOODBYE!) (GOODBYE!) (GOODBYE! GOODBYE! GOODBYE!) cruel world!' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash2 , expected ) ;
shouldCompileToExpected ( templateString , objectHash2 , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-18 17:06:37 +03:00
it ( '@last in foreach with limit' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '{{#foreach goodbyes limit="2"}}{{#if @last}}{{text}}! {{/if}}{{/foreach}}cruel {{world}}!' ;
const expected = 'Goodbye! cruel world!' ;
2015-12-18 17:06:37 +03:00
shouldCompileToExpected ( templateString , arrayHash2 , expected ) ;
shouldCompileToExpected ( templateString , objectHash2 , expected ) ;
} ) ;
2015-12-11 00:54:27 +03:00
it ( 'foreach with limit 1' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts limit="1"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>first</li></ul>' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
it ( 'foreach with limit 3' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts limit="3"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>first</li><li>second</li><li>third</li></ul>' ;
2014-10-10 18:54:07 +04:00
2015-12-11 00:54:27 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
2015-06-27 18:40:37 +03:00
} ) ;
2015-12-29 20:52:13 +03:00
2021-03-01 11:27:39 +03:00
it ( 'foreach with limit 3 and post with members visibility' , function ( ) {
const templateString = '<ul>{{#foreach posts limit="3"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>first</li><li>second</li><li>third</li></ul>' ;
shouldCompileToExpected ( templateString , arrayHashWithVis , expected ) ;
shouldCompileToExpected ( templateString , objectHashWithVis , expected ) ;
} ) ;
2015-12-29 20:52:13 +03:00
it ( 'foreach with from 2' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="2"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>second</li><li>third</li><li>fourth</li><li>fifth</li></ul>' ;
2015-12-29 20:52:13 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( 'foreach with to 4' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts to="4"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>first</li><li>second</li><li>third</li><li>fourth</li></ul>' ;
2015-12-29 20:52:13 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( 'foreach with from 2 and to 3' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="2" to="3"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>second</li><li>third</li></ul>' ;
2015-12-29 20:52:13 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( 'foreach with from 3 and limit 2' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="3" limit="2"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>third</li><li>fourth</li></ul>' ;
2015-12-29 20:52:13 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( 'foreach with from 2, to 5 and limit 3' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="2" to="5" limit="3"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>' ;
const expected = '<ul><li>second</li><li>third</li><li>fourth</li></ul>' ;
2015-12-29 20:52:13 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@first in foreach with from 2 and to 4' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="2" to="4"}}{{#if @first}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>second</li></ul>' ;
2015-12-29 20:52:13 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@last in foreach with from 2 and to 4' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="2" to="4"}}{{#if @last}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>fourth</li></ul>' ;
2015-12-29 20:52:13 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
2016-03-18 00:37:18 +03:00
it ( '@last in foreach with from 4' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="4"}}{{#if @last}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>fifth</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@first in foreach with from 4' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="4"}}{{#if @first}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>fourth</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@last in foreach with to 4' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts to="4"}}{{#if @last}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>fourth</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@first in foreach with to 4' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts to="4"}}{{#if @first}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>first</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@last in foreach with from 4 and limit 3' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="4" limit="3"}}{{#if @last}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>fifth</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@first in foreach with from 4 and limit 3' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts from="4" limit="3"}}{{#if @first}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>fourth</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@last in foreach with to 4 and limit 3' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts to="4" limit="3"}}{{#if @last}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>third</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
it ( '@first in foreach with to 4 and limit 3' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach posts to="4" limit="3"}}{{#if @first}}<li>{{title}}</li>{{/if}}{{/foreach}}</ul>' ;
const expected = '<ul><li>first</li></ul>' ;
2016-03-18 00:37:18 +03:00
shouldCompileToExpected ( templateString , arrayHash , expected ) ;
shouldCompileToExpected ( templateString , objectHash , expected ) ;
} ) ;
2016-06-11 14:49:08 +03:00
describe ( 'Internal Tags' , function ( ) {
2020-04-29 18:44:27 +03:00
const tagArrayHash = {
tags : [
{ name : 'first' , visibility : 'public' } ,
{ name : 'second' , visibility : 'public' } ,
{ name : 'third' , visibility : 'internal' } ,
{ name : 'fourth' , visibility : 'public' } ,
{ name : 'fifth' , visibility : 'public' }
]
} ;
const tagObjectHash = {
tags : {
first : { name : 'first' , visibility : 'public' } ,
second : { name : 'second' , visibility : 'public' } ,
third : { name : 'third' , visibility : 'internal' } ,
fourth : { name : 'fourth' , visibility : 'public' } ,
fifth : { name : 'fifth' , visibility : 'public' }
}
} ;
2016-06-11 14:49:08 +03:00
2016-10-10 11:51:03 +03:00
it ( 'will not output internal tags' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach tags}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>0 first</li><li>1 second</li><li>2 fourth</li><li>3 fifth</li></ul>' ;
2016-06-11 14:49:08 +03:00
2016-10-10 11:51:03 +03:00
shouldCompileToExpected ( templateString , tagObjectHash , expected ) ;
shouldCompileToExpected ( templateString , tagArrayHash , expected ) ;
} ) ;
it ( 'should still correctly apply from & limit tags' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach tags from="2" limit="2"}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>1 second</li><li>2 fourth</li></ul>' ;
2016-10-10 11:51:03 +03:00
shouldCompileToExpected ( templateString , tagObjectHash , expected ) ;
shouldCompileToExpected ( templateString , tagArrayHash , expected ) ;
} ) ;
2016-06-11 14:49:08 +03:00
2016-10-10 11:51:03 +03:00
it ( 'should output all tags with visibility="all"' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach tags visibility="all"}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>0 first</li><li>1 second</li><li>2 third</li><li>3 fourth</li><li>4 fifth</li></ul>' ;
2016-06-11 14:49:08 +03:00
2016-10-10 11:51:03 +03:00
shouldCompileToExpected ( templateString , tagObjectHash , expected ) ;
shouldCompileToExpected ( templateString , tagArrayHash , expected ) ;
} ) ;
it ( 'should output all tags with visibility property set with visibility="public,internal"' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach tags visibility="public,internal"}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>0 first</li><li>1 second</li><li>2 third</li><li>3 fourth</li><li>4 fifth</li></ul>' ;
2016-10-10 11:51:03 +03:00
shouldCompileToExpected ( templateString , tagObjectHash , expected ) ;
shouldCompileToExpected ( templateString , tagArrayHash , expected ) ;
} ) ;
2016-06-11 14:49:08 +03:00
2016-10-10 11:51:03 +03:00
it ( 'should output all tags with visibility="internal"' , function ( ) {
2020-04-29 18:44:27 +03:00
const templateString = '<ul>{{#foreach tags visibility="internal"}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>' ;
const expected = '<ul><li>0 third</li></ul>' ;
2016-06-11 14:49:08 +03:00
2016-10-10 11:51:03 +03:00
shouldCompileToExpected ( templateString , tagObjectHash , expected ) ;
shouldCompileToExpected ( templateString , tagArrayHash , expected ) ;
2016-06-11 14:49:08 +03:00
} ) ;
2016-10-10 11:51:03 +03:00
it ( 'should output nothing if all tags are internal' , function ( ) {
2020-10-19 07:45:26 +03:00
const internalTagArrayHash = {
2020-04-29 18:44:27 +03:00
tags : [
{ name : 'first' , visibility : 'internal' } ,
{ name : 'second' , visibility : 'internal' }
]
} ;
2020-10-19 07:45:26 +03:00
const internalTagObjectHash = {
2020-04-29 18:44:27 +03:00
tags : {
first : { name : 'first' , visibility : 'internal' } ,
second : { name : 'second' , visibility : 'internal' }
}
} ;
const templateString = '<ul>{{#foreach tags}}<li>{{@index}} {{name}}</li>{{/foreach}}</ul>' ;
const expected = '<ul></ul>' ;
2016-10-10 11:51:03 +03:00
2020-10-19 07:45:26 +03:00
shouldCompileToExpected ( templateString , internalTagObjectHash , expected ) ;
shouldCompileToExpected ( templateString , internalTagArrayHash , expected ) ;
2016-06-11 14:49:08 +03:00
} ) ;
} ) ;
2014-10-10 18:54:07 +04:00
} ) ;
} ) ;