Files
liquidjs/test/e2e/parse-and-render.ts
T

66 lines
2.4 KiB
TypeScript

import Liquid from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)
describe('.parseAndRender()', function () {
var engine, strictEngine
beforeEach(function () {
engine = new Liquid()
strictEngine = new Liquid({
strict_filters: true
})
})
it('should stringify object', async function () {
var ctx = { obj: { foo: 'bar' } }
const html = await engine.parseAndRender('{{obj}}', ctx)
return expect(html).to.equal('{"foo":"bar"}')
})
it('should stringify array ', async function () {
var ctx = { arr: [-2, 'a'] }
const html = await engine.parseAndRender('{{arr}}', ctx)
return expect(html).to.equal('[-2,"a"]')
})
it('should render undefined as empty', async function () {
const html = await engine.parseAndRender('foo{{zzz}}bar', {})
return expect(html).to.equal('foobar')
})
it('should render as null when filter undefined', async function () {
const html = await engine.parseAndRender('{{"foo" | filter1}}', {})
return expect(html).to.equal('foo')
})
it('should throw upon undefined filter when strict_filters set', function () {
return expect(strictEngine.parseAndRender('{{"foo" | filter1}}', {})).to
.be.rejectedWith(/undefined filter: filter1/)
})
it('should parse html', function () {
expect(function () {
engine.parse('{{obj}}')
}).to.not.throw()
expect(function () {
engine.parse('<html><head>{{obj}}</head></html>')
}).to.not.throw()
})
it('should render template multiple times', async function () {
const ctx = { obj: { foo: 'bar' } }
const template = engine.parse('{{obj}}')
const result = await engine.render(template, ctx)
expect(result).to.equal('{"foo":"bar"}')
const result2 = await engine.render(template, ctx)
expect(result2).to.equal('{"foo":"bar"}')
})
it('should render filters', async function () {
var ctx = { names: ['alice', 'bob'] }
var template = engine.parse('<p>{{names | join: ","}}</p>')
const html = await engine.render(template, ctx)
return expect(html).to.equal('<p>alice,bob</p>')
})
it('should render accessive filters', async function () {
var src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
'{{ my_array | first }}'
const html = await engine.parseAndRender(src)
return expect(html).to.equal('apples')
})
})