feat: pass context to filters

This commit is contained in:
harttle
2019-04-17 10:24:55 +08:00
parent 6fb4796202
commit 00bc1efe6a
15 changed files with 77 additions and 85 deletions
+5 -26
View File
@@ -137,15 +137,15 @@ describe('scope', function () {
return expect(ctx.get('notdefined')).to.be.rejectedWith(/undefined variable: notdefined/)
})
it('should throw when deep variable not exist', async function () {
ctx.scopes.push({ 'foo': 'FOO' })
ctx.push({ foo: 'FOO' })
return expect(ctx.get('foo.bar.not.defined')).to.be.rejectedWith(/undefined variable: bar/)
})
it('should throw when itself not defined', async function () {
ctx.scopes.push({ 'foo': 'FOO' })
ctx.push({ foo: 'FOO' })
return expect(ctx.get('foo.BAR')).to.be.rejectedWith(/undefined variable: BAR/)
})
it('should find variable in parent scope', async function () {
ctx.scopes.push({ 'foo': 'foo' })
ctx.push({ 'foo': 'foo' })
ctx.push({
'bar': 'bar'
})
@@ -161,7 +161,7 @@ describe('scope', function () {
describe('.push()', function () {
it('should push scope', async function () {
ctx.scopes.push({ 'bar': 'bar' })
ctx.push({ 'bar': 'bar' })
ctx.push({
foo: 'foo'
})
@@ -169,7 +169,7 @@ describe('scope', function () {
expect(await ctx.get('bar')).to.equal('bar')
})
it('should hide deep properties by push', async function () {
ctx.scopes.push({ 'bar': { bar: 'bar' } })
ctx.push({ bar: { bar: 'bar' } })
ctx.push({ bar: { foo: 'foo' } })
expect(await ctx.get('bar.foo')).to.equal('foo')
expect(await ctx.get('bar.bar')).to.equal(undefined)
@@ -184,25 +184,4 @@ describe('scope', function () {
expect(await ctx.get('foo')).to.equal('zoo')
})
})
it('should pop specified scope', async function () {
const scope1 = {
foo: 'foo'
}
const scope2 = {
bar: 'bar'
}
ctx.push(scope1)
ctx.push(scope2)
expect(await ctx.get('foo')).to.equal('foo')
expect(await ctx.get('bar')).to.equal('bar')
ctx.pop(scope1)
expect(await ctx.get('foo')).to.equal('zoo')
expect(await ctx.get('bar')).to.equal('bar')
})
it('should throw when specified scope not found', function () {
const scope1 = {
foo: 'foo'
}
expect(() => ctx.pop(scope1)).to.throw('scope not found, cannot pop')
})
})
+7 -1
View File
@@ -22,12 +22,18 @@ describe('filter', function () {
expect(await new Filter('undefined', [], false).render('foo', ctx)).to.equal('foo')
})
it('should call filter impl with corrct arguments', async function () {
it('should call filter impl with correct arguments', async function () {
const spy = sinon.spy()
Filter.register('foo', spy)
await new Filter('foo', ['33'], false).render('foo', ctx)
expect(spy).to.have.been.calledWith('foo', 33)
})
it('should call filter impl with correct this arg', async function () {
const spy = sinon.spy()
Filter.register('foo', spy)
await new Filter('foo', ['33'], false).render('foo', ctx)
expect(spy).to.have.been.calledOn(sinon.match.has('context', ctx))
})
it('should render a simple filter', async function () {
Filter.register('upcase', x => x.toUpperCase())
expect(await new Filter('upcase', [], false).render('foo', ctx)).to.equal('FOO')