fix string type checking and circular reference, working on #81

This commit is contained in:
harttle
2018-08-14 00:11:50 +08:00
committed by Jun Yang
parent 96829b5293
commit fa5b731daf
3 changed files with 71 additions and 11 deletions
+41 -2
View File
@@ -17,11 +17,17 @@ describe('render', function () {
var scope, render
beforeEach(function () {
scope = Scope.factory({
var ctx = {
foo: {
bar: ['a', 2]
},
bar: {
to_liquid: x => 'custom'
}
})
}
ctx.self = ctx
scope = Scope.factory(ctx)
filter.clear()
tag.clear()
render = Render()
@@ -39,6 +45,34 @@ describe('render', function () {
})
})
describe('.renderValue()', function () {
it('should respect to .to_liquid() method', function () {
var tpl = Template.parseValue('bar')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('custom')
})
it('should stringify objects', function () {
let scope = Scope.factory({
foo: { obj: { arr: ['a', 2] } }
})
let tpl = Template.parseValue('foo')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"obj":{"arr":["a",2]}}')
})
it('should skip circular property', function () {
let ctx = { foo: { num: 2 }, bar: 'bar' }
ctx.foo.circular = ctx
let scope = Scope.factory(ctx)
let tpl = Template.parseValue('foo')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"num":2,"circular":{"bar":"bar"}}')
})
it('should skip function property', function () {
let scope = Scope.factory({obj: {foo: 'foo', bar: x => x}})
let tpl = Template.parseValue('obj')
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"foo":"foo"}')
})
})
it('should eval filter with correct arguments', function () {
var date = sinon.stub().returns('y')
var time = sinon.spy()
@@ -62,5 +96,10 @@ describe('render', function () {
var tpl = Template.parseValue('foo.bar[0] | date: "b" | time:2')
expect(render.evalValue(tpl, scope)).to.equal('ab6')
})
it('should reserve type', function () {
filter.register('arr', () => [1])
var tpl = Template.parseValue('"x" | arr')
expect(render.evalValue(tpl, scope)).to.deep.equal([1])
})
})
})