2018-07-19 19:05:42 +00:00
|
|
|
const compare = require('../../../src/website/theme-base/helpers/compare')
|
2017-11-27 18:56:06 +00:00
|
|
|
const handlebars = require('handlebars')
|
|
|
|
const should = require('should/as-function')
|
|
|
|
|
|
|
|
describe('Handlebars helpers: compare', () => {
|
|
|
|
handlebars.registerHelper('compare', compare)
|
|
|
|
|
|
|
|
it('executes the block if the condition is true', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '==' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const res = template({ value: 3 })
|
2017-11-27 18:56:06 +00:00
|
|
|
should(res).eql('TRUE')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not execute the block if the condition is false', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '==' 3}}{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const res = template({ value: 4 })
|
2017-11-27 18:56:06 +00:00
|
|
|
should(res).eql('')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('executes the {{else}} block if the condition is false', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '==' 3}}TRUE{{else}}FALSE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const res = template({ value: 4 })
|
2017-11-27 18:56:06 +00:00
|
|
|
should(res).eql('FALSE')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('defaults to a strict equality', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const res = template({ value: 3 })
|
2017-11-27 18:56:06 +00:00
|
|
|
should(res).eql('TRUE')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws an error if the operator is not known', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '~~' 3}}TRUE{{/compare}}`)
|
|
|
|
should(() => {
|
2018-12-11 22:11:03 +00:00
|
|
|
template({ value: 3 })
|
2017-11-27 18:56:06 +00:00
|
|
|
}).throw(/operator/)
|
|
|
|
})
|
|
|
|
|
2018-06-08 21:39:09 +00:00
|
|
|
it('throws an error if there arent enough parameters', () => {
|
|
|
|
should(() => {
|
|
|
|
const template = handlebars.compile(`{{#compare value}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
template({ value: 3 })
|
2018-06-08 21:39:09 +00:00
|
|
|
}).throw(/needs 2 parameters/)
|
|
|
|
})
|
|
|
|
|
2017-11-29 09:58:13 +00:00
|
|
|
it('keeps the context when executing the block', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '==' 3}}{{hello}}{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const res = template({ value: 3, hello: 'world' })
|
2017-11-29 09:58:13 +00:00
|
|
|
should(res).eql('world')
|
|
|
|
})
|
|
|
|
|
2017-11-27 18:56:06 +00:00
|
|
|
describe('operators', () => {
|
|
|
|
it('equal', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '==' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 3 })).eql('TRUE')
|
|
|
|
should(template({ value: '3' })).eql('TRUE')
|
|
|
|
should(template({ value: 4 })).eql('')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('strict equal', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '===' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 3 })).eql('TRUE')
|
|
|
|
should(template({ value: '3' })).eql('')
|
|
|
|
should(template({ value: 4 })).eql('')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('different', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '!=' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 3 })).eql('')
|
|
|
|
should(template({ value: '3' })).eql('')
|
|
|
|
should(template({ value: 4 })).eql('TRUE')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('strict different', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '!==' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 3 })).eql('')
|
|
|
|
should(template({ value: '3' })).eql('TRUE')
|
|
|
|
should(template({ value: 4 })).eql('TRUE')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('less than', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '<' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 2 })).eql('TRUE')
|
|
|
|
should(template({ value: 3 })).eql('')
|
|
|
|
should(template({ value: 4 })).eql('')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('greater than', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '>' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 2 })).eql('')
|
|
|
|
should(template({ value: 3 })).eql('')
|
|
|
|
should(template({ value: 4 })).eql('TRUE')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('less or equal', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '<=' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 2 })).eql('TRUE')
|
|
|
|
should(template({ value: 3 })).eql('TRUE')
|
|
|
|
should(template({ value: 4 })).eql('')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('greater or equal', () => {
|
|
|
|
const template = handlebars.compile(`{{#compare value '>=' 3}}TRUE{{/compare}}`)
|
2018-12-11 22:11:03 +00:00
|
|
|
should(template({ value: 2 })).eql('')
|
|
|
|
should(template({ value: 3 })).eql('TRUE')
|
|
|
|
should(template({ value: 4 })).eql('TRUE')
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|