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:
@@ -0,0 +1,16 @@
|
||||
import { assert } from './assert'
|
||||
|
||||
describe('assert', function () {
|
||||
it('should not throw if predicate is truthy', function () {
|
||||
const fn = () => assert('foo', () => 'bar')
|
||||
expect(fn).not.toThrow()
|
||||
})
|
||||
it('should not throw if predicate is truthy', function () {
|
||||
const fn = () => assert('', () => 'bar')
|
||||
expect(fn).toThrow(/bar/)
|
||||
})
|
||||
it('should populate default message', function () {
|
||||
const fn = () => assert(false)
|
||||
expect(fn).toThrow(/expect false to be true/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,112 @@
|
||||
import { toPromise, toValueSync } from './async'
|
||||
|
||||
describe('utils/async', () => {
|
||||
describe('#toPromise()', function () {
|
||||
it('should return a promise', async () => {
|
||||
function * foo () {
|
||||
return 'foo'
|
||||
}
|
||||
const result = await toPromise(foo())
|
||||
expect(result).toBe('foo')
|
||||
})
|
||||
it('should support iterable with single return statement', async () => {
|
||||
function * foo () {
|
||||
return 'foo'
|
||||
}
|
||||
const result = await toPromise(foo())
|
||||
expect(result).toBe('foo')
|
||||
})
|
||||
it('should support promise', async () => {
|
||||
function foo () {
|
||||
return Promise.resolve('foo')
|
||||
}
|
||||
const result = await toPromise(foo())
|
||||
expect(result).toBe('foo')
|
||||
})
|
||||
it('should resolve dependency', async () => {
|
||||
function * foo (): Generator<Generator<string>> {
|
||||
return yield bar()
|
||||
}
|
||||
function * bar (): Generator<string> {
|
||||
return 'bar'
|
||||
}
|
||||
const result = await toPromise(foo())
|
||||
expect(result).toBe('bar')
|
||||
})
|
||||
it('should support promise dependency', async () => {
|
||||
function * foo (): Generator<Promise<string>> {
|
||||
return yield Promise.resolve('foo')
|
||||
}
|
||||
const result = await toPromise(foo())
|
||||
expect(result).toBe('foo')
|
||||
})
|
||||
it('should reject Promise if dependency throws syncly', done => {
|
||||
function * foo (): Generator<Generator<never>> {
|
||||
return yield bar()
|
||||
}
|
||||
function * bar (): Generator<never> {
|
||||
throw new Error('bar')
|
||||
}
|
||||
toPromise(foo()).catch(err => {
|
||||
expect(err.message).toBe('bar')
|
||||
done()
|
||||
return 0 as any
|
||||
})
|
||||
})
|
||||
it('should resume promise after catch', async () => {
|
||||
function * foo () {
|
||||
let ret = ''
|
||||
try {
|
||||
yield bar()
|
||||
} catch (e) {
|
||||
ret += 'bar'
|
||||
}
|
||||
ret += 'foo'
|
||||
return ret
|
||||
}
|
||||
function * bar (): Generator<never> {
|
||||
throw new Error('bar')
|
||||
}
|
||||
const ret = await toPromise(foo())
|
||||
expect(ret).toBe('barfoo')
|
||||
})
|
||||
})
|
||||
describe('#toValueSync()', function () {
|
||||
it('should throw Error if dependency throws syncly', () => {
|
||||
function * foo (): Generator<Generator<never>> {
|
||||
return yield bar()
|
||||
}
|
||||
function * bar (): Generator<never> {
|
||||
throw new Error('bar')
|
||||
}
|
||||
expect(() => toValueSync(foo())).toThrow('bar')
|
||||
})
|
||||
it('should resume yield after catch', () => {
|
||||
function * foo (): Generator<unknown, never, never> {
|
||||
try {
|
||||
yield bar()
|
||||
} catch (e) {}
|
||||
return yield 'foo'
|
||||
}
|
||||
function * bar (): Generator<never> {
|
||||
throw new Error('bar')
|
||||
}
|
||||
expect(toValueSync(foo())).toBe('foo')
|
||||
})
|
||||
it('should resume return after catch', () => {
|
||||
function * foo (): Generator<Generator<never>, string> {
|
||||
try {
|
||||
yield bar()
|
||||
} catch (e) {}
|
||||
return 'foo'
|
||||
}
|
||||
function * bar (): Generator<never> {
|
||||
throw new Error('bar')
|
||||
}
|
||||
expect(toValueSync(foo())).toBe('foo')
|
||||
})
|
||||
it('should return non iterator value as it is', () => {
|
||||
expect(toValueSync('foo')).toBe('foo')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,217 @@
|
||||
import { strftime as t } from './strftime'
|
||||
import { DateWithTimezone } from '../../test/stub/date-with-timezone'
|
||||
|
||||
describe('util/strftime', function () {
|
||||
const now = new Date('2016-01-04 13:15:23')
|
||||
const then = new Date('2016-03-06 03:05:03')
|
||||
|
||||
describe('Date (Year, Month, Day)', () => {
|
||||
it('should format %C as century', function () {
|
||||
expect(t(now, '%C')).toBe('20')
|
||||
})
|
||||
it('should format %B as month name', function () {
|
||||
expect(t(now, '%B')).toBe('January')
|
||||
})
|
||||
it('should format %e as space padded date', function () {
|
||||
expect(t(now, '%e')).toBe(' 4')
|
||||
})
|
||||
it('should format %y as 2-digit year', function () {
|
||||
expect(t(now, '%y')).toBe('16')
|
||||
})
|
||||
describe('%j', function () {
|
||||
it('should format %j as day of year', function () {
|
||||
expect(t(then, '%j')).toBe('066')
|
||||
})
|
||||
it('should take count of leap years', function () {
|
||||
const date = new Date('2001 03 01')
|
||||
expect(t(date, '%j')).toBe('060')
|
||||
})
|
||||
it('should take count of leap years', function () {
|
||||
const date = new Date('2000 03 01')
|
||||
expect(t(date, '%j')).toBe('061')
|
||||
})
|
||||
})
|
||||
it('should format %q as date suffix', function () {
|
||||
const st = new Date('2016-03-01 03:05:03')
|
||||
const nd = new Date('2016-03-02 03:05:03')
|
||||
const rd = new Date('2016-03-03 03:05:03')
|
||||
expect(t(st, '%q')).toBe('st')
|
||||
expect(t(nd, '%q')).toBe('nd')
|
||||
expect(t(rd, '%q')).toBe('rd')
|
||||
expect(t(now, '%q')).toBe('th')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Time (Hour, Minute, Second, Subsecond)', function () {
|
||||
it('should format %I as 0 padded hour12', function () {
|
||||
expect(t(now, '%I')).toBe('01')
|
||||
})
|
||||
it('should format %I as 12 for 00:00', function () {
|
||||
const date = new Date('2016-01-01 00:00:00')
|
||||
expect(t(date, '%I')).toBe('12')
|
||||
})
|
||||
it('should format %k as space padded hour', function () {
|
||||
expect(t(then, '%k')).toBe(' 3')
|
||||
})
|
||||
it('should format %l as space padded hour12', function () {
|
||||
expect(t(now, '%l')).toBe(' 1')
|
||||
})
|
||||
it('should format %l as 12 for 00:00', function () {
|
||||
const date = new Date('2016-01-01 00:00:00')
|
||||
expect(t(date, '%l')).toBe('12')
|
||||
})
|
||||
it('should format %L as 0 padded millisecond', function () {
|
||||
expect(t(then, '%L')).toBe('000')
|
||||
})
|
||||
it('should format %N as fractional seconds digits', function () {
|
||||
const time = new Date('2019-12-15 01:21:00.129')
|
||||
expect(t(time, '%N')).toBe('129000000')
|
||||
expect(t(time, '%2N')).toBe('12')
|
||||
expect(t(time, '%10N')).toBe('1290000000')
|
||||
expect(t(time, '%0N')).toBe('129000000')
|
||||
})
|
||||
it('should format %p as upper cased am/pm', function () {
|
||||
expect(t(now, '%p')).toBe('PM')
|
||||
expect(t(then, '%p')).toBe('AM')
|
||||
})
|
||||
it('should format %P as lower cased am/pm', function () {
|
||||
expect(t(now, '%P')).toBe('pm')
|
||||
expect(t(now, '%^8P')).toBe(' PM')
|
||||
expect(t(then, '%P')).toBe('am')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Weekday', function () {
|
||||
it('should format %A as Monday', function () {
|
||||
expect(t(now, '%A')).toBe('Monday')
|
||||
expect(t(now, '%^A')).toBe('MONDAY')
|
||||
expect(t(now, '%#A')).toBe('MONDAY')
|
||||
})
|
||||
it('should format %a as Mon', function () {
|
||||
expect(t(now, '%a')).toBe('Mon')
|
||||
expect(t(now, '%^a')).toBe('MON')
|
||||
})
|
||||
it('should format %u as day of week(1-7)', function () {
|
||||
expect(t(now, '%u')).toBe('1')
|
||||
expect(t(then, '%u')).toBe('7')
|
||||
})
|
||||
it('should format %w as day of week(0-7)', function () {
|
||||
expect(t(now, '%w')).toBe('1')
|
||||
expect(t(then, '%w')).toBe('0')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Seconds since the Unix Epoch', () => {
|
||||
it('should format %s as UNIX seconds', function () {
|
||||
expect(t(now, '%s')).toMatch(/\d+/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Week number', () => {
|
||||
it('should format %U as week of year, starts with 0', function () {
|
||||
expect(t(now, '%U')).toBe('01')
|
||||
})
|
||||
it('should format %W as week of year, starts with 1', function () {
|
||||
expect(t(now, '%W')).toBe('01')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Time zone', () => {
|
||||
it('should format %z as time zone', function () {
|
||||
// suppose we're in +8:00
|
||||
const now = new DateWithTimezone('2016-01-04 13:15:23', -480)
|
||||
expect(t(now, '%z')).toBe('+0800')
|
||||
})
|
||||
it('should format %z as negative time zone', function () {
|
||||
// suppose we're in -8:00
|
||||
const date = new DateWithTimezone('2016-01-04T13:15:23.000Z', 480)
|
||||
expect(t(date, '%z')).toBe('-0800')
|
||||
})
|
||||
})
|
||||
|
||||
describe('combination', () => {
|
||||
it('should format %x as local date string', function () {
|
||||
expect(t(now, '%x')).toBe(now.toLocaleDateString())
|
||||
})
|
||||
it('should format %X as local time string', function () {
|
||||
expect(t(now, '%X')).toBe(now.toLocaleTimeString())
|
||||
})
|
||||
it('should format detailed datetime', function () {
|
||||
expect(t(now, '%Y-%m-%d %H:%M:%S')).toBe('2016-01-04 13:15:23')
|
||||
})
|
||||
|
||||
it('should format %c as local string', function () {
|
||||
expect(t(now, '%c')).toBe(now.toLocaleString())
|
||||
})
|
||||
})
|
||||
|
||||
describe('literal strings', () => {
|
||||
it('should escape %% as %', function () {
|
||||
expect(t(now, '%%')).toBe('%')
|
||||
})
|
||||
it('should escape %n as \\n', function () {
|
||||
expect(t(now, '%n')).toBe('\n')
|
||||
})
|
||||
it('should escape %t as \\t', function () {
|
||||
expect(t(now, '%t')).toBe('\t')
|
||||
})
|
||||
it('should retain un-recognized formaters', function () {
|
||||
expect(t(now, '%o')).toBe('%o')
|
||||
})
|
||||
})
|
||||
|
||||
describe('width field', () => {
|
||||
it('should support width field', () => {
|
||||
expect(t(now, '%8Y')).toBe('00002016')
|
||||
})
|
||||
it('should ignore invalid width', () => {
|
||||
expect(t(then, '%1Y')).toBe('2016')
|
||||
expect(t(then, '%1H')).toBe('3')
|
||||
})
|
||||
it('should have higher priority than H', () => {
|
||||
expect(t(then, '%0H')).toBe('03')
|
||||
})
|
||||
})
|
||||
describe('modifier field', () => {
|
||||
it('should ignore E modifier', () => {
|
||||
expect(t(now, '%EY')).toBe('2016')
|
||||
})
|
||||
it('should ignore O modifier', () => {
|
||||
expect(t(now, '%OY')).toBe('2016')
|
||||
})
|
||||
it('should support modifier with width field', () => {
|
||||
expect(t(now, '%8EY')).toBe('00002016')
|
||||
})
|
||||
})
|
||||
describe('flags field', () => {
|
||||
it('should support - flag', () => {
|
||||
expect(t(now, '%-m')).toBe('1')
|
||||
})
|
||||
it('should support _ flag', () => {
|
||||
expect(t(now, '%_m')).toBe(' 1')
|
||||
})
|
||||
it('should support 0 flag', () => {
|
||||
expect(t(now, '%0m')).toBe('01')
|
||||
})
|
||||
it('should support ^ flag', () => {
|
||||
expect(t(now, '%^B')).toBe('JANUARY')
|
||||
})
|
||||
it('should respect to specific conversion', () => {
|
||||
expect(t(now, '%^P')).toBe('PM')
|
||||
expect(t(now, '%P')).toBe('pm')
|
||||
})
|
||||
it('should support # flag', () => {
|
||||
expect(t(now, '%#B')).toBe('JANUARY')
|
||||
expect(t(now, '%#P')).toBe('PM')
|
||||
})
|
||||
it('should support : flag', () => {
|
||||
// suppose we're in +8:00
|
||||
const date = new DateWithTimezone('2016-01-04T13:15:23.000Z', -480)
|
||||
expect(t(date, '%:z')).toBe('+08:00')
|
||||
expect(t(date, '%z')).toBe('+0800')
|
||||
})
|
||||
it('should support multiple flags', () => {
|
||||
expect(t(now, '%^08P')).toBe('000000PM')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,37 @@
|
||||
import { TimezoneDate } from './timezone-date'
|
||||
|
||||
describe('TimezoneDate', () => {
|
||||
it('should respect timezone set to 00:00', () => {
|
||||
const date = new TimezoneDate('2021-10-06T14:26:00.000+08:00', 0)
|
||||
expect(date.getTimezoneOffset()).toBe(0)
|
||||
expect(date.getHours()).toBe(6)
|
||||
expect(date.getMinutes()).toBe(26)
|
||||
})
|
||||
it('should respect timezone set to -06:00', () => {
|
||||
const date = new TimezoneDate('2021-10-06T14:26:00.000+08:00', -360)
|
||||
expect(date.getTimezoneOffset()).toBe(-360)
|
||||
expect(date.getMinutes()).toBe(26)
|
||||
})
|
||||
it('should support Date as argument', () => {
|
||||
const date = new TimezoneDate(new Date('2021-10-06T14:26:00.000+08:00'), 0)
|
||||
expect(date.getHours()).toBe(6)
|
||||
})
|
||||
it('should support .getMilliseconds()', () => {
|
||||
const date = new TimezoneDate('2021-10-06T14:26:00.001+00:00', 0)
|
||||
expect(date.getMilliseconds()).toBe(1)
|
||||
})
|
||||
it('should support .getDay()', () => {
|
||||
const date = new TimezoneDate('2021-12-07T00:00:00.001+08:00', -480)
|
||||
expect(date.getDay()).toBe(2)
|
||||
})
|
||||
it('should support .toLocaleTimeString()', () => {
|
||||
const date = new TimezoneDate('2021-10-06T00:00:00.001+00:00', -480)
|
||||
expect(date.toLocaleTimeString('en-US')).toBe('8:00:00 AM')
|
||||
expect(() => date.toLocaleDateString()).not.toThrow()
|
||||
})
|
||||
it('should support .toLocaleDateString()', () => {
|
||||
const date = new TimezoneDate('2021-10-06T22:00:00.001+00:00', -480)
|
||||
expect(date.toLocaleDateString('en-US')).toBe('10/7/2021')
|
||||
expect(() => date.toLocaleDateString()).not.toThrow()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,122 @@
|
||||
import * as _ from './underscore'
|
||||
|
||||
describe('util/underscore', function () {
|
||||
describe('.isString()', function () {
|
||||
it('should return true for literal string', function () {
|
||||
expect(_.isString('foo')).toBeTruthy()
|
||||
})
|
||||
it('should return true String instance', function () {
|
||||
expect(_.isString(String('foo'))).toBeTruthy()
|
||||
})
|
||||
it('should return false for 123 ', function () {
|
||||
expect(_.isString(123)).toBeFalsy()
|
||||
})
|
||||
})
|
||||
describe('.isNumber()', function () {
|
||||
it('should return false for "foo"', function () {
|
||||
expect(_.isNumber('foo')).toBeFalsy()
|
||||
})
|
||||
it('should return true for 0', function () {
|
||||
expect(_.isNumber(0)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
describe('.stringify()', function () {
|
||||
it('should return "" for null', function () {
|
||||
expect(_.stringify(null)).toBe('')
|
||||
})
|
||||
it('should return "" for undefined', function () {
|
||||
expect(_.stringify(undefined)).toBe('')
|
||||
})
|
||||
it('should return regex string for RegExp', function () {
|
||||
const reg = /foo/g
|
||||
expect(_.stringify(reg)).toBe('/foo/g')
|
||||
})
|
||||
it('should return locale string for date', function () {
|
||||
const date = new Date('2018-10-01T14:51:00.000Z')
|
||||
// Mon Oct 01 2018 22:51:00 GMT+0800 (CST)
|
||||
expect(_.stringify(date)).toBe(date.toString())
|
||||
})
|
||||
})
|
||||
describe('.forOwn()', function () {
|
||||
it('should iterate all properties', function () {
|
||||
const spy = jest.fn()
|
||||
const obj = {
|
||||
foo: 'bar'
|
||||
}
|
||||
_.forOwn(obj, spy)
|
||||
expect(spy).toHaveBeenCalledWith('bar', 'foo', obj)
|
||||
})
|
||||
it('should default to empty object', function () {
|
||||
const spy = jest.fn()
|
||||
_.forOwn(undefined, spy)
|
||||
expect(spy).not.toHaveBeenCalled()
|
||||
})
|
||||
it('should not iterate over properties on prototype', function () {
|
||||
const spy = jest.fn()
|
||||
const obj = Object.create({
|
||||
bar: 'foo'
|
||||
})
|
||||
obj.foo = 'bar'
|
||||
_.forOwn(obj, spy)
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
expect(spy).toHaveBeenCalledWith('bar', 'foo', obj)
|
||||
})
|
||||
it('should break when returned false', function () {
|
||||
const spy = jest.fn(() => false)
|
||||
_.forOwn({
|
||||
'foo': 'foo',
|
||||
'bar': 'foo'
|
||||
}, spy)
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
describe('.range()', function () {
|
||||
it('should return a range of integers', function () {
|
||||
expect(_.range(3, 5)).toEqual([3, 4])
|
||||
})
|
||||
})
|
||||
describe('.isObject()', function () {
|
||||
it('should return true for function', function () {
|
||||
expect(_.isObject((x: any) => x)).toBeTruthy()
|
||||
})
|
||||
it('should return true for plain object', function () {
|
||||
expect(_.isObject({})).toBeTruthy()
|
||||
})
|
||||
it('should return false for null', function () {
|
||||
expect(_.isObject(null)).toBeFalsy()
|
||||
})
|
||||
it('should return false for number', function () {
|
||||
expect(_.isObject(2)).toBeFalsy()
|
||||
})
|
||||
})
|
||||
describe('.padEnd()', function () {
|
||||
it('should default ch to " "', () => {
|
||||
expect(_.padEnd('foo', 5)).toBe('foo ')
|
||||
})
|
||||
})
|
||||
describe('.changeCase()', function () {
|
||||
it('should to upper case if there is one lowercase', () => {
|
||||
expect(_.changeCase('fooA')).toBe('FOOA')
|
||||
})
|
||||
it('should to lower case if all upper case', () => {
|
||||
expect(_.changeCase('FOOA')).toBe('fooa')
|
||||
})
|
||||
})
|
||||
describe('.caseInsensitiveCompare()', function () {
|
||||
it('should "foo" > "bar"', () => {
|
||||
expect(_.caseInsensitiveCompare('foo', 'bar')).toBe(1)
|
||||
})
|
||||
it('should "foo" < null', () => {
|
||||
expect(_.caseInsensitiveCompare('foo', null)).toBe(-1)
|
||||
})
|
||||
it('should null > "foo"', () => {
|
||||
expect(_.caseInsensitiveCompare(null, 'foo')).toBe(1)
|
||||
})
|
||||
it('should -1 < 0', () => {
|
||||
expect(_.caseInsensitiveCompare(-1, 0)).toBe(-1)
|
||||
})
|
||||
it('should 1 > 0', () => {
|
||||
expect(_.caseInsensitiveCompare(1, 0)).toBe(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user