test: cases for TagMap

This commit is contained in:
harttle
2020-03-07 00:58:39 +08:00
parent 73dd48861d
commit 2c428221cb
3 changed files with 64 additions and 35 deletions
+42
View File
@@ -0,0 +1,42 @@
import { expect } from 'chai'
import { Liquid } from '../../../src/liquid'
describe('liquid#registerTag()', function () {
it('should support render to simple string', async () => {
const liquid = new Liquid()
liquid.registerTag('simple-string', {
render: () => 'B'
})
const html = await liquid.parseAndRender(`A{% simple-string %}C`)
return expect(html).to.equal('ABC')
})
it('should support async tag render', async () => {
const liquid = new Liquid()
liquid.registerTag('async-string', {
render: async () => 'B'
})
const html = await liquid.parseAndRender(`A{% async-string %}C`)
return expect(html).to.equal('ABC')
})
it('should have access to ctx in render()', async () => {
const liquid = new Liquid()
liquid.registerTag('dynamic-string', {
render: async (ctx) => ctx.get('c')
})
const html = await liquid.parseAndRender(`A{% dynamic-string %}C`, {
c: 'B'
})
return expect(html).to.equal('ABC')
})
it('should have access to tag arguments', async () => {
const liquid = new Liquid()
liquid.registerTag('argument-reflector', {
parse: function (token) { this.variable = token.args.split('=')[1] },
render: async function (ctx) { return ctx.get(this.variable) }
})
const html = await liquid.parseAndRender(`A{% argument-reflector variable=c %}C`, {
c: 'B'
})
return expect(html).to.equal('ABC')
})
})
+16
View File
@@ -0,0 +1,16 @@
import { expect } from 'chai'
import { TagMap } from '../../../src/template/tag/tag-map'
describe('TagMap', function () {
it('should throw when not exist', function () {
const map = new TagMap()
expect(() => map.get('not-exist'))
.to.throw(/tag "not-exist" not found/)
})
it('should get previously set value', function () {
const map = new TagMap()
const impl = { render: () => 'foo' }
map.set('foo', impl)
expect(map.get('foo')).to.equal(impl)
})
})
+6 -35
View File
@@ -3,7 +3,6 @@ import { Tag } from '../../../src/template/tag/tag'
import { Context } from '../../../src/context/context'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import { Liquid } from '../../../src/liquid'
import { TagToken } from '../../../src/parser/tag-token'
import { toThenable } from '../../../src/util/async'
@@ -11,50 +10,22 @@ chai.use(sinonChai)
const expect = chai.expect
describe('Tag', function () {
let ctx: Context
let liquid: Liquid
const ctx = new Context()
const emitter: any = { write: (html: string) => (emitter.html += html), html: '' }
before(function () {
ctx = new Context({
foo: 'bar',
arr: [2, 1],
bar: {
coo: 'uoo'
}
})
liquid = new Liquid()
})
beforeEach(function () {
emitter.html = ''
})
it('should throw when not registered', function () {
expect(function () {
new Tag({ // eslint-disable-line
type: 'tag',
content: 'foo',
args: '',
name: 'not-exist'
} as TagToken, [], liquid)
}).to.throw(/tag "not-exist" not found/)
})
it('should register simple tag', function () {
expect(function () {
liquid.registerTag('foo', { render: () => 'bar' })
}).not.throw()
})
it('should call tag.render', async function () {
const spy = sinon.spy()
liquid.registerTag('foo', { render: spy })
const token = {
type: 'tag',
content: 'foo',
args: '',
name: 'foo'
} as TagToken
await toThenable(new Tag(token, [], liquid).render(ctx, emitter))
await toThenable(new Tag(token, [], {
tags: {
get: () => ({ render: spy })
}
} as any).render(ctx, emitter))
expect(spy).to.have.been.called
})
})