mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-26 13:45:15 -07:00
test: use inline Tag classes in register-tags spec
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -3,49 +3,35 @@ import { Tag } from '../../../src/template/tag'
|
|||||||
import type { Context } from '../../../src/context'
|
import type { Context } from '../../../src/context'
|
||||||
import type { TagToken, TopLevelToken } from '../../../src/tokens'
|
import type { TagToken, TopLevelToken } from '../../../src/tokens'
|
||||||
|
|
||||||
class SimpleStringTag extends Tag {
|
|
||||||
render () {
|
|
||||||
return 'B'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AsyncStringTag extends Tag {
|
|
||||||
async render () {
|
|
||||||
return 'B'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DynamicStringTag extends Tag {
|
|
||||||
async render (ctx: Context) {
|
|
||||||
return ctx.get(['c'])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ArgumentReflectorTag extends Tag {
|
|
||||||
variable: string
|
|
||||||
constructor (token: TagToken, remainTokens: TopLevelToken[], liquid: Liquid) {
|
|
||||||
super(token, remainTokens, liquid)
|
|
||||||
this.variable = token.args.split('=')[1]
|
|
||||||
}
|
|
||||||
async render (ctx: Context) {
|
|
||||||
return ctx.get([this.variable])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('liquid#registerTag()', function () {
|
describe('liquid#registerTag()', function () {
|
||||||
it('should support render to simple string', async () => {
|
it('should support render to simple string', async () => {
|
||||||
|
class SimpleStringTag extends Tag {
|
||||||
|
render () {
|
||||||
|
return 'B'
|
||||||
|
}
|
||||||
|
}
|
||||||
const liquid = new Liquid()
|
const liquid = new Liquid()
|
||||||
liquid.registerTag('simple-string', SimpleStringTag)
|
liquid.registerTag('simple-string', SimpleStringTag)
|
||||||
const html = await liquid.parseAndRender(`A{% simple-string %}C`)
|
const html = await liquid.parseAndRender(`A{% simple-string %}C`)
|
||||||
return expect(html).toBe('ABC')
|
return expect(html).toBe('ABC')
|
||||||
})
|
})
|
||||||
it('should support async tag render', async () => {
|
it('should support async tag render', async () => {
|
||||||
|
class AsyncStringTag extends Tag {
|
||||||
|
async render () {
|
||||||
|
return 'B'
|
||||||
|
}
|
||||||
|
}
|
||||||
const liquid = new Liquid()
|
const liquid = new Liquid()
|
||||||
liquid.registerTag('async-string', AsyncStringTag)
|
liquid.registerTag('async-string', AsyncStringTag)
|
||||||
const html = await liquid.parseAndRender(`A{% async-string %}C`)
|
const html = await liquid.parseAndRender(`A{% async-string %}C`)
|
||||||
return expect(html).toBe('ABC')
|
return expect(html).toBe('ABC')
|
||||||
})
|
})
|
||||||
it('should have access to ctx in render()', async () => {
|
it('should have access to ctx in render()', async () => {
|
||||||
|
class DynamicStringTag extends Tag {
|
||||||
|
async render (ctx: Context) {
|
||||||
|
return ctx.get(['c'])
|
||||||
|
}
|
||||||
|
}
|
||||||
const liquid = new Liquid()
|
const liquid = new Liquid()
|
||||||
liquid.registerTag('dynamic-string', DynamicStringTag)
|
liquid.registerTag('dynamic-string', DynamicStringTag)
|
||||||
const html = await liquid.parseAndRender(`A{% dynamic-string %}C`, {
|
const html = await liquid.parseAndRender(`A{% dynamic-string %}C`, {
|
||||||
@@ -54,6 +40,16 @@ describe('liquid#registerTag()', function () {
|
|||||||
return expect(html).toBe('ABC')
|
return expect(html).toBe('ABC')
|
||||||
})
|
})
|
||||||
it('should have access to tag arguments', async () => {
|
it('should have access to tag arguments', async () => {
|
||||||
|
class ArgumentReflectorTag extends Tag {
|
||||||
|
variable: string
|
||||||
|
constructor (token: TagToken, remainTokens: TopLevelToken[], liquid: Liquid) {
|
||||||
|
super(token, remainTokens, liquid)
|
||||||
|
this.variable = token.args.split('=')[1]
|
||||||
|
}
|
||||||
|
async render (ctx: Context) {
|
||||||
|
return ctx.get([this.variable])
|
||||||
|
}
|
||||||
|
}
|
||||||
const liquid = new Liquid()
|
const liquid = new Liquid()
|
||||||
liquid.registerTag('argument-reflector', ArgumentReflectorTag)
|
liquid.registerTag('argument-reflector', ArgumentReflectorTag)
|
||||||
const html = await liquid.parseAndRender(`A{% argument-reflector variable=c %}C`, {
|
const html = await liquid.parseAndRender(`A{% argument-reflector variable=c %}C`, {
|
||||||
|
|||||||
Reference in New Issue
Block a user