mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 05:10:40 -07:00
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
This commit is contained in:
@@ -3,36 +3,87 @@ import { Liquid } from '../../../src/liquid'
|
||||
import { mock, restore } from '../../stub/mockfs'
|
||||
|
||||
describe('LiquidOptions#cache', function () {
|
||||
let engine: Liquid
|
||||
beforeEach(function () {
|
||||
engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html'
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
})
|
||||
afterEach(restore)
|
||||
it('should be disabled by default', function () {
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('bar'))
|
||||
})
|
||||
it('should respect cache=true option', function () {
|
||||
engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
|
||||
describe('#renderFile', function () {
|
||||
it('should be disabled by default', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html'
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = await engine.renderFile('files/foo')
|
||||
expect(y).to.equal('bar')
|
||||
})
|
||||
it('should respect cache=true option', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = await engine.renderFile('files/foo')
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
it('should not cache not exist file', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
try { await engine.renderFile('foo') } catch (err) {}
|
||||
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
const y = await engine.renderFile('foo')
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#renderFileSync', function () {
|
||||
it('should be disabled by default', function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html'
|
||||
})
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
const x = engine.renderFileSync('foo')
|
||||
expect(x).to.equal('foo')
|
||||
|
||||
mock({ '/root/foo.html': 'bar' })
|
||||
const y = engine.renderFileSync('foo')
|
||||
expect(y).to.equal('bar')
|
||||
})
|
||||
it('should respect cache=true option', function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
const x = engine.renderFileSync('foo')
|
||||
expect(x).to.equal('foo')
|
||||
|
||||
mock({ '/root/foo.html': 'bar' })
|
||||
const y = engine.renderFileSync('foo')
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
it('should not cache not exist file', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
try { engine.renderFileSync('foo') } catch (err) {}
|
||||
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
const y = await engine.renderFile('foo')
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -53,14 +53,14 @@ describe('Liquid', function () {
|
||||
})
|
||||
after(restore)
|
||||
it('should render single template', function (done) {
|
||||
render.call({ root: '.' }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
render.call({ root: '/root' }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
if (err) return done(err)
|
||||
expect(result).to.equal('foo')
|
||||
done()
|
||||
})
|
||||
})
|
||||
it('should render single template with Array-typed root', function (done) {
|
||||
render.call({ root: ['.'] }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
render.call({ root: ['/root'] }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
if (err) return done(err)
|
||||
expect(result).to.equal('foo')
|
||||
done()
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
import { test, liquid } from '../../stub/render'
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('liquid#registerFilter()', function () {
|
||||
const liquid = new Liquid()
|
||||
|
||||
describe('object arguments', function () {
|
||||
liquid.registerFilter('obj_test', function () {
|
||||
return JSON.stringify(arguments)
|
||||
liquid.registerFilter('obj_test', function (...args) {
|
||||
return JSON.stringify(args)
|
||||
})
|
||||
it('should support object', async () => {
|
||||
const src = `{{ "a" | obj_test: k1: "v1", k2: foo }}`,
|
||||
const dst = '["a",["k1","v1"],["k2","bar"]]'
|
||||
const html = await liquid.parseAndRender(src, { foo: 'bar' })
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
it('should support mixed object', async () => {
|
||||
const src = `{{ "a" | obj_test: "something", k1: "v1", k2: foo }}`,
|
||||
const dst = '["a","something",["k1","v1"],["k2","bar"]]'
|
||||
const html = await liquid.parseAndRender(src, { foo: 'bar' })
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
it('should support object', () => test(
|
||||
`{{ "a" | obj_test: k1: "v1", k2: foo }}`,
|
||||
'{"0":"a","1":["k1","v1"],"2":["k2","bar"]}'
|
||||
))
|
||||
it('should support mixed object', () => test(
|
||||
`{{ "a" | obj_test: "something", k1: "v1", k2: foo }}`,
|
||||
'{"0":"a","1":"something","2":["k1","v1"],"3":["k2","bar"]}'
|
||||
))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user