chore(TypeScript): fix linting and generate .d.ts

This commit is contained in:
harttle
2019-02-17 18:20:04 +08:00
parent 7ee87008c7
commit fc9ebdb90f
79 changed files with 809 additions and 820 deletions
+3 -6
View File
@@ -1,12 +1,9 @@
var chai = require('chai')
var Liquid = require('../..')
var expect = chai.expect
chai.use(require('chai-as-promised'))
import { expect } from 'chai'
import Liquid from '../..'
describe('.evalValue()', function () {
var engine
beforeEach(() => engine = new Liquid())
beforeEach(() => { engine = new Liquid() })
it('should throw when scope undefined', function () {
expect(() => engine.evalValue('{{"foo"}}')).to.throw(/scope undefined/)
+1 -5
View File
@@ -1,12 +1,8 @@
import * as chai from 'chai'
import { expect } from 'chai'
import * as request from 'supertest'
import * as express from 'express'
import * as mock from 'mock-fs'
import Liquid from '../../dist/liquid.common.js'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
describe('express()', function () {
var app, engine
+29 -23
View File
@@ -1,8 +1,8 @@
var chai = require('chai')
var Liquid = require('../..')
var expect = chai.expect
import Liquid from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
chai.use(require('chai-as-promised'))
use(chaiAsPromised)
describe('.parseAndRender()', function () {
var engine, strictEngine
@@ -12,19 +12,23 @@ describe('.parseAndRender()', function () {
strict_filters: true
})
})
it('should stringify object', function () {
it('should stringify object', async function () {
var ctx = { obj: { foo: 'bar' } }
return expect(engine.parseAndRender('{{obj}}', ctx)).to.eventually.equal('{"foo":"bar"}')
const html = await engine.parseAndRender('{{obj}}', ctx)
return expect(html).to.equal('{"foo":"bar"}')
})
it('should stringify array ', function () {
it('should stringify array ', async function () {
var ctx = { arr: [-2, 'a'] }
return expect(engine.parseAndRender('{{arr}}', ctx)).to.eventually.equal('[-2,"a"]')
const html = await engine.parseAndRender('{{arr}}', ctx)
return expect(html).to.equal('[-2,"a"]')
})
it('should render undefined as empty', function () {
return expect(engine.parseAndRender('foo{{zzz}}bar', {})).to.eventually.equal('foobar')
it('should render undefined as empty', async function () {
const html = await engine.parseAndRender('foo{{zzz}}bar', {})
return expect(html).to.equal('foobar')
})
it('should render as null when filter undefined', function () {
return expect(engine.parseAndRender('{{"foo" | filter1}}', {})).to.eventually.equal('foo')
it('should render as null when filter undefined', async function () {
const html = await engine.parseAndRender('{{"foo" | filter1}}', {})
return expect(html).to.equal('foo')
})
it('should throw upon undefined filter when strict_filters set', function () {
return expect(strictEngine.parseAndRender('{{"foo" | filter1}}', {})).to
@@ -38,22 +42,24 @@ describe('.parseAndRender()', function () {
engine.parse('<html><head>{{obj}}</head></html>')
}).to.not.throw()
})
it('should render template multiple times', function () {
var ctx = { obj: { foo: 'bar' } }
var template = engine.parse('{{obj}}')
return engine.render(template, ctx)
.then(result => expect(result).to.equal('{"foo":"bar"}'))
.then(() => engine.render(template, ctx))
.then((result) => expect(result).to.equal('{"foo":"bar"}'))
it('should render template multiple times', async function () {
const ctx = { obj: { foo: 'bar' } }
const template = engine.parse('{{obj}}')
const result = await engine.render(template, ctx)
expect(result).to.equal('{"foo":"bar"}')
const result2 = await engine.render(template, ctx)
expect(result2).to.equal('{"foo":"bar"}')
})
it('should render filters', function () {
it('should render filters', async function () {
var ctx = { names: ['alice', 'bob'] }
var template = engine.parse('<p>{{names | join: ","}}</p>')
return expect(engine.render(template, ctx)).to.eventually.equal('<p>alice,bob</p>')
const html = await engine.render(template, ctx)
return expect(html).to.equal('<p>alice,bob</p>')
})
it('should render accessive filters', function () {
it('should render accessive filters', async function () {
var src = '{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}' +
'{{ my_array | first }}'
return expect(engine.parseAndRender(src)).to.eventually.equal('apples')
const html = await engine.parseAndRender(src)
return expect(html).to.equal('apples')
})
})
+24 -26
View File
@@ -1,9 +1,6 @@
var chai = require('chai')
var mock = require('mock-fs')
var Liquid = require('../..')
var expect = chai.expect
chai.use(require('chai-as-promised'))
import { expect } from 'chai'
import * as mock from 'mock-fs'
import Liquid from '../..'
describe('#renderFile()', function () {
var engine
@@ -24,28 +21,28 @@ describe('#renderFile()', function () {
afterEach(function () {
mock.restore()
})
it('should render file', function () {
return expect(engine.renderFile('/root/files/foo.html', {}))
.to.eventually.equal('foo')
it('should render file', async function () {
const html = await engine.renderFile('/root/files/foo.html', {})
return expect(html).to.equal('foo')
})
it('should find files without extname', function () {
it('should find files without extname', async function () {
var engine = new Liquid({ root: '/root' })
return expect(engine.renderFile('/root/files/bar', {}))
.to.eventually.equal('bar')
const html = await engine.renderFile('/root/files/bar', {})
return expect(html).to.equal('bar')
})
it('should accept relative path', function () {
return expect(engine.renderFile('files/foo.html'))
.to.eventually.equal('foo')
it('should accept relative path', async function () {
const html = await engine.renderFile('files/foo.html')
return expect(html).to.equal('foo')
})
it('should resolve array as root', function () {
it('should resolve array as root', async function () {
engine = new Liquid({
root: ['/boo', '/root/'],
extname: '.html'
})
return expect(engine.renderFile('files/foo.html'))
.to.eventually.equal('foo')
const html = await engine.renderFile('files/foo.html')
return expect(html).to.equal('foo')
})
it('should default root to cwd', function () {
it('should default root to cwd', async function () {
var files = {}
files[process.cwd() + '/foo.html'] = 'FOO'
mock(files)
@@ -53,15 +50,16 @@ describe('#renderFile()', function () {
engine = new Liquid({
extname: '.html'
})
return expect(engine.renderFile('foo.html'))
.to.eventually.equal('FOO')
const html = await engine.renderFile('foo.html')
return expect(html).to.equal('FOO')
})
it('should render file with context', function () {
return expect(engine.renderFile('/root/files/name.html', { name: 'harttle' }))
.to.eventually.equal('My name is harttle.')
it('should render file with context', async function () {
const html = await engine.renderFile('/root/files/name.html', { name: 'harttle' })
return expect(html).to.equal('My name is harttle.')
})
it('should use default extname', function () {
return expect(engine.renderFile('files/name', { name: 'harttle' })).to.eventually.equal('My name is harttle.')
it('should use default extname', async function () {
const html = await engine.renderFile('files/name', { name: 'harttle' })
return expect(html).to.equal('My name is harttle.')
})
it('should throw with lookup list when file not exist', function () {
engine = new Liquid({
+5 -5
View File
@@ -1,10 +1,7 @@
import * as chai from 'chai'
import { expect } from 'chai'
import Liquid from '../..'
import * as chaiAsPromised from 'chai-as-promised'
const liquid = new Liquid()
const expect = chai.expect
chai.use(chaiAsPromised)
const cases = [
{
@@ -445,6 +442,9 @@ const cases = [
describe('Whitespace Control', function () {
cases.forEach(item => it(
item.text,
() => expect(liquid.parseAndRender(item.text)).to.eventually.equal(item.expected)
async () => {
const html = await liquid.parseAndRender(item.text)
expect(html).to.equal(item.expected)
}
))
})
+38 -42
View File
@@ -1,11 +1,10 @@
import Liquid from '../../dist/liquid.js'
import { createFakeServer, useFakeXMLHttpRequest } from 'sinon'
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { expect, use } from 'chai'
import { JSDOM } from 'jsdom'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
use(chaiAsPromised)
describe('xhr', () => {
if (+process.version.match(/^v(\d+)/)[1] < 8) {
@@ -24,7 +23,7 @@ describe('xhr', () => {
includeNodeLocations: true
});
(global as any).XMLHttpRequest = useFakeXMLHttpRequest();
(global as any).document = dom.window.document;
(global as any).document = dom.window.document
engine = new Liquid({
root: 'https://example.com/views/',
extname: '.html'
@@ -36,97 +35,94 @@ describe('xhr', () => {
delete (global as any).document
})
describe('#renderFile()', () => {
it('should support without extname', () => {
return expect(engine.renderFile('hello', { name: 'alice1' }))
.to.eventually.equal('hello alice1')
it('should support without extname', async () => {
const html = await engine.renderFile('hello', { name: 'alice1' })
return expect(html).to.equal('hello alice1')
})
it('should support with extname', () => {
return expect(engine.renderFile('hello.html', { name: 'alice2' }))
.to.eventually.equal('hello alice2')
it('should support with extname', async () => {
const html = await engine.renderFile('hello.html', { name: 'alice2' })
return expect(html).to.equal('hello alice2')
})
it('should support with absolute path', () => {
it('should support with absolute path', async () => {
server.respondWith('GET', 'https://example.com/foo.html',
[200, { 'Content-Type': 'text/plain' }, 'foo'])
return expect(engine.renderFile('/foo.html'))
.to.eventually.equal('foo')
const html = await engine.renderFile('/foo.html')
return expect(html).to.equal('foo')
})
it('should support with url', () => {
return expect(engine.renderFile('https://example.com/views/hello.html', { name: 'alice4' }))
.to.eventually.equal('hello alice4')
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')
})
it('should support include', () => {
it('should support include', async () => {
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, "hello {% include 'name.html' %}"])
server.respondWith('GET', 'https://example.com/views/name.html',
[200, { 'Content-Type': 'text/plain' }, '{{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should throw 404', () => {
return expect(engine.renderFile('/not/exist.html'))
.to.be.rejectedWith('Not Found')
})
it('should throw error', function (done) {
engine.renderFile('hello.html')
.then(() => done('should not be resolved'))
.catch(function (e) {
expect(e.message).to.equal('An error occurred whilst receiving the response.')
done()
});
it('should throw error', function () {
const result = expect(engine.renderFile('hello.html'))
.to.be.rejectedWith('An error occurred whilst receiving the response.');
(global as any).XMLHttpRequest.onCreate = function (request) {
setTimeout(() => request.error())
}
return result
})
})
describe('#renderFile() with root specified', () => {
it('should support undefined root', () => {
it('should support undefined root', async () => {
engine = new Liquid({
extname: '.html'
})
server.respondWith('GET', 'https://example.com/foo/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support empty root', () => {
it('should support empty root', async () => {
engine = new Liquid({
root: '',
extname: '.html'
})
server.respondWith('https://example.com/foo/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support with relative path', () => {
it('should support with relative path', async () => {
engine = new Liquid({
root: './views/',
extname: '.html'
})
server.respondWith('GET', 'https://example.com/foo/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support with absolute path', () => {
it('should support with absolute path', async () => {
engine = new Liquid({
root: '/views/',
extname: '.html'
})
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
it('should support with url', () => {
it('should support with url', async () => {
engine = new Liquid({
root: 'https://foo.com/bar/',
extname: '.html'
})
server.respondWith('GET', 'https://foo.com/bar/hello.html',
[200, { 'Content-Type': 'text/plain' }, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', { name: 'alice5' }))
.to.eventually.equal('hello alice5')
const html = await engine.renderFile('hello.html', { name: 'alice5' })
return expect(html).to.equal('hello alice5')
})
})
describe('cache options', () => {