mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
chore: migrate test cases from Chai to Jest
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { isTruthy, isFalsy } from './boolean'
|
||||
import { Context } from '../context'
|
||||
|
||||
describe('boolean Shopify', function () {
|
||||
describe('.isTruthy()', function () {
|
||||
const ctx = {
|
||||
opts: {
|
||||
jsTruthy: false
|
||||
}
|
||||
} as unknown as Context
|
||||
//
|
||||
// Spec: https://shopify.github.io/liquid/basics/truthy-and-falsy/
|
||||
it('true is truthy', function () {
|
||||
expect(isTruthy(true, ctx)).toBeTruthy()
|
||||
})
|
||||
it('false is falsy', function () {
|
||||
expect(isTruthy(false, ctx)).toBeFalsy()
|
||||
})
|
||||
it('null is falsy', function () {
|
||||
expect(isTruthy(null, ctx)).toBeFalsy()
|
||||
})
|
||||
it('"foo" is truthy', function () {
|
||||
expect(isTruthy('foo', ctx)).toBeTruthy()
|
||||
})
|
||||
it('"" is truthy', function () {
|
||||
expect(isTruthy('', ctx)).toBeTruthy()
|
||||
})
|
||||
it('0 is truthy', function () {
|
||||
expect(isTruthy(0, ctx)).toBeTruthy()
|
||||
})
|
||||
it('1 is truthy', function () {
|
||||
expect(isTruthy(1, ctx)).toBeTruthy()
|
||||
})
|
||||
it('1.1 is truthy', function () {
|
||||
expect(isTruthy(1.1, ctx)).toBeTruthy()
|
||||
})
|
||||
it('[1] is truthy', function () {
|
||||
expect(isTruthy([1], ctx)).toBeTruthy()
|
||||
})
|
||||
it('[] is truthy', function () {
|
||||
expect(isTruthy([], ctx)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('boolean jsTruthy', function () {
|
||||
const ctx = {
|
||||
opts: {
|
||||
jsTruthy: true
|
||||
}
|
||||
} as unknown as Context
|
||||
|
||||
describe('.isFalsy()', function () {
|
||||
it('null is always falsy', function () {
|
||||
expect(isFalsy(null, ctx)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('.isTruthy()', function () {
|
||||
it('true is truthy', function () {
|
||||
expect(isTruthy(true, ctx)).toBeTruthy()
|
||||
})
|
||||
it('false is falsy', function () {
|
||||
expect(isTruthy(false, ctx)).toBeFalsy()
|
||||
})
|
||||
it('null is always falsy', function () {
|
||||
expect(isTruthy(null, ctx)).toBeFalsy()
|
||||
})
|
||||
it('null is always falsy', function () {
|
||||
expect(isTruthy(null, ctx)).toBeFalsy()
|
||||
})
|
||||
it('"foo" is truthy', function () {
|
||||
expect(isTruthy('foo', ctx)).toBeTruthy()
|
||||
})
|
||||
it('"" is falsy', function () {
|
||||
expect(isTruthy('', ctx)).toBeFalsy()
|
||||
})
|
||||
it('0 is falsy', function () {
|
||||
expect(isTruthy(0, ctx)).toBeFalsy()
|
||||
})
|
||||
it('1 is truthy', function () {
|
||||
expect(isTruthy(1, ctx)).toBeTruthy()
|
||||
})
|
||||
it('1.1 is truthy', function () {
|
||||
expect(isTruthy(1.1, ctx)).toBeTruthy()
|
||||
})
|
||||
it('[1] is truthy', function () {
|
||||
expect(isTruthy([1], ctx)).toBeTruthy()
|
||||
})
|
||||
it('[] is truthy', function () {
|
||||
expect(isTruthy([], ctx)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,190 @@
|
||||
import { Tokenizer } from '../parser'
|
||||
import { Drop } from '../drop'
|
||||
import { Context } from '../context'
|
||||
import { toPromise, toValueSync } from '../util'
|
||||
|
||||
describe('Expression', function () {
|
||||
const ctx = new Context({})
|
||||
const create = (str: string) => new Tokenizer(str).readExpression()
|
||||
|
||||
it('should throw when context not defined', done => {
|
||||
toPromise(create('foo').evaluate(undefined!, false))
|
||||
.then(() => done(new Error('should not resolved')))
|
||||
.catch(err => {
|
||||
expect(err.message).toMatch(/context not defined/)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('single value', function () {
|
||||
it('should eval literal', async function () {
|
||||
expect(await toPromise(create('2.4').evaluate(ctx, false))).toBe(2.4)
|
||||
expect(await toPromise(create('"foo"').evaluate(ctx, false))).toBe('foo')
|
||||
expect(await toPromise(create('false').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should eval range expression', async function () {
|
||||
const ctx = new Context({ two: 2 })
|
||||
expect(await toPromise(create('(2..4)').evaluate(ctx, false))).toEqual([2, 3, 4])
|
||||
expect(await toPromise(create('(two..4)').evaluate(ctx, false))).toEqual([2, 3, 4])
|
||||
})
|
||||
it('should eval literal', async function () {
|
||||
expect(await toPromise(create('2.4').evaluate(ctx, false))).toBe(2.4)
|
||||
expect(await toPromise(create('"foo"').evaluate(ctx, false))).toBe('foo')
|
||||
expect(await toPromise(create('false').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
|
||||
it('should eval property access', async function () {
|
||||
const ctx = new Context({
|
||||
foo: { bar: 'BAR' },
|
||||
coo: 'bar',
|
||||
doo: { foo: 'bar', bar: { foo: 'bar' } }
|
||||
})
|
||||
expect(await toPromise(create('foo.bar').evaluate(ctx, false))).toBe('BAR')
|
||||
expect(await toPromise(create('foo["bar"]').evaluate(ctx, false))).toBe('BAR')
|
||||
expect(await toPromise(create('foo[coo]').evaluate(ctx, false))).toBe('BAR')
|
||||
expect(await toPromise(create('foo[doo.foo]').evaluate(ctx, false))).toBe('BAR')
|
||||
expect(await toPromise(create('foo[doo["foo"]]').evaluate(ctx, false))).toBe('BAR')
|
||||
expect(await toPromise(create('doo[coo].foo').evaluate(ctx, false))).toBe('bar')
|
||||
})
|
||||
})
|
||||
|
||||
describe('simple expression', function () {
|
||||
it('should return false for "1==2"', async () => {
|
||||
expect(await toPromise(create('1==2').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should apply deep equal for arrays', async () => {
|
||||
const ctx = new Context({
|
||||
arr1: [1, 2],
|
||||
arr2: [1, 2],
|
||||
arr3: [1, 2, 3]
|
||||
})
|
||||
expect(await toPromise(create('arr1==arr2').evaluate(ctx, false))).toBe(true)
|
||||
expect(await toPromise(create('arr1==arr3').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should return true for "1<2"', async () => {
|
||||
expect(await toPromise(create('1<2').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should return true for "1 < 2"', async () => {
|
||||
expect(await toPromise(create('1 < 2').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should return true for "1 < 2"', async () => {
|
||||
expect(await toPromise(create('1 < 2').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should return true for "2 <= 2"', async () => {
|
||||
expect(await toPromise(create('2 <= 2').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should return true for "one <= two"', async () => {
|
||||
const ctx = new Context({ one: 1, two: 2 })
|
||||
expect(await toPromise(create('one <= two').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should return false for "x contains "x""', async () => {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(await toPromise(create('x contains "x"').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should return true for "x contains "X""', async () => {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(await toPromise(create('x contains "X"').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should return false for "1 contains "x""', async () => {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(await toPromise(create('1 contains "x"').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should return false for "y contains "x""', async () => {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(await toPromise(create('y contains "x"').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should return false for "z contains "x""', async () => {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(await toPromise(create('z contains "x"').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should return true for "(1..5) contains 3"', async () => {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(await toPromise(create('(1..5) contains 3').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should return false for "(1..5) contains 6"', async () => {
|
||||
const ctx = new Context({ x: 'XXX' })
|
||||
expect(await toPromise(create('(1..5) contains 6').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should return true for ""<=" == "<=""', async () => {
|
||||
expect(await toPromise(create('"<=" == "<="').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
it('should allow space in quoted value', async function () {
|
||||
const ctx = new Context({ space: ' ' })
|
||||
expect(await toPromise(create('" " == space').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
|
||||
describe('escape', () => {
|
||||
it('should escape quote', async function () {
|
||||
const ctx = new Context({ quote: '"' })
|
||||
expect(await toPromise(create('"\\"" == quote').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should escape square bracket', async function () {
|
||||
const ctx = new Context({ obj: { ']': 'bracket' } })
|
||||
expect(await toPromise(create('obj["]"] == "bracket"').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('complex expression', function () {
|
||||
it('should support value or value', async function () {
|
||||
expect(await toPromise(create('false or true').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should support < and contains', async function () {
|
||||
expect(await toPromise(create('1 < 2 and x contains "x"').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should support < or contains', async function () {
|
||||
expect(await toPromise(create('1 < 2 or x contains "x"').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should support Drops for "x contains "x""', async () => {
|
||||
class TemplateDrop extends Drop {
|
||||
valueOf () { return 'X' }
|
||||
}
|
||||
const ctx = new Context({ x: 'XXX', X: new TemplateDrop() })
|
||||
expect(await toPromise(create('x contains X').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should support value and !=', async function () {
|
||||
const ctx = new Context({ empty: '' })
|
||||
expect(await toPromise(create('empty and empty != ""').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should recognize quoted value', async function () {
|
||||
expect(await toPromise(create('">"').evaluate(ctx, false))).toBe('>')
|
||||
})
|
||||
it('should evaluate from right to left', async function () {
|
||||
expect(await toPromise(create('true or false and false').evaluate(ctx, false))).toBe(true)
|
||||
expect(await toPromise(create('true and false and false or true').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should recognize property access', async function () {
|
||||
const ctx = new Context({ obj: { foo: true } })
|
||||
expect(await toPromise(create('obj["foo"] and true').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should allow nested property access', async function () {
|
||||
const ctx = new Context({ obj: { foo: 'FOO' }, keys: { "what's this": 'foo' } })
|
||||
expect(await toPromise(create('obj[keys["what\'s this"]]').evaluate(ctx, false))).toBe('FOO')
|
||||
})
|
||||
it('should support not', async function () {
|
||||
expect(await toPromise(create('not 1 < 2').evaluate(ctx))).toBe(false)
|
||||
})
|
||||
it('not should have higher precedence than and/or', async function () {
|
||||
expect(await toPromise(create('not 1 < 2 or not 1 > 2').evaluate(ctx))).toBe(true)
|
||||
expect(await toPromise(create('not 1 < 2 and not 1 > 2').evaluate(ctx))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('sync', function () {
|
||||
it('should eval literal', function () {
|
||||
expect(toValueSync(create('2.4').evaluate(ctx, false))).toBe(2.4)
|
||||
})
|
||||
it('should return false for "1==2"', () => {
|
||||
expect(toValueSync(create('1==2').evaluate(ctx, false))).toBe(false)
|
||||
})
|
||||
it('should escape quote', function () {
|
||||
const ctx = new Context({ quote: '"' })
|
||||
expect(toValueSync(create('"\\"" == quote').evaluate(ctx, false))).toBe(true)
|
||||
})
|
||||
it('should allow nested property access', function () {
|
||||
const ctx = new Context({ obj: { foo: 'FOO' }, keys: { "what's this": 'foo' } })
|
||||
expect(toValueSync(create('obj[keys["what\'s this"]]').evaluate(ctx, false))).toBe('FOO')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,65 @@
|
||||
import { Context } from '../context'
|
||||
import { HTMLToken, TagToken } from '../tokens'
|
||||
import { Render } from './render'
|
||||
import { Tag, HTML } from '../template'
|
||||
import { SimpleEmitter } from '../emitters'
|
||||
import { toPromise } from '../util'
|
||||
|
||||
describe('render', function () {
|
||||
let render: Render
|
||||
beforeEach(function () {
|
||||
render = new Render()
|
||||
})
|
||||
|
||||
describe('.renderTemplates()', function () {
|
||||
it('should render html', async function () {
|
||||
const scope = new Context()
|
||||
const token = { getContent: () => '<p>' } as HTMLToken
|
||||
const html = await toPromise(render.renderTemplates([new HTML(token)], scope, new SimpleEmitter()))
|
||||
return expect(html).toBe('<p>')
|
||||
})
|
||||
})
|
||||
|
||||
describe('.renderTemplatesToNodeStream()', function () {
|
||||
it('should render to html stream', function (done) {
|
||||
const scope = new Context()
|
||||
const tpls = [
|
||||
new HTML({ getContent: () => '<p>' } as HTMLToken),
|
||||
new HTML({ getContent: () => '</p>' } as HTMLToken)
|
||||
]
|
||||
const stream = render.renderTemplatesToNodeStream(tpls, scope)
|
||||
let result = ''
|
||||
stream.on('data', (data) => {
|
||||
result += data
|
||||
})
|
||||
stream.on('end', () => {
|
||||
expect(result).toBe('<p></p>')
|
||||
done()
|
||||
})
|
||||
})
|
||||
it('should render to html stream asyncly', function (done) {
|
||||
const scope = new Context()
|
||||
class CustomTag extends Tag {
|
||||
render () {
|
||||
return new Promise(
|
||||
resolve => setTimeout(() => resolve('async tag'), 10)
|
||||
)
|
||||
}
|
||||
}
|
||||
const tpls = [
|
||||
new HTML({ getContent: () => '<p>' } as HTMLToken),
|
||||
new CustomTag({ content: 'foo', args: '', name: 'foo' } as TagToken, [], {} as any),
|
||||
new HTML({ getContent: () => '</p>' } as HTMLToken)
|
||||
]
|
||||
const stream = render.renderTemplatesToNodeStream(tpls, scope)
|
||||
let result = ''
|
||||
stream.on('data', (data) => {
|
||||
result += data
|
||||
})
|
||||
stream.on('end', () => {
|
||||
expect(result).toBe('<p>async tag</p>')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user