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
+55
View File
@@ -0,0 +1,55 @@
import { Context } from '../context'
import { toPromise } from '../util'
import { IdentifierToken, NumberToken, QuotedToken } from '../tokens'
import { Filter } from './filter'
describe('filter', function () {
const ctx = new Context({ thirty: 30 })
const liquid = { testVersion: '1.0' } as any
it('should not change input if filter not registered', async function () {
const filter = new Filter('foo', undefined as any, [], liquid)
expect(await toPromise(filter.render('value', ctx))).toBe('value')
})
it('should call filter impl with correct arguments', async function () {
const spy = jest.fn()
const thirty = new NumberToken(new IdentifierToken('30', 0, 2), undefined)
const filter = new Filter('foo', spy, [thirty], liquid)
await toPromise(filter.render('foo', ctx))
expect(spy).toHaveBeenCalledWith('foo', 30)
})
it('should call filter impl with correct this', async function () {
const spy = jest.fn(function * (valStr, diff): Generator<string> {
const val = yield this.context._get([valStr])
return `${this.liquid.testVersion}: ${val + diff}`
})
const ten = new NumberToken(new IdentifierToken('10', 0, 2), undefined)
const filter = new Filter('add', spy, [ten], liquid)
const val = await toPromise(filter.render('thirty', ctx))
expect(val).toEqual('1.0: 40')
})
it('should render a simple filter', async function () {
expect(await toPromise(new Filter('upcase', (x: string) => x.toUpperCase(), [], liquid).render('foo', ctx))).toBe('FOO')
})
it('should render filters with argument', async function () {
const two = new NumberToken(new IdentifierToken('2', 0, 1), undefined)
expect(await toPromise(new Filter('add', (a: number, b: number) => a + b, [two], liquid).render(3, ctx))).toBe(5)
})
it('should render filters with multiple arguments', async function () {
const two = new NumberToken(new IdentifierToken('2', 0, 1), undefined)
const c = new QuotedToken('"c"', 0, 3)
expect(await toPromise(new Filter('add', (a: number, b: number, c: number) => a + b + c, [two, c], liquid).render(3, ctx))).toBe('5c')
})
it('should pass Objects/Drops as it is', async function () {
class Foo {}
expect(await toPromise(new Filter('name', (a: any) => a.constructor.name, [], liquid).render(new Foo(), ctx))).toBe('Foo')
})
it('should support key value pairs', async function () {
const two = new NumberToken(new IdentifierToken('2', 0, 1), undefined)
expect(await toPromise(new Filter('add', (a: number, b: number[]) => b[0] + ':' + (a + b[1]), [['num', two]], liquid).render(3, ctx))).toBe('num:5')
})
})
+42
View File
@@ -0,0 +1,42 @@
import { toPromise } from '../util'
import { Hash } from './hash'
import { Context } from '../context'
describe('Hash', function () {
it('should parse "reverse"', async function () {
const hash = await toPromise(new Hash('reverse').render(new Context({ foo: 3 })))
expect(hash).toHaveProperty('reverse')
expect(hash.reverse).toBeTruthy()
})
it('should parse "num:foo"', async function () {
const hash = await toPromise(new Hash('num:foo').render(new Context({ foo: 3 })))
expect(hash.num).toBe(3)
})
it('should parse "num:3"', async function () {
const hash = await toPromise(new Hash('num:3').render(new Context()))
expect(hash.num).toBe(3)
})
it('should parse "num: arr[0]"', async function () {
const hash = await toPromise(new Hash('num:3').render(new Context({ arr: [3] })))
expect(hash.num).toBe(3)
})
it('should parse "num: 2.3"', async function () {
const hash = await toPromise(new Hash('num:2.3').render(new Context()))
expect(hash.num).toBe(2.3)
})
it('should parse "num:bar.coo"', async function () {
const pending = new Hash('num:bar.coo').render(new Context({ bar: { coo: 3 } }))
const hash = await toPromise(pending)
expect(hash.num).toBe(3)
})
it('should parse "num1:2.3 reverse,num2:bar.coo\n num3: arr[0]"', async function () {
const ctx = new Context({ bar: { coo: 3 }, arr: [4] })
const hash = await toPromise(new Hash('num1:2.3 reverse,num2:bar.coo\n num3: arr[0]').render(ctx))
expect(hash).toEqual({
num1: 2.3,
reverse: true,
num2: 3,
num3: 4
})
})
})
+86
View File
@@ -0,0 +1,86 @@
import { toPromise } from '../util'
import { Context } from '../context'
import { Output } from '../template'
import { OutputToken } from '../tokens'
import { defaultOptions } from '../liquid-options'
describe('Output', function () {
const emitter: any = { write: (html: string) => (emitter.html += html), html: '' }
const liquid = { options: {} } as any
beforeEach(() => { emitter.html = '' })
it('should stringify objects', async function () {
const scope = new Context({
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toBe('[object Object]')
})
it('should skip function property', async function () {
const scope = new Context({ obj: { foo: 'foo', bar: (x: any) => x } })
const output = new Output({ content: 'obj' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toBe('[object Object]')
})
it('should respect to .toString()', async () => {
const scope = new Context({ obj: { toString: () => 'FOO' } })
const output = new Output({ content: 'obj' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toBe('FOO')
})
it('should respect to .toString()', async () => {
const scope = new Context({ obj: { toString: () => 'FOO' } })
const output = new Output({ content: 'obj' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toBe('FOO')
})
describe('when keepOutputType is enabled', () => {
const emitter: any = {
write: (html: any) => {
if (emitter.keepOutputType && typeof html !== 'string') {
emitter.html = html
} else {
emitter.html += html as string
}
},
html: '',
keepOutputType: true
}
beforeEach(() => { emitter.html = '' })
it('should respect output variable number type', async () => {
const scope = new Context({
foo: 42
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toBe(42)
})
it('should respect output variable boolean type', async () => {
const scope = new Context({
foo: true
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toBe(true)
})
it('should respect output variable object type', async () => {
const scope = new Context({
foo: 'test'
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toBe('test')
})
it('should respect output variable string type', async () => {
const scope = new Context({
foo: { a: { b: 42 } }
}, { ...defaultOptions, keepOutputType: true })
const output = new Output({ content: 'foo' } as OutputToken, liquid)
await toPromise(output.render(scope, emitter))
return expect(emitter.html).toEqual({ a: { b: 42 } })
})
})
})
+37
View File
@@ -0,0 +1,37 @@
import { Liquid } from '../liquid'
import { QuotedToken } from '../tokens'
import { toPromise } from '../util'
import { Context } from '../context'
import { Value } from '../template'
describe('Value', function () {
const liquid = new Liquid()
describe('#constructor()', function () {
it('should parse filters in value content', function () {
const f = new Value('o | foo: a: "a"', liquid)
expect(f.filters[0].name).toBe('foo')
expect(f.filters[0].args).toHaveLength(1)
const [k, v] = f.filters[0].args[0] as any
expect(k).toBe('a')
expect(v).toBeInstanceOf(QuotedToken)
expect((v as QuotedToken).getText()).toBe('"a"')
})
})
describe('#value()', function () {
it('should call chained filters correctly', async function () {
const date = jest.fn(() => 'y')
const time = jest.fn()
liquid.registerFilter('date', date)
liquid.registerFilter('time', time)
const tpl = new Value('foo.bar | date: "b" | time:2', liquid)
const scope = new Context({
foo: { bar: 'bar' }
})
await toPromise(tpl.value(scope, false))
expect(date).toHaveBeenCalledWith('bar', 'b')
expect(time).toHaveBeenCalledWith('y', 2)
})
})
})