chore: migrate test cases from Chai to Jest

This commit is contained in:
Harttle
2023-03-20 00:41:06 +08:00
committed by Jun Yang
parent dccb90c591
commit c6cde9cd10
97 changed files with 8163 additions and 26440 deletions
+2 -6
View File
@@ -1,9 +1,5 @@
import { Liquid } from '../../src/liquid'
import { Drop } from '../../src/drop/drop'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)
class SettingsDrop extends Drop {
private foo = 'FOO'
@@ -24,7 +20,7 @@ describe('drop', function () {
it('should support liquidMethodMissing', async function () {
const src = `{{settings.foo}},{{settings.bar}},{{settings.coo}}`
const html = await engine.parseAndRender(src, { settings })
return expect(html).to.equal('FOO,BAR,COO')
return expect(html).toBe('FOO,BAR,COO')
})
describe('BlandDrop', function () {
@@ -35,7 +31,7 @@ describe('drop', function () {
{% endunless %}`
var ctx = { settings: { fpHeading: '' } }
const html = await engine.parseAndRender(src, ctx)
return expect(html).to.match(/^\s+$/)
return expect(html).toMatch(/^\s+$/)
})
})
})
@@ -1,4 +1,3 @@
import { expect } from 'chai'
import { Liquid } from '../..'
describe('#evalValueSync()', function () {
@@ -6,6 +5,6 @@ describe('#evalValueSync()', function () {
beforeEach(() => { engine = new Liquid() })
it('should eval value syncly', async function () {
return expect(engine.evalValueSync('true', { opts: {} } as any)).to.equal(true)
return expect(engine.evalValueSync('true', { opts: {} } as any)).toBe(true)
})
})
@@ -1,4 +1,3 @@
import { expect } from 'chai'
import { Liquid } from '../..'
describe('#evalValue()', function () {
@@ -7,16 +6,16 @@ describe('#evalValue()', function () {
it('should support boolean', async function () {
const val = await engine.evalValue('true')
expect(val).to.equal(true)
expect(val).toBe(true)
})
it('should support binary expression with Context', async function () {
const val = await engine.evalValue('a > b', { a: 1, b: 2 })
expect(val).to.equal(false)
expect(val).toBe(false)
})
it('should inherit Liquid options', async function () {
const val = await engine.evalValue('foo')
expect(val).to.equal('FOO')
expect(val).toBe('FOO')
})
})
@@ -1,4 +1,3 @@
import { expect } from 'chai'
import * as request from 'supertest'
import * as express from 'express'
import { resolve } from 'path'
@@ -47,8 +46,8 @@ describe('express()', function () {
const ctx = {}
engine.express().call(view, file, ctx, function (err: any) {
try {
expect(err.code).to.equal('ENOENT')
expect(err.message).to.match(/Failed to lookup/)
expect(err.code).toBe('ENOENT')
expect(err.message).toMatch(/Failed to lookup/)
done()
} catch (e) {
done(e)
+67 -71
View File
@@ -1,18 +1,11 @@
import { TopLevelToken, TagToken, Tokenizer, Context, Liquid, Drop, toValueSync } from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
const LiquidUMD = require('../../dist/liquid.browser.umd.js').Liquid
use(chaiAsPromised)
use(sinonChai)
describe('Issues', function () {
it('#221 unicode blanks are not properly treated', async () => {
const engine = new Liquid({ strictVariables: true, strictFilters: true })
const html = engine.parseAndRenderSync('{{huh | truncate: 11}}', { huh: 'fdsafdsafdsafdsaaaaa' })
expect(html).to.equal('fdsafdsa...')
expect(html).toBe('fdsafdsa...')
})
it('#252 "Not valid identifier" error for a quotes-containing identifier', async () => {
const template = `{% capture "form_classes" -%}
@@ -20,12 +13,12 @@ describe('Issues', function () {
{%- endcapture %}{{form_classes}}`
const engine = new Liquid()
const html = await engine.parseAndRender(template)
expect(html).to.equal('foo')
expect(html).toBe('foo')
})
it('#259 complex property access with braces is not supported', async () => {
const engine = new Liquid()
const html = engine.parseAndRenderSync('{{ ["complex key"] }}', { 'complex key': 'foo' })
expect(html).to.equal('foo')
expect(html).toBe('foo')
})
it('#243 Potential for ReDoS through string replace function', async () => {
const engine = new Liquid()
@@ -38,13 +31,13 @@ describe('Issues', function () {
const html = engine.parseAndRenderSync(template, parameters)
// should stringify the regexp rather than execute it
expect(html).to.equal(INPUT)
expect(html).toBe(INPUT)
})
it('#263 raw/endraw block not ignoring {% characters', () => {
const template = `{% raw %}This is a code snippet showing how {% breaks the raw block.{% endraw %}`
const engine = new Liquid()
const html = engine.parseAndRenderSync(template)
expect(html).to.equal('This is a code snippet showing how {% breaks the raw block.')
expect(html).toBe('This is a code snippet showing how {% breaks the raw block.')
})
it('#268 elsif is not supported for unless', () => {
const template = `{%- unless condition1 -%}
@@ -56,7 +49,7 @@ describe('Issues', function () {
{% endunless %}`
const engine = new Liquid()
const html = engine.parseAndRenderSync(template, { condition1: true, condition2: true })
expect(html).to.equal('<div>Y</div>')
expect(html).toBe('<div>Y</div>')
})
it('#277 Passing liquid in FilterImpl', () => {
const engine = new Liquid()
@@ -67,12 +60,12 @@ describe('Issues', function () {
`{{ subtemplate | render: "foo" }}`,
{ subtemplate: encodeURIComponent('hello {{ name }}') }
)
expect(html).to.equal('hello foo')
expect(html).toBe('hello foo')
})
it('#288 Unexpected behavior when string literals contain }}', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(`{{ '{{' }}{{ '}}' }}`)
expect(html).to.equal('{{}}')
expect(html).toBe('{{}}')
})
it('#222 Support function calls', async () => {
const engine = new Liquid()
@@ -80,7 +73,7 @@ describe('Issues', function () {
`{{ obj.property }}`,
{ obj: { property: () => 'BAR' } }
)
expect(html).to.equal('BAR')
expect(html).toBe('BAR')
})
it('#313 lenientIf not working as expected in umd', async () => {
const engine = new LiquidUMD({
@@ -88,7 +81,7 @@ describe('Issues', function () {
lenientIf: true
})
const html = await engine.parseAndRender(`{{ name | default: "default name" }}`)
expect(html).to.equal('default name')
expect(html).toBe('default name')
})
it('#321 comparison for empty/nil', async () => {
const engine = new Liquid()
@@ -96,30 +89,31 @@ describe('Issues', function () {
'{% if empty == nil %}true{%else%}false{%endif%}' +
'{% if nil == empty %}true{%else%}false{%endif%}'
)
expect(html).to.equal('falsefalse')
expect(html).toBe('falsefalse')
})
it('#320 newline_to_br filter should output <br /> instead of <br/>', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(
`{{ 'a \n b \n c' | newline_to_br | split: '<br />' }}`
)
expect(html).to.equal('a \n b \n c')
expect(html).toBe('a \n b \n c')
})
it('#342 New lines in logical operator', async () => {
const engine = new Liquid()
const tpl = `{%\r\nif\r\ntrue\r\nor\r\nfalse\r\n%}\r\ntrue\r\n{%\r\nendif\r\n%}`
const html = await engine.parseAndRender(tpl)
expect(html).to.equal('\r\ntrue\r\n')
expect(html).toBe('\r\ntrue\r\n')
})
it('#401 Timezone Offset Issue', async () => {
const engine = new Liquid({ timezoneOffset: -600 })
const tpl = engine.parse('{{ date | date: "%Y-%m-%d %H:%M %p %z" }}')
const html = await engine.render(tpl, { date: '2021-10-06T15:31:00+08:00' })
expect(html).to.equal('2021-10-06 17:31 PM +1000')
expect(html).toBe('2021-10-06 17:31 PM +1000')
})
it('#412 Pass root as it is to `resolve`', async () => {
const engine = new Liquid({
root: '/tmp',
relativeReference: false,
fs: {
readFileSync: (file: string) => file,
async readFile (file: string) { return 'foo' },
@@ -130,15 +124,16 @@ describe('Issues', function () {
})
const tpl = engine.parse('{% include "foo.liquid" %}')
const html = await engine.renderSync(tpl)
expect(html).to.equal('/tmp/foo.liquid')
expect(html).toBe('/tmp/foo.liquid')
})
it('#416 Templates imported by {% render %} not cached for concurrent async render', async () => {
const readFile = sinon.spy(() => Promise.resolve('HELLO'))
const exists = sinon.spy(() => 'HELLO')
const readFile = jest.fn(() => Promise.resolve('HELLO'))
const exists = jest.fn(() => 'HELLO')
const engine = new Liquid({
cache: true,
extname: '.liquid',
root: '~',
relativeReference: false,
fs: {
exists,
resolve: (root: string, file: string, ext: string) => root + '#' + file + ext,
@@ -150,20 +145,21 @@ describe('Issues', function () {
await Promise.all(Array(5).fill(0).map(
x => engine.parseAndRender("{% render 'template' %}")
))
expect(exists).to.be.calledOnce
expect(readFile).to.be.calledOnce
expect(exists).toHaveBeenCalledTimes(1)
expect(readFile).toHaveBeenCalledTimes(1)
})
it('#431 Error when using Date timezoneOffset in 9.28.5', async () => {
it('#431 Error when using Date timezoneOffset in 9.28.5', () => {
const engine = new Liquid({
timezoneOffset: 0,
preserveTimezones: true
})
const tpl = engine.parse('Welcome to {{ now | date: "%Y-%m-%d" }}!')
expect(engine.render(tpl, { now: new Date('2019/02/01') })).to.eventually.equal('Welcome to 2019-02-01')
const tpl = engine.parse('Welcome to {{ now | date: "%Y-%m-%d" }}')
return expect(engine.render(tpl, { now: new Date('2019-02-01T00:00:00.000Z') })).resolves.toBe('Welcome to 2019-02-01')
})
it('#433 Support Jekyll-like includes', async () => {
const engine = new Liquid({
dynamicPartials: false,
relativeReference: false,
root: '/tmp',
fs: {
readFileSync: (file: string) => file,
@@ -175,7 +171,7 @@ describe('Issues', function () {
})
const tpl = engine.parse('{% include prefix/{{ my_variable | append: "-bar" }}/suffix %}')
const html = await engine.render(tpl, { my_variable: 'foo' })
expect(html).to.equal('CONTENT for /tmp/prefix/foo-bar/suffix')
expect(html).toBe('CONTENT for /tmp/prefix/foo-bar/suffix')
})
it('#428 Implement liquid/echo tags', () => {
const template = `{%- liquid
@@ -192,17 +188,17 @@ describe('Issues', function () {
-%}`
const engine = new Liquid()
const html = engine.parseAndRenderSync(template, { array: [1, 2, 3] })
expect(html).to.equal('4#8#12#6')
expect(html).toBe('4#8#12#6')
})
it('#454 leaking JS prototype getter functions in evaluation', async () => {
const engine = new Liquid({ ownPropertyOnly: true })
const html = engine.parseAndRenderSync('{{foo | size}}-{{bar.coo}}', { foo: 'foo', bar: Object.create({ coo: 'COO' }) })
expect(html).to.equal('3-')
expect(html).toBe('3-')
})
it('#465 Liquidjs divided_by not compatible with Ruby/Shopify Liquid', () => {
const engine = new Liquid({ ownPropertyOnly: true })
const html = engine.parseAndRenderSync('{{ 5 | divided_by: 3, true }}')
expect(html).to.equal('1')
expect(html).toBe('1')
})
it('#479 url_encode throws on undefined value', async () => {
const engine = new Liquid({
@@ -210,7 +206,7 @@ describe('Issues', function () {
})
const tpl = engine.parse('{{ v | url_encode }}')
const html = await engine.render(tpl, { v: undefined })
expect(html).to.equal('')
expect(html).toBe('')
})
it('#481 filters that should not throw', async () => {
const engine = new Liquid()
@@ -225,17 +221,17 @@ describe('Issues', function () {
{{ foo | concat | json }}
`)
const html = await engine.render(tpl, { foo: undefined })
expect(html.trim()).to.equal('[]')
expect(html.trim()).toBe('[]')
})
it('#481 concat should always return an array', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(`{{ foo | concat | json }}`)
expect(html).to.equal('[]')
expect(html).toBe('[]')
})
it('#486 Access array items from the right with negative indexes', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(`{% assign a = "x,y,z" | split: ',' -%}{{ a[-1] }} {{ a[-3] }} {{ a[-8] }}`)
expect(html).to.equal('z x ')
expect(html).toBe('z x ')
})
it('#492 contains operator does not support Drop', async () => {
class TemplateDrop extends Drop {
@@ -244,44 +240,44 @@ describe('Issues', function () {
const engine = new Liquid()
const ctx = { template: new TemplateDrop() }
const html = await engine.parseAndRender(`{% if template contains "product" %}contains{%endif%}`, ctx)
expect(html).to.equal('contains')
expect(html).toBe('contains')
})
it('#513 should support large number of templates [async]', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(`{% for i in (1..10000) %}{{ i }}{% endfor %}`)
expect(html).to.have.lengthOf(38894)
expect(html).toHaveLength(38894)
})
it('#513 should support large number of templates [sync]', () => {
const engine = new Liquid()
const html = engine.parseAndRenderSync(`{% for i in (1..10000) %}{{ i }}{% endfor %}`)
expect(html).to.have.lengthOf(38894)
expect(html).toHaveLength(38894)
})
it('#519 should throw parse error for invalid assign expression', () => {
const engine = new Liquid()
expect(() => engine.parse('{% assign headshot = https://testurl.com/not_enclosed_in_quotes.jpg %}')).to.throw(/unexpected token at ":/)
expect(() => engine.parse('{% assign headshot = https://testurl.com/not_enclosed_in_quotes.jpg %}')).toThrow(/unexpected token at ":/)
})
it('#527 export Liquid Expression', () => {
const tokenizer = new Tokenizer('a > b')
const expression = tokenizer.readExpression()
const result = toValueSync(expression.evaluate(new Context({ a: 1, b: 2 })))
expect(result).to.equal(false)
expect(result).toBe(false)
})
it('#527 export Liquid Expression (evalValue)', async () => {
const liquid = new Liquid()
const result = await liquid.evalValue('a > b', { a: 1, b: 2 })
expect(result).to.equal(false)
expect(result).toBe(false)
})
it('#527 export Liquid Expression (evalValueSync)', async () => {
const liquid = new Liquid()
const result = liquid.evalValueSync('a > b', { a: 1, b: 2 })
expect(result).to.equal(false)
expect(result).toBe(false)
})
it('#276 Promise support in expressions', async () => {
const liquid = new Liquid()
const tpl = '{%if name == "alice" %}true{%endif%}'
const ctx = { name: Promise.resolve('alice') }
const html = await liquid.parseAndRender(tpl, ctx)
expect(html).to.equal('true')
expect(html).toBe('true')
})
it('#533 Nested Promise support for scope object', async () => {
const liquid = new Liquid()
@@ -308,26 +304,26 @@ describe('Issues', function () {
})
}
expect(await liquid.evalValue('a == 1', context)).to.equal(true)
expect(await liquid.evalValue('b == 1', context)).to.equal(true)
expect(await liquid.evalValue('c == 1', context)).to.equal(true)
expect(await liquid.evalValue('d.d == 1', context)).to.equal(true)
expect(await liquid.evalValue('e.e == 1', context)).to.equal(true)
expect(await liquid.evalValue('f.f == 1', context)).to.equal(true)
expect(await liquid.evalValue('g.g == 1', context)).to.equal(true)
expect(await liquid.evalValue('h.h == 1', context)).to.equal(true)
expect(await liquid.evalValue('i.i == 1', context)).to.equal(true)
expect(await liquid.evalValue('j.j == 1', context)).to.equal(true)
expect(await liquid.parseAndRender('{{a}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{b}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{c}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{d.d}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{e.e}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{f.f}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{g.g}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{h.h}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{i.i}}', context)).to.equal('1')
expect(await liquid.parseAndRender('{{j.j}}', context)).to.equal('1')
expect(await liquid.evalValue('a == 1', context)).toBe(true)
expect(await liquid.evalValue('b == 1', context)).toBe(true)
expect(await liquid.evalValue('c == 1', context)).toBe(true)
expect(await liquid.evalValue('d.d == 1', context)).toBe(true)
expect(await liquid.evalValue('e.e == 1', context)).toBe(true)
expect(await liquid.evalValue('f.f == 1', context)).toBe(true)
expect(await liquid.evalValue('g.g == 1', context)).toBe(true)
expect(await liquid.evalValue('h.h == 1', context)).toBe(true)
expect(await liquid.evalValue('i.i == 1', context)).toBe(true)
expect(await liquid.evalValue('j.j == 1', context)).toBe(true)
expect(await liquid.parseAndRender('{{a}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{b}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{c}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{d.d}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{e.e}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{f.f}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{g.g}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{h.h}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{i.i}}', context)).toBe('1')
expect(await liquid.parseAndRender('{{j.j}}', context)).toBe('1')
})
it('#559 Case/When should evaluate multiple When statements', async () => {
const liquid = new Liquid()
@@ -344,7 +340,7 @@ describe('Issues', function () {
{% endcase %}
`
const html = await liquid.parseAndRender(tpl)
expect(html).to.match(/^\s*This is a love or luck potion.\s+This is a strength or health or love potion.\s*$/)
expect(html).toMatch(/^\s*This is a love or luck potion.\s+This is a strength or health or love potion.\s*$/)
})
it('#570 tag registration compatible to v9', async () => {
const liquid = new Liquid()
@@ -360,7 +356,7 @@ describe('Issues', function () {
const tpl = '{% metadata_file foo %}'
const ctx = { foo: 'FOO' }
const html = await liquid.parseAndRender(tpl, ctx)
expect(html).to.equal('FOO')
expect(html).toBe('FOO')
})
it('#573 date filter should return parsed input when no format is provided', async () => {
const liquid = new Liquid()
@@ -376,7 +372,7 @@ describe('Issues', function () {
const tpl = `{{ 'now' | date }}`
const html = await liquid.parseAndRender(tpl)
// sample: Thursday, February 2, 2023 at 6:25 pm +0000
expect(html).to.match(/\w+, \w+ \d+, \d\d\d\d at \d+:\d\d [ap]m [-+]\d\d\d\d/)
expect(html).toMatch(/\w+, \w+ \d+, \d\d\d\d at \d+:\d\d [ap]m [-+]\d\d\d\d/)
})
it('#575 Add support for Not operator', async () => {
const liquid = new Liquid()
@@ -388,7 +384,7 @@ describe('Issues', function () {
{% endif %}`
const ctx = { link: 'https://example.com', button: false }
const html = await liquid.parseAndRender(tpl, ctx)
expect(html.trim()).to.equal('<a href="https://example.com">Lot more code here</a>')
expect(html.trim()).toBe('<a href="https://example.com">Lot more code here</a>')
})
it('#70 strip multiline content of <style>', async () => {
const str = `
@@ -398,7 +394,7 @@ describe('Issues', function () {
const engine = new Liquid()
const template = '{{ str | strip_html }}'
const html = await engine.parseAndRender(template, { str })
expect(html).to.match(/^\s*$/)
expect(html).toMatch(/^\s*$/)
})
it('#589 Arrays should compare values', async () => {
const engine = new Liquid()
@@ -408,6 +404,6 @@ describe('Issues', function () {
{% if people1 == people2 %}true{%else%}false{% endif %}
`
const html = await engine.parseAndRender(template)
expect(html).to.contain('true')
expect(html).toContain('true')
})
})
@@ -1,8 +1,4 @@
import { Liquid } from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)
describe('.parseAndRender()', function () {
var engine: Liquid, strictEngine: Liquid
@@ -15,51 +11,50 @@ describe('.parseAndRender()', function () {
it('should stringify array ', async function () {
var ctx = { arr: [-2, 'a'] }
const html = await engine.parseAndRender('{{arr}}', ctx)
return expect(html).to.equal('-2a')
return expect(html).toBe('-2a')
})
it('should render undefined as empty', async function () {
const html = await engine.parseAndRender('foo{{zzz}}bar', {})
return expect(html).to.equal('foobar')
return expect(html).toBe('foobar')
})
it('should render as null when filter undefined', async function () {
const html = await engine.parseAndRender('{{"foo" | filter1}}', {})
return expect(html).to.equal('foo')
return expect(html).toBe('foo')
})
it('should throw upon undefined filter when strictFilters set', function () {
return expect(strictEngine.parseAndRender('{{"foo" | filter1}}', {})).to
.be.rejectedWith(/undefined filter: filter1/)
return expect(strictEngine.parseAndRender('{{"foo" | filter1}}', {})).rejects.toThrow(/undefined filter: filter1/)
})
it('should parse html', function () {
expect(function () {
engine.parse('{{obj}}')
}).to.not.throw()
}).not.toThrow()
expect(function () {
engine.parse('<html><head>{{obj}}</head></html>')
}).to.not.throw()
}).not.toThrow()
})
it('template should be able to be rendered multiple times', async function () {
const ctx = { obj: [1, 2] }
const template = engine.parse('{{obj}}')
const result = await engine.render(template, ctx)
expect(result).to.equal('12')
expect(result).toBe('12')
const result2 = await engine.render(template, ctx)
expect(result2).to.equal('12')
expect(result2).toBe('12')
})
it('should support the "join" filter', 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>')
return expect(html).toBe('<p>alice,bob</p>')
})
it('should support the "first" filter', 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')
return expect(html).toBe('apples')
})
it('should support nil(null, undefined) literal', async function () {
const src = '{% if notexist == nil %}true{% endif %}'
const html = await engine.parseAndRender(src)
expect(html).to.equal('true')
expect(html).toBe('true')
})
})
@@ -1,10 +1,6 @@
import { Liquid } from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { resolve } from 'path'
use(chaiAsPromised)
describe('#renderFile()', function () {
const root = resolve(__dirname, '../stub/root')
const views = resolve(__dirname, '../stub/views')
@@ -17,16 +13,16 @@ describe('#renderFile()', function () {
})
it('should render file', async function () {
const html = await engine.renderFile(resolve(root, 'foo.html'), {})
return expect(html).to.equal('foo')
return expect(html).toBe('foo')
})
it('should find files without extname', async function () {
var engine = new Liquid({ root })
const html = await engine.renderFile(resolve(root, 'bar'), {})
return expect(html).to.equal('bar')
return expect(html).toBe('bar')
})
it('should accept relative path', async function () {
const html = await engine.renderFile('foo.html')
return expect(html).to.equal('foo')
return expect(html).toBe('foo')
})
it('should traverse root array', async function () {
engine = new Liquid({
@@ -34,27 +30,26 @@ describe('#renderFile()', function () {
extname: '.html'
})
const html = await engine.renderFile('foo.html')
return expect(html).to.equal('foo')
return expect(html).toBe('foo')
})
it('should default root to cwd', async function () {
engine = new Liquid()
const html = await engine.renderFile('package.json')
return expect(html).to.contain('"name": "liquidjs"')
return expect(html).toContain('"name": "liquidjs"')
})
it('should render file with context', async function () {
const html = await engine.renderFile(resolve(views, 'name.html'), { name: 'harttle' })
return expect(html).to.equal('My name is harttle.')
return expect(html).toBe('My name is harttle.')
})
it('should use default extname', async function () {
const html = await engine.renderFile(resolve(root, 'foo'))
return expect(html).to.equal('foo')
return expect(html).toBe('foo')
})
it('should throw with lookup list when file not exist', function () {
engine = new Liquid({
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\/"/)
})
})
@@ -1,10 +1,6 @@
import { expect, use } from 'chai'
import { resolve } from 'path'
import * as chaiAsPromised from 'chai-as-promised'
import { drainStream } from '../stub/stream'
use(chaiAsPromised)
describe('.renderToNodeStream()', function () {
it('should render to stream in Node.js', done => {
const cjs = require('../../dist/liquid.node.cjs')
@@ -15,7 +11,7 @@ describe('.renderToNodeStream()', function () {
stream.on('data', (data: string) => { html += data })
stream.on('end', () => {
try {
expect(html).to.equal('foo')
expect(html).toBe('foo')
done()
} catch (err) {
done(err)
@@ -26,7 +22,7 @@ describe('.renderToNodeStream()', function () {
const cjs = require('../../dist/liquid.browser.umd')
const engine = new cjs.Liquid()
const render = () => engine.renderToNodeStream('foo')
return expect(render).to.throw('streaming not supported in browser')
return expect(render).toThrow('streaming not supported in browser')
})
})
@@ -37,6 +33,6 @@ describe('.renderFileToNodeStream()', function () {
root: resolve(__dirname, '../stub/root/')
})
const stream = await engine.renderFileToNodeStream('foo.html')
expect(drainStream(stream)).to.eventually.equal('foo')
expect(drainStream(stream)).resolves.toBe('foo')
})
})
+25 -28
View File
@@ -1,10 +1,7 @@
import { Liquid } from '../../dist/liquid.browser.umd.js'
import * as sinon from 'sinon'
import { expect, use } from 'chai'
import { JSDOM } from 'jsdom'
import * as chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)
import type { Liquid } from '../..'
const LiquidUMD = require('../../dist/liquid.browser.umd.js').Liquid
describe('xhr', () => {
if (+(process.version.match(/^v(\d+)/) as RegExpMatchArray)[1] < 8) {
@@ -24,7 +21,7 @@ describe('xhr', () => {
});
(global as any).XMLHttpRequest = sinon.FakeXMLHttpRequest;
(global as any).document = dom.window.document
engine = new Liquid({
engine = new LiquidUMD({
root: 'https://example.com/views/',
extname: '.html'
})
@@ -37,21 +34,21 @@ describe('xhr', () => {
describe('#renderFile()', () => {
it('should support without extname', async () => {
const html = await engine.renderFile('hello', { name: 'alice1' })
return expect(html).to.equal('hello alice1')
return expect(html).toBe('hello alice1')
})
it('should support with extname', async () => {
const html = await engine.renderFile('hello.html', { name: 'alice2' })
return expect(html).to.equal('hello alice2')
return expect(html).toBe('hello alice2')
})
it('should support with absolute path', async () => {
server.respondWith('GET', 'https://example.com/foo.html',
[200, { 'Content-Type': 'text/plain' }, 'foo'])
const html = await engine.renderFile('/foo.html')
return expect(html).to.equal('foo')
return expect(html).toBe('foo')
})
it('should support with url', async () => {
const html = await engine.renderFile('https://example.com/views/hello.html', { name: 'alice4' })
return expect(html).to.equal('hello alice4')
return expect(html).toBe('hello alice4')
})
it('should support include', async () => {
server.respondWith('GET', 'https://example.com/views/hello.html',
@@ -59,15 +56,15 @@ describe('xhr', () => {
server.respondWith('GET', 'https://example.com/views/name.html',
[200, { 'Content-Type': 'text/plain' }, '{{name}}'])
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
return expect(html).toBe('hello alice5')
})
it('should throw 404', () => {
return expect(engine.renderFile('/not/exist.html'))
.to.be.rejectedWith('Not Found')
.rejects.toThrow('Not Found')
})
it('should throw error', function () {
const result = expect(engine.renderFile('hello.html'))
.to.be.rejectedWith('An error occurred whilst receiving the response.');
.rejects.toThrow('An error occurred whilst receiving the response.');
(global as any).XMLHttpRequest.onCreate = function (request: sinon.SinonFakeXMLHttpRequest) {
setTimeout(() => request.error())
}
@@ -76,53 +73,53 @@ describe('xhr', () => {
})
describe('#renderFile() with root specified', () => {
it('should support undefined root', async () => {
engine = new Liquid({
engine = new LiquidUMD({
extname: '.html'
})
server.respondWith('GET', 'https://example.com/foo/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
return expect(html).toBe('hello alice5')
})
it('should support empty root', async () => {
engine = new Liquid({
engine = new LiquidUMD({
root: '',
extname: '.html'
})
server.respondWith('https://example.com/foo/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
return expect(html).toBe('hello alice5')
})
it('should support with relative path', async () => {
engine = new Liquid({
engine = new LiquidUMD({
root: './views/',
extname: '.html'
})
server.respondWith('GET', 'https://example.com/foo/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
return expect(html).toBe('hello alice5')
})
it('should support with absolute path', async () => {
engine = new Liquid({
engine = new LiquidUMD({
root: '/views/',
extname: '.html'
})
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
return expect(html).toBe('hello alice5')
})
it('should support with url', async () => {
engine = new Liquid({
engine = new LiquidUMD({
root: 'https://foo.com/bar/',
extname: '.html'
})
server.respondWith('GET', 'https://foo.com/bar/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
return expect(html).toBe('hello alice5')
})
})
describe('cache options', () => {
@@ -131,15 +128,15 @@ describe('xhr', () => {
[200, { 'Content-Type': 'text/plain' }, 'foo1'])
return engine.renderFile('foo.html')
.then((html: string) => {
expect(html).to.equal('foo1')
expect(html).toBe('foo1')
server.respondWith('GET', 'https://example.com/views/foo.html',
[200, { 'Content-Type': 'text/plain' }, 'foo2'])
return engine.renderFile('foo.html')
})
.then((html: string) => expect(html).to.equal('foo2'))
.then((html: string) => expect(html).toBe('foo2'))
})
it('should respect cache=true option', () => {
engine = new Liquid({
engine = new LiquidUMD({
root: '/views/',
extname: '.html',
cache: true
@@ -148,12 +145,12 @@ describe('xhr', () => {
[200, { 'Content-Type': 'text/plain' }, 'foo1'])
return engine.renderFile('foo.html')
.then((html: string) => {
expect(html).to.equal('foo1')
expect(html).toBe('foo1')
server.respondWith('GET', 'https://example.com/views/foo.html',
[200, { 'Content-Type': 'text/plain' }, 'foo2'])
return engine.renderFile('foo.html')
})
.then((html: string) => expect(html).to.equal('foo1'))
.then((html: string) => expect(html).toBe('foo1'))
})
})
})