diff --git a/test/integration/liquid/register-tags.ts b/test/integration/liquid/register-tags.ts new file mode 100644 index 000000000..1b73e7a5d --- /dev/null +++ b/test/integration/liquid/register-tags.ts @@ -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') + }) +}) diff --git a/test/unit/template/tag-map.ts b/test/unit/template/tag-map.ts new file mode 100644 index 000000000..d2b631bac --- /dev/null +++ b/test/unit/template/tag-map.ts @@ -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) + }) +}) diff --git a/test/unit/template/tag.ts b/test/unit/template/tag.ts index 2f31835dc..f3104644a 100644 --- a/test/unit/template/tag.ts +++ b/test/unit/template/tag.ts @@ -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 }) })