mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
chore: migrate test cases from Chai to Jest
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid, Template } from '../../../src/liquid'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
import { mock, restore } from '../../stub/mockfs'
|
||||
import { Template } from '../../../src/template'
|
||||
|
||||
describe('LiquidOptions#cache', function () {
|
||||
afterEach(restore)
|
||||
@@ -13,10 +13,10 @@ describe('LiquidOptions#cache', function () {
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
expect(x).toBe('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = await engine.renderFile('files/foo')
|
||||
expect(y).to.equal('bar')
|
||||
expect(y).toBe('bar')
|
||||
})
|
||||
it('should be disabled when cache <= 0', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -26,10 +26,10 @@ describe('LiquidOptions#cache', function () {
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
expect(x).toBe('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = await engine.renderFile('files/foo')
|
||||
expect(y).to.equal('bar')
|
||||
expect(y).toBe('bar')
|
||||
})
|
||||
it('should respect cache=true option', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -39,10 +39,10 @@ describe('LiquidOptions#cache', function () {
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
expect(x).toBe('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = await engine.renderFile('files/foo')
|
||||
expect(y).to.equal('foo')
|
||||
expect(y).toBe('foo')
|
||||
})
|
||||
it('should respect cache=2 option', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -57,12 +57,12 @@ describe('LiquidOptions#cache', function () {
|
||||
mock({ '/root/files/foo.html': 'FOO' })
|
||||
await engine.renderFile('files/bar')
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
expect(x).toBe('foo')
|
||||
|
||||
await engine.renderFile('files/bar')
|
||||
await engine.renderFile('files/coo')
|
||||
const y = await engine.renderFile('files/foo')
|
||||
expect(y).to.equal('FOO')
|
||||
expect(y).toBe('FOO')
|
||||
})
|
||||
it('should respect cache={read, write} option', async function () {
|
||||
let last: Template[] | undefined
|
||||
@@ -70,6 +70,7 @@ describe('LiquidOptions#cache', function () {
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: {
|
||||
remove: () => void (0),
|
||||
read: (): Template[] | undefined => last,
|
||||
write: (key: string, value: Template[]) => { last = value }
|
||||
}
|
||||
@@ -77,9 +78,9 @@ describe('LiquidOptions#cache', function () {
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
mock({ '/root/files/bar.html': 'bar' })
|
||||
mock({ '/root/files/coo.html': 'coo' })
|
||||
expect(await engine.renderFile('files/foo')).to.equal('foo')
|
||||
expect(await engine.renderFile('files/bar')).to.equal('foo')
|
||||
expect(await engine.renderFile('files/coo')).to.equal('foo')
|
||||
expect(await engine.renderFile('files/foo')).toBe('foo')
|
||||
expect(await engine.renderFile('files/bar')).toBe('foo')
|
||||
expect(await engine.renderFile('files/coo')).toBe('foo')
|
||||
})
|
||||
it('should respect cache={ async read, async write } option', async function () {
|
||||
const cached: { [key: string]: Template[] | undefined } = {}
|
||||
@@ -87,6 +88,7 @@ describe('LiquidOptions#cache', function () {
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: {
|
||||
remove: (key: string) => { delete cached[key] },
|
||||
read: (key: string) => Promise.resolve(cached[key]),
|
||||
write: (key: string, value: Template[]) => { cached[key] = value; Promise.resolve() }
|
||||
}
|
||||
@@ -94,11 +96,11 @@ describe('LiquidOptions#cache', function () {
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
mock({ '/root/files/bar.html': 'bar' })
|
||||
mock({ '/root/files/coo.html': 'coo' })
|
||||
expect(await engine.renderFile('files/foo')).to.equal('foo')
|
||||
expect(await engine.renderFile('files/bar')).to.equal('bar')
|
||||
expect(await engine.renderFile('files/coo')).to.equal('coo')
|
||||
expect(await engine.renderFile('files/foo')).toBe('foo')
|
||||
expect(await engine.renderFile('files/bar')).toBe('bar')
|
||||
expect(await engine.renderFile('files/coo')).toBe('coo')
|
||||
mock({ '/root/files/coo.html': 'COO' })
|
||||
expect(await engine.renderFile('files/coo')).to.equal('coo')
|
||||
expect(await engine.renderFile('files/coo')).toBe('coo')
|
||||
})
|
||||
it('should handle concurrent cache read/write', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -115,10 +117,10 @@ describe('LiquidOptions#cache', function () {
|
||||
engine.renderFile('files/bar'),
|
||||
engine.renderFile('files/coo')
|
||||
])
|
||||
expect(foo1).to.equal('foo')
|
||||
expect(foo2).to.equal('foo')
|
||||
expect(bar).to.equal('bar')
|
||||
expect(coo).to.equal('coo')
|
||||
expect(foo1).toBe('foo')
|
||||
expect(foo2).toBe('foo')
|
||||
expect(bar).toBe('bar')
|
||||
expect(coo).toBe('coo')
|
||||
})
|
||||
it('should not cache not exist file', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -132,7 +134,7 @@ describe('LiquidOptions#cache', function () {
|
||||
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
const html = await engine.renderFile('foo')
|
||||
expect(html).to.equal('foo')
|
||||
expect(html).toBe('foo')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -144,11 +146,11 @@ describe('LiquidOptions#cache', function () {
|
||||
})
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
const x = engine.renderFileSync('foo')
|
||||
expect(x).to.equal('foo')
|
||||
expect(x).toBe('foo')
|
||||
|
||||
mock({ '/root/foo.html': 'bar' })
|
||||
const y = engine.renderFileSync('foo')
|
||||
expect(y).to.equal('bar')
|
||||
expect(y).toBe('bar')
|
||||
})
|
||||
it('should respect cache=true option', function () {
|
||||
const engine = new Liquid({
|
||||
@@ -157,9 +159,9 @@ describe('LiquidOptions#cache', function () {
|
||||
cache: true
|
||||
})
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
expect(engine.renderFileSync('foo')).to.equal('foo')
|
||||
expect(engine.renderFileSync('foo')).toBe('foo')
|
||||
mock({ '/root/foo.html': 'bar' })
|
||||
expect(engine.renderFileSync('foo')).to.equal('foo')
|
||||
expect(engine.renderFileSync('foo')).toBe('foo')
|
||||
})
|
||||
it('should not cache not exist file', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -171,7 +173,7 @@ describe('LiquidOptions#cache', function () {
|
||||
|
||||
mock({ '/root/foo.html': 'foo' })
|
||||
const y = await engine.renderFile('foo')
|
||||
expect(y).to.equal('foo')
|
||||
expect(y).toBe('foo')
|
||||
})
|
||||
it('should cache relative referenced files properly', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -186,10 +188,10 @@ describe('LiquidOptions#cache', function () {
|
||||
'/root/another/bar.html': 'bar2'
|
||||
})
|
||||
const foo1 = await engine.renderFile('foo')
|
||||
expect(foo1).to.equal('bar1')
|
||||
expect(foo1).toBe('bar1')
|
||||
|
||||
const foo2 = await engine.renderFile('another/foo')
|
||||
expect(foo2).to.equal('bar2')
|
||||
expect(foo2).toBe('bar2')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('LiquidOptions#*_delimiter_*', function () {
|
||||
@@ -8,7 +7,7 @@ describe('LiquidOptions#*_delimiter_*', function () {
|
||||
tagDelimiterRight: '%>'
|
||||
})
|
||||
const html = await engine.parseAndRender('<%=if true%>foo<%=endif%> ')
|
||||
return expect(html).to.equal('foo ')
|
||||
return expect(html).toBe('foo ')
|
||||
})
|
||||
it('should respect output_delimiter_*', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -16,7 +15,7 @@ describe('LiquidOptions#*_delimiter_*', function () {
|
||||
outputDelimiterRight: '>>'
|
||||
})
|
||||
const html = await engine.parseAndRender('<< "liquid" | capitalize >>')
|
||||
return expect(html).to.equal('Liquid')
|
||||
return expect(html).toBe('Liquid')
|
||||
})
|
||||
it('should support trimming with tag_delimiter_* set', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -26,6 +25,6 @@ describe('LiquidOptions#*_delimiter_*', function () {
|
||||
trimTagRight: true
|
||||
})
|
||||
const html = await engine.parseAndRender(' <%=if true%> \tfoo\t <%=endif%> ')
|
||||
return expect(html).to.equal('foo')
|
||||
return expect(html).toBe('foo')
|
||||
})
|
||||
})
|
||||
@@ -1,11 +1,10 @@
|
||||
import { expect, use } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('LiquidOptions#fs', function () {
|
||||
let engine: Liquid
|
||||
const fs = {
|
||||
sep: '/',
|
||||
dirname: (x: string) => x.split('/').slice(0, -1).join('/'),
|
||||
exists: (x: string) => Promise.resolve(!x.match(/not-exist/)),
|
||||
existsSync: (x: string) => !x.match(/not-exist/),
|
||||
readFile: (x: string) => Promise.resolve(`content for ${x}`),
|
||||
@@ -21,17 +20,17 @@ describe('LiquidOptions#fs', function () {
|
||||
})
|
||||
it('should be used to read templates', async function () {
|
||||
const html = await engine.renderFile('files/foo')
|
||||
expect(html).to.equal('content for /root/files/foo')
|
||||
expect(html).toBe('content for /root/files/foo')
|
||||
})
|
||||
|
||||
it('should support fallback', async function () {
|
||||
const html = await engine.renderFile('not-exist/foo')
|
||||
expect(html).to.equal('content for /root/files/fallback')
|
||||
expect(html).toBe('content for /root/files/fallback')
|
||||
})
|
||||
|
||||
it('should support renderSync', function () {
|
||||
const html = engine.renderFileSync('not-exist/foo')
|
||||
expect(html).to.equal('content for /root/files/fallback')
|
||||
expect(html).toBe('content for /root/files/fallback')
|
||||
})
|
||||
|
||||
it('should throw lookup failure if fallback not specified', function () {
|
||||
@@ -40,6 +39,14 @@ describe('LiquidOptions#fs', function () {
|
||||
fs: { ...fs, fallback: undefined }
|
||||
} as any)
|
||||
return expect(engine.renderFile('not-exist/foo'))
|
||||
.to.be.rejectedWith('Failed to lookup')
|
||||
.rejects.toThrow('Failed to lookup')
|
||||
})
|
||||
|
||||
it('should disable relativeReference if `sep` and `dirname` not specified', function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
fs: { ...fs, sep: undefined, dirname: undefined }
|
||||
} as any)
|
||||
expect(engine.options.relativeReference).toBe(false)
|
||||
})
|
||||
})
|
||||
+8
-9
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('LiquidOptions#*keepOutputType*', function () {
|
||||
@@ -12,13 +11,13 @@ describe('LiquidOptions#*keepOutputType*', function () {
|
||||
'my-string': 'test'
|
||||
}
|
||||
const booleanHtml = await engine.parseAndRender('{{my-boolean}}', context)
|
||||
expect(booleanHtml).to.equal(true)
|
||||
expect(booleanHtml).toBe(true)
|
||||
const numberHtml = await engine.parseAndRender('{{my-number}}', context)
|
||||
expect(numberHtml).to.equal(42)
|
||||
expect(numberHtml).toBe(42)
|
||||
const html = await engine.parseAndRender('{{my-string}}', context)
|
||||
expect(html).to.equal('test')
|
||||
expect(html).toBe('test')
|
||||
const composedHtml = await engine.parseAndRender('{{my-string}}:{{my-number}}', context)
|
||||
expect(composedHtml).to.equal('test:42')
|
||||
expect(composedHtml).toBe('test:42')
|
||||
})
|
||||
|
||||
it('should respect keepOutputType = false as default', async function () {
|
||||
@@ -29,12 +28,12 @@ describe('LiquidOptions#*keepOutputType*', function () {
|
||||
'my-string': 'test'
|
||||
}
|
||||
const booleanHtml = await engine.parseAndRender('{{my-boolean}}', context)
|
||||
expect(booleanHtml).to.equal('true')
|
||||
expect(booleanHtml).toBe('true')
|
||||
const numberHtml = await engine.parseAndRender('{{my-number}}', context)
|
||||
expect(numberHtml).to.equal('42')
|
||||
expect(numberHtml).toBe('42')
|
||||
const html = await engine.parseAndRender('{{my-string}}', context)
|
||||
expect(html).to.equal('test')
|
||||
expect(html).toBe('test')
|
||||
const composedHtml = await engine.parseAndRender('{{my-string}}:{{my-number}}', context)
|
||||
expect(composedHtml).to.equal('test:42')
|
||||
expect(composedHtml).toBe('test:42')
|
||||
})
|
||||
})
|
||||
@@ -1,12 +1,7 @@
|
||||
import * as chai from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
import { Liquid, Context, isFalsy } from '../../../src'
|
||||
import { mock, restore } from '../../stub/mockfs'
|
||||
import { drainStream } from '../../stub/stream'
|
||||
|
||||
const expect = chai.expect
|
||||
chai.use(chaiAsPromised)
|
||||
|
||||
describe('Liquid', function () {
|
||||
describe('#plugin()', function () {
|
||||
it('should call plugin on the instance', async function () {
|
||||
@@ -15,7 +10,7 @@ describe('Liquid', function () {
|
||||
this.registerFilter('foo', x => `foo${x}foo`)
|
||||
})
|
||||
const html = await engine.parseAndRender('{{"bar"|foo}}')
|
||||
expect(html).to.equal('foobarfoo')
|
||||
expect(html).toBe('foobarfoo')
|
||||
})
|
||||
it('should call plugin with Liquid', async function () {
|
||||
const engine = new Liquid()
|
||||
@@ -23,92 +18,92 @@ describe('Liquid', function () {
|
||||
this.registerFilter('t', function (v) { return isFalsy(v, this.context) })
|
||||
})
|
||||
const html = await engine.parseAndRender('{{false|t}}')
|
||||
expect(html).to.equal('true')
|
||||
expect(html).toBe('true')
|
||||
})
|
||||
})
|
||||
describe('#parseAndRender', function () {
|
||||
const engine = new Liquid()
|
||||
it('should parse and render variable output', async function () {
|
||||
const html = await engine.parseAndRender('{{"foo"}}')
|
||||
expect(html).to.equal('foo')
|
||||
expect(html).toBe('foo')
|
||||
})
|
||||
it('should parse and render complex output', async function () {
|
||||
const tpl = '{{ "Welcome|to]Liquid" | split: "|" | join: "("}}'
|
||||
const html = await engine.parseAndRender(tpl)
|
||||
expect(html).to.equal('Welcome(to]Liquid')
|
||||
expect(html).toBe('Welcome(to]Liquid')
|
||||
})
|
||||
it('should support for-in with variable', async function () {
|
||||
const src = '{% assign total = 3 | minus: 1 %}' +
|
||||
'{% for i in (1..total) %}{{ i }}{% endfor %}'
|
||||
const html = await engine.parseAndRender(src, {})
|
||||
return expect(html).to.equal('12')
|
||||
return expect(html).toBe('12')
|
||||
})
|
||||
it('should support `globals` render option', async function () {
|
||||
const src = '{{ foo }}'
|
||||
const html = await engine.parseAndRender(src, {}, { globals: { foo: 'FOO' } })
|
||||
return expect(html).to.equal('FOO')
|
||||
return expect(html).toBe('FOO')
|
||||
})
|
||||
it('should support `strictVariables` render option', function () {
|
||||
const src = '{{ foo }}'
|
||||
return expect(engine.parseAndRender(src, {}, { strictVariables: true })).rejectedWith(/undefined variable/)
|
||||
return expect(engine.parseAndRender(src, {}, { strictVariables: true })).rejects.toThrow(/undefined variable/)
|
||||
})
|
||||
it('should support async variables in output', async () => {
|
||||
const src = '{{ foo }}'
|
||||
const html = await engine.parseAndRender(src, { foo: Promise.resolve('FOO') })
|
||||
expect(html).to.equal('FOO')
|
||||
expect(html).toBe('FOO')
|
||||
})
|
||||
it('should parse and render with Context', async function () {
|
||||
const html = await engine.parseAndRender('{{foo}}', new Context({ foo: 'FOO' }))
|
||||
expect(html).to.equal('FOO')
|
||||
expect(html).toBe('FOO')
|
||||
})
|
||||
})
|
||||
describe('#parseAndRenderSync', function () {
|
||||
const engine = new Liquid()
|
||||
it('should parse and render variable output', function () {
|
||||
const html = engine.parseAndRenderSync('{{"foo"}}')
|
||||
expect(html).to.equal('foo')
|
||||
expect(html).toBe('foo')
|
||||
})
|
||||
it('should parse and render complex output', function () {
|
||||
const tpl = '{{ "Welcome|to]Liquid" | split: "|" | join: "("}}'
|
||||
const html = engine.parseAndRenderSync(tpl)
|
||||
expect(html).to.equal('Welcome(to]Liquid')
|
||||
expect(html).toBe('Welcome(to]Liquid')
|
||||
})
|
||||
it('should support for-in with variable', function () {
|
||||
const src = '{% assign total = 3 | minus: 1 %}' +
|
||||
'{% for i in (1..total) %}{{ i }}{% endfor %}'
|
||||
const html = engine.parseAndRenderSync(src, {})
|
||||
return expect(html).to.equal('12')
|
||||
return expect(html).toBe('12')
|
||||
})
|
||||
it('should support `globals` render option', function () {
|
||||
const src = '{{ foo }}'
|
||||
const html = engine.parseAndRenderSync(src, {}, { globals: { foo: 'FOO' } })
|
||||
return expect(html).to.equal('FOO')
|
||||
return expect(html).toBe('FOO')
|
||||
})
|
||||
it('should support `strictVariables` render option', function () {
|
||||
const src = '{{ foo }}'
|
||||
return expect(() => engine.parseAndRenderSync(src, {}, { strictVariables: true })).throw(/undefined variable/)
|
||||
return expect(() => engine.parseAndRenderSync(src, {}, { strictVariables: true })).toThrow(/undefined variable/)
|
||||
})
|
||||
})
|
||||
describe('#express()', function () {
|
||||
const liquid = new Liquid({ root: '/root' })
|
||||
const render = liquid.express()
|
||||
before(function () {
|
||||
beforeEach(function () {
|
||||
mock({
|
||||
'/root/foo': 'foo'
|
||||
})
|
||||
})
|
||||
after(restore)
|
||||
afterEach(restore)
|
||||
it('should render single template', function (done) {
|
||||
render.call({ root: '/root' }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
if (err) return done(err)
|
||||
expect(result).to.equal('foo')
|
||||
expect(result).toBe('foo')
|
||||
done()
|
||||
})
|
||||
})
|
||||
it('should render single template with Array-typed root', function (done) {
|
||||
render.call({ root: ['/root'] }, 'foo', null as any, (err: Error | null, result: string | undefined) => {
|
||||
if (err) return done(err)
|
||||
expect(result).to.equal('foo')
|
||||
expect(result).toBe('foo')
|
||||
done()
|
||||
})
|
||||
})
|
||||
@@ -119,8 +114,7 @@ describe('Liquid', function () {
|
||||
root: ['/boo', '/root/'],
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(engine.renderFile('/not/exist.html')).to
|
||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
return expect(engine.renderFile('/not/exist.html')).rejects.toThrow(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
})
|
||||
describe('#parseFile', function () {
|
||||
@@ -129,17 +123,16 @@ describe('Liquid', function () {
|
||||
root: ['/boo', '/root/'],
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(engine.parseFile('/not/exist.html')).to
|
||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
return expect(engine.parseFile('/not/exist.html')).rejects.toThrow(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
it('should fallback to require.resolve in Node.js', async function () {
|
||||
const engine = new Liquid({
|
||||
root: ['/root/'],
|
||||
extname: '.html'
|
||||
})
|
||||
const tpls = await engine.parseFileSync('mocha')
|
||||
expect(tpls.length).to.gte(1)
|
||||
expect(tpls[0].token.getText()).to.contain('module.exports')
|
||||
const tpls = await engine.parseFileSync('jest')
|
||||
expect(tpls.length).toBeGreaterThanOrEqual(1)
|
||||
expect(tpls[0].token.getText()).toContain('use strict')
|
||||
})
|
||||
})
|
||||
describe('#evalValue', function () {
|
||||
@@ -147,12 +140,12 @@ describe('Liquid', function () {
|
||||
const engine = new Liquid()
|
||||
const ctx = new Context()
|
||||
const str = await engine.evalValue('"foo"', ctx)
|
||||
expect(str).to.equal('foo')
|
||||
expect(str).toBe('foo')
|
||||
})
|
||||
it('should support plain scope', async function () {
|
||||
const engine = new Liquid()
|
||||
const str = await engine.evalValue('foo', { foo: 'FOO' })
|
||||
expect(str).to.equal('FOO')
|
||||
expect(str).toBe('FOO')
|
||||
})
|
||||
})
|
||||
describe('#evalValueSync', function () {
|
||||
@@ -160,7 +153,7 @@ describe('Liquid', function () {
|
||||
const engine = new Liquid()
|
||||
const ctx = new Context()
|
||||
const str = engine.evalValueSync('"foo"', ctx)
|
||||
expect(str).to.equal('foo')
|
||||
expect(str).toBe('foo')
|
||||
})
|
||||
})
|
||||
describe('#parse', function () {
|
||||
@@ -173,7 +166,7 @@ describe('Liquid', function () {
|
||||
'/root/partial.html': 'foo'
|
||||
})
|
||||
const tpls = engine.parse('{% render "./partial.html" %}', '/root/index.html')
|
||||
return expect(engine.renderSync(tpls)).to.equal('foo')
|
||||
return expect(engine.renderSync(tpls)).toBe('foo')
|
||||
})
|
||||
it('should resolve against pwd for relative filepath', function () {
|
||||
const engine = new Liquid({
|
||||
@@ -184,7 +177,7 @@ describe('Liquid', function () {
|
||||
[`${process.cwd()}/partial.html`]: 'foo'
|
||||
})
|
||||
const tpls = engine.parse('{% render "./partial.html" %}', './index.html')
|
||||
return expect(engine.renderSync(tpls)).to.equal('foo')
|
||||
return expect(engine.renderSync(tpls)).toBe('foo')
|
||||
})
|
||||
})
|
||||
describe('#parseFileSync', function () {
|
||||
@@ -194,7 +187,7 @@ describe('Liquid', function () {
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(() => engine.parseFileSync('/not/exist.html'))
|
||||
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
.toThrow(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
it('should throw with lookup list when file not exist', function () {
|
||||
const engine = new Liquid({
|
||||
@@ -202,19 +195,19 @@ describe('Liquid', function () {
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(() => engine.parseFileSync('/not/exist.html'))
|
||||
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
.toThrow(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
})
|
||||
describe('#enderToNodeStream', function () {
|
||||
const engine = new Liquid()
|
||||
it('should render a simple value', async () => {
|
||||
const stream = engine.renderToNodeStream(engine.parse('{{"foo"}}'))
|
||||
expect(drainStream(stream)).to.eventually.equal('foo')
|
||||
expect(drainStream(stream)).resolves.toBe('foo')
|
||||
})
|
||||
})
|
||||
describe('#enderFileToNodeStream', function () {
|
||||
let engine: Liquid
|
||||
before(function () {
|
||||
beforeEach(function () {
|
||||
mock({
|
||||
'/root/foo.html': 'foo',
|
||||
'/root/error.html': 'A{%throwingTag%}B'
|
||||
@@ -226,14 +219,14 @@ describe('Liquid', function () {
|
||||
}
|
||||
})
|
||||
})
|
||||
after(restore)
|
||||
afterEach(restore)
|
||||
it('should render a simple value', async () => {
|
||||
const stream = await engine.renderFileToNodeStream('foo.html')
|
||||
expect(drainStream(stream)).to.be.eventually.equal('foo')
|
||||
expect(drainStream(stream)).resolves.toBe('foo')
|
||||
})
|
||||
it('should throw RenderError when tag throws', async () => {
|
||||
const stream = await engine.renderFileToNodeStream('error.html')
|
||||
expect(drainStream(stream)).to.be.rejectedWith(/intended render error/)
|
||||
expect(drainStream(stream)).rejects.toThrow(/intended render error/)
|
||||
})
|
||||
})
|
||||
})
|
||||
+6
-7
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid, defaultOperators } from '../../../src'
|
||||
|
||||
describe('LiquidOptions#operators', function () {
|
||||
@@ -8,27 +7,27 @@ describe('LiquidOptions#operators', function () {
|
||||
engine = new Liquid({
|
||||
operators: {
|
||||
...defaultOperators,
|
||||
isFooBar: (l, r) => l === 'foo' && r === 'bar'
|
||||
isFooBar: (l: string, r: string) => l === 'foo' && r === 'bar'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('should evaluate the default operators', async function () {
|
||||
const result = await engine.parseAndRender('{% if "foo" == "foo" %}True{% endif %}')
|
||||
expect(result).to.equal('True')
|
||||
expect(result).toBe('True')
|
||||
})
|
||||
|
||||
it('should evaluate a custom operator', async function () {
|
||||
const first = await engine.parseAndRender('{% if "foo" isFooBar "bar" %}True{% else %}False{% endif %}')
|
||||
expect(first).to.equal('True')
|
||||
expect(first).toBe('True')
|
||||
const second = await engine.parseAndRender('{% if "foo" isFooBar "foo" %}True{% else %}False{% endif %}')
|
||||
expect(second).to.equal('False')
|
||||
expect(second).toBe('False')
|
||||
})
|
||||
|
||||
it('should evaluate a custom operator with the correct precedence', async function () {
|
||||
const first = await engine.parseAndRender('{% if "foo" isFooBar "bar" or "foo" == "bar" %}True{% else %}False{% endif %}')
|
||||
expect(first).to.equal('True')
|
||||
expect(first).toBe('True')
|
||||
const second = await engine.parseAndRender('{% if "foo" isFooBar "foo" or "foo" == "bar" %}True{% else %}False{% endif %}')
|
||||
expect(second).to.equal('False')
|
||||
expect(second).toBe('False')
|
||||
})
|
||||
})
|
||||
+5
-6
@@ -1,11 +1,10 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('LiquidOptions#*outputEscape*', function () {
|
||||
it('when outputEscape is not set', async function () {
|
||||
const engine = new Liquid()
|
||||
const html = await engine.parseAndRender('{{"<"}}')
|
||||
expect(html).to.equal('<')
|
||||
expect(html).toBe('<')
|
||||
})
|
||||
|
||||
it('should escape when outputEscape="escape"', async function () {
|
||||
@@ -13,7 +12,7 @@ describe('LiquidOptions#*outputEscape*', function () {
|
||||
outputEscape: 'escape'
|
||||
})
|
||||
const html = await engine.parseAndRender('{{"<"}}')
|
||||
expect(html).to.equal('<')
|
||||
expect(html).toBe('<')
|
||||
})
|
||||
|
||||
it('should json stringify when outputEscape="json"', async function () {
|
||||
@@ -21,7 +20,7 @@ describe('LiquidOptions#*outputEscape*', function () {
|
||||
outputEscape: 'json'
|
||||
})
|
||||
const html = await engine.parseAndRender('{{"<"}}')
|
||||
expect(html).to.equal('"<"')
|
||||
expect(html).toBe('"<"')
|
||||
})
|
||||
|
||||
it('should support outputEscape=Function', async function () {
|
||||
@@ -29,7 +28,7 @@ describe('LiquidOptions#*outputEscape*', function () {
|
||||
outputEscape: (v: any) => `{${v}}`
|
||||
})
|
||||
const html = await engine.parseAndRender('{{"<"}}')
|
||||
expect(html).to.equal('{<}')
|
||||
expect(html).toBe('{<}')
|
||||
})
|
||||
|
||||
it('should skip escape for output with filter "| raw"', async function () {
|
||||
@@ -37,6 +36,6 @@ describe('LiquidOptions#*outputEscape*', function () {
|
||||
outputEscape: 'escape'
|
||||
})
|
||||
const html = await engine.parseAndRender('{{"<" | raw}}')
|
||||
expect(html).to.equal('<')
|
||||
expect(html).toBe('<')
|
||||
})
|
||||
})
|
||||
+5
-6
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('liquid#registerFilter()', function () {
|
||||
@@ -15,13 +14,13 @@ describe('liquid#registerFilter()', function () {
|
||||
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)
|
||||
return expect(html).toBe(dst)
|
||||
})
|
||||
it('should support mixed arguments', 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)
|
||||
return expect(html).toBe(dst)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -33,7 +32,7 @@ describe('liquid#registerFilter()', function () {
|
||||
const src = `{{ userId | get_user_data | json }}`
|
||||
const dst = '{"userId":"alice","userName":"ALICE"}'
|
||||
const html = await liquid.parseAndRender(src, { userId: 'alice' })
|
||||
return expect(html).to.equal(dst)
|
||||
return expect(html).toBe(dst)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -48,7 +47,7 @@ describe('liquid#registerFilter()', function () {
|
||||
const src = `{{ "a\nb" | break }}`
|
||||
const dst = 'a<br/>b'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
return expect(html).toBe(dst)
|
||||
})
|
||||
it('should not escape filter output when registered as "raw"', async () => {
|
||||
liquid.registerFilter('break', {
|
||||
@@ -58,7 +57,7 @@ describe('liquid#registerFilter()', function () {
|
||||
const src = `{{ "a\nb" | break }}`
|
||||
const dst = 'a<br/>b'
|
||||
const html = await liquid.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
return expect(html).toBe(dst)
|
||||
})
|
||||
})
|
||||
})
|
||||
+4
-5
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('liquid#registerTag()', function () {
|
||||
@@ -8,7 +7,7 @@ describe('liquid#registerTag()', function () {
|
||||
render: () => 'B'
|
||||
})
|
||||
const html = await liquid.parseAndRender(`A{% simple-string %}C`)
|
||||
return expect(html).to.equal('ABC')
|
||||
return expect(html).toBe('ABC')
|
||||
})
|
||||
it('should support async tag render', async () => {
|
||||
const liquid = new Liquid()
|
||||
@@ -16,7 +15,7 @@ describe('liquid#registerTag()', function () {
|
||||
render: async () => 'B'
|
||||
})
|
||||
const html = await liquid.parseAndRender(`A{% async-string %}C`)
|
||||
return expect(html).to.equal('ABC')
|
||||
return expect(html).toBe('ABC')
|
||||
})
|
||||
it('should have access to ctx in render()', async () => {
|
||||
const liquid = new Liquid()
|
||||
@@ -26,7 +25,7 @@ describe('liquid#registerTag()', function () {
|
||||
const html = await liquid.parseAndRender(`A{% dynamic-string %}C`, {
|
||||
c: 'B'
|
||||
})
|
||||
return expect(html).to.equal('ABC')
|
||||
return expect(html).toBe('ABC')
|
||||
})
|
||||
it('should have access to tag arguments', async () => {
|
||||
const liquid = new Liquid()
|
||||
@@ -37,6 +36,6 @@ describe('liquid#registerTag()', function () {
|
||||
const html = await liquid.parseAndRender(`A{% argument-reflector variable=c %}C`, {
|
||||
c: 'B'
|
||||
})
|
||||
return expect(html).to.equal('ABC')
|
||||
return expect(html).toBe('ABC')
|
||||
})
|
||||
})
|
||||
@@ -1,15 +1,14 @@
|
||||
import { normalize } from '../../../src/liquid-options'
|
||||
import { expect } from 'chai'
|
||||
|
||||
describe('LiquidOptions#root', function () {
|
||||
describe('#normalize ()', function () {
|
||||
it('should normalize string typed root array', function () {
|
||||
const options = normalize({ root: 'foo' })
|
||||
expect(options.root).to.eql(['foo'])
|
||||
expect(options.root).toEqual(['foo'])
|
||||
})
|
||||
it('should normalize null typed root as empty array', function () {
|
||||
const options = normalize({ root: null } as any)
|
||||
expect(options.root).to.eql([])
|
||||
expect(options.root).toEqual([])
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,9 +1,4 @@
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
import * as chai from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
chai.use(chaiAsPromised)
|
||||
const expect = chai.expect
|
||||
|
||||
describe('LiquidOptions#strict*', function () {
|
||||
let engine: Liquid
|
||||
@@ -16,7 +11,7 @@ describe('LiquidOptions#strict*', function () {
|
||||
})
|
||||
it('should not throw when strictVariables false (default)', async function () {
|
||||
const html = await engine.parseAndRender('before{{notdefined}}after', ctx)
|
||||
return expect(html).to.equal('beforeafter')
|
||||
return expect(html).toBe('beforeafter')
|
||||
})
|
||||
it('should throw when strictVariables true', function () {
|
||||
const tpl = engine.parse('before{{notdefined}}after')
|
||||
@@ -25,8 +20,7 @@ describe('LiquidOptions#strict*', function () {
|
||||
extname: '.html',
|
||||
strictVariables: true
|
||||
})
|
||||
return expect(engine.render(tpl, ctx)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
return expect(engine.render(tpl, ctx)).rejects.toThrow(/undefined variable: notdefined/)
|
||||
})
|
||||
it('should pass strictVariables to render by parseAndRender', function () {
|
||||
const html = 'before{{notdefined}}after'
|
||||
@@ -35,8 +29,7 @@ describe('LiquidOptions#strict*', function () {
|
||||
extname: '.html',
|
||||
strictVariables: true
|
||||
})
|
||||
return expect(engine.parseAndRender(html, ctx)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
return expect(engine.parseAndRender(html, ctx)).rejects.toThrow(/undefined variable: notdefined/)
|
||||
})
|
||||
describe('with strictVariables and lenientIf', function () {
|
||||
beforeEach(() => {
|
||||
@@ -50,31 +43,31 @@ describe('LiquidOptions#strict*', function () {
|
||||
it('should not throw in `if` with a single variable', async function () {
|
||||
const tpl = engine.parse('before{% if notdefined %}{{notdefined}}{% endif %}after')
|
||||
const html = await engine.render(tpl, ctx)
|
||||
return expect(html).to.equal('beforeafter')
|
||||
return expect(html).toBe('beforeafter')
|
||||
})
|
||||
it('should support elsif with undefined variables', async function () {
|
||||
const tpl = engine.parse('{% if notdefined1 %}a{% elsif notdefined2 %}b{% elsif defined3 %}{{defined3}}{% else %}d{% endif %}')
|
||||
const html = await engine.render(tpl, { 'defined3': 'bla' })
|
||||
return expect(html).to.equal('bla')
|
||||
return expect(html).toBe('bla')
|
||||
})
|
||||
it('should not throw in `unless` with a single variable', async function () {
|
||||
const tpl = engine.parse('before{% unless notdefined %}X{% else %}{{notdefined}}{% endunless %}after')
|
||||
const html = await engine.render(tpl, ctx)
|
||||
return expect(html).to.equal('beforeXafter')
|
||||
return expect(html).toBe('beforeXafter')
|
||||
})
|
||||
it('should still throw with an undefined variable in a compound `if` expression', function () {
|
||||
const tpl = engine.parse('{% if notdefined == 15 %}a{% endif %}')
|
||||
const fhtml = engine.render(tpl, ctx)
|
||||
return expect(fhtml).to.be.rejectedWith(/undefined variable: notdefined/)
|
||||
return expect(fhtml).rejects.toThrow(/undefined variable: notdefined/)
|
||||
})
|
||||
it('should allow an undefined variable when before the `default` filter', async function () {
|
||||
const tpl = engine.parse('{{notdefined | default: "a" | tolower}}')
|
||||
const html = await engine.render(tpl, ctx)
|
||||
return expect(html).to.equal('a')
|
||||
return expect(html).toBe('a')
|
||||
})
|
||||
it('should not allow undefined variable even if `lenientIf` set', async function () {
|
||||
const tpl = engine.parse('{{notdefined | tolower}}')
|
||||
return expect(() => engine.renderSync(tpl, ctx)).to.throw('undefined variable: notdefined')
|
||||
return expect(() => engine.renderSync(tpl, ctx)).toThrow('undefined variable: notdefined')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
describe('LiquidOptions#trimming', function () {
|
||||
@@ -8,34 +7,34 @@ describe('LiquidOptions#trimming', function () {
|
||||
it('should respect trimTagLeft', async function () {
|
||||
const engine = new Liquid({ trimTagLeft: true })
|
||||
const html = await engine.parseAndRender(' \n \t{%if true%}foo{%endif%} ')
|
||||
return expect(html).to.equal('foo ')
|
||||
return expect(html).toBe('foo ')
|
||||
})
|
||||
it('should respect trimTagRight', async function () {
|
||||
const engine = new Liquid({ trimTagRight: true } as any)
|
||||
const html = await engine.parseAndRender('\t{%if true%}foo{%endif%} \n')
|
||||
return expect(html).to.equal('\tfoo')
|
||||
return expect(html).toBe('\tfoo')
|
||||
})
|
||||
it('should not trim value', async function () {
|
||||
const engine = new Liquid({ trimTagLeft: true, trimTagRight: true } as any)
|
||||
const html = await engine.parseAndRender('{%if true%}a {{name}} b{%endif%}', ctx)
|
||||
return expect(html).to.equal('a harttle b')
|
||||
return expect(html).toBe('a harttle b')
|
||||
})
|
||||
})
|
||||
describe('value trimming', function () {
|
||||
it('should respect trimOutputLeft', async function () {
|
||||
const engine = new Liquid({ trimOutputLeft: true } as any)
|
||||
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
||||
return expect(html).to.equal('harttle ')
|
||||
return expect(html).toBe('harttle ')
|
||||
})
|
||||
it('should respect trimOutputRight', async function () {
|
||||
const engine = new Liquid({ trimOutputRight: true } as any)
|
||||
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
||||
return expect(html).to.equal(' \n \tharttle')
|
||||
return expect(html).toBe(' \n \tharttle')
|
||||
})
|
||||
it('should respect not trim tag', async function () {
|
||||
const engine = new Liquid({ trimOutputLeft: true, trimOutputRight: true } as any)
|
||||
const html = await engine.parseAndRender('\t{% if true %} aha {%endif%}\t')
|
||||
return expect(html).to.equal('\t aha \t')
|
||||
return expect(html).toBe('\t aha \t')
|
||||
})
|
||||
})
|
||||
describe('greedy', function () {
|
||||
@@ -43,12 +42,12 @@ describe('LiquidOptions#trimming', function () {
|
||||
it('should enable greedy by default', async function () {
|
||||
const engine = new Liquid()
|
||||
const html = await engine.parseAndRender(src, ctx)
|
||||
return expect(html).to.equal('aharttle')
|
||||
return expect(html).toBe('aharttle')
|
||||
})
|
||||
it('should allow greedy:false', async function () {
|
||||
const engine = new Liquid({ greedy: false } as any)
|
||||
const html = await engine.parseAndRender(src, ctx)
|
||||
return expect(html).to.equal('\n a \nharttle ')
|
||||
return expect(html).toBe('\n a \nharttle ')
|
||||
})
|
||||
})
|
||||
describe('markup', function () {
|
||||
@@ -64,7 +63,7 @@ describe('LiquidOptions#trimming', function () {
|
||||
].join('\n')
|
||||
const dst = 'Wow, John G. Chalmers-Smith, you have a long name!'
|
||||
const html = await engine.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
return expect(html).toBe(dst)
|
||||
})
|
||||
it('should not trim when not specified', async function () {
|
||||
const engine = new Liquid()
|
||||
@@ -78,7 +77,7 @@ describe('LiquidOptions#trimming', function () {
|
||||
].join('\n')
|
||||
const dst = '\n\n Wow, John G. Chalmers-Smith, you have a long name!\n'
|
||||
const html = await engine.parseAndRender(src)
|
||||
return expect(html).to.equal(dst)
|
||||
return expect(html).toBe(dst)
|
||||
})
|
||||
})
|
||||
})
|
||||
+1
-2
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
const liquid = new Liquid()
|
||||
@@ -444,7 +443,7 @@ describe('Whitespace Control', function () {
|
||||
item.text,
|
||||
async () => {
|
||||
const html = await liquid.parseAndRender(item.text)
|
||||
expect(html).to.equal(item.expected)
|
||||
expect(html).toBe(item.expected)
|
||||
}
|
||||
))
|
||||
})
|
||||
Reference in New Issue
Block a user