test: cover all

This commit is contained in:
harttle
2018-08-27 23:27:42 +08:00
parent d6876bd9ec
commit 5378f2709b
9 changed files with 351 additions and 1133 deletions
+3
View File
@@ -5,4 +5,7 @@ node_js:
before_script:
- npm install -g mocha
after_script:
- npm run test
- npm run e2e
after_success:
- npm run coveralls
+222 -1118
View File
File diff suppressed because it is too large Load Diff
+5 -5
View File
@@ -7,10 +7,10 @@
"browser": "dist/liquid.js",
"scripts": {
"lint": "eslint src/ test/ *.js",
"test": "mocha test/unit",
"e2e": "mocha test/e2e",
"coverage": "cross-env NODE_ENV=test nyc report --reporter=html mocha test/unit",
"coveralls": "cross-env NODE_ENV=test nyc report --reporter=text-lcov mocha test/unit | coveralls",
"dev": "mocha test/unit",
"test": "cross-env NODE_ENV=test nyc --reporter=html mocha test/unit",
"e2e": "npm run dist && mocha test/e2e",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"dist": "rollup -c && ls -lh dist",
"demo:browser": "echo open http://localhost:8080/demo/browser && http-server -c-1",
"demo:nodejs": "node ./demo/nodejs/index.js",
@@ -62,7 +62,7 @@
"jsdom": "^11.5.1",
"mocha": "^5.2.0",
"mock-fs": "^4.4.1",
"nyc": "^12.0.2",
"nyc": "^13.0.1",
"regenerator-runtime": "^0.12.1",
"rollup": "^0.64.1",
"rollup-plugin-alias": "^1.4.0",
+1 -1
View File
@@ -90,7 +90,7 @@ const _engine = {
function normalizeStringArray (value) {
if (_.isArray(value)) return value
if (_.isString(value)) return [value]
return []
throw new TypeError('illegal root: ' + value)
}
export default function Liquid (options) {
+5 -9
View File
@@ -3,10 +3,13 @@ import path from 'path'
import {anySeries} from './util/promise.js'
import {statFileAsync, readFileAsync} from './util/fs.js'
function lookup (filepath, root, options) {
export async function resolve (filepath, root, options) {
if (!path.extname(filepath)) {
filepath += options.extname
}
root = options.root.concat(root || [])
root = _.uniq(root)
const paths = root.map(root => path.resolve(root || location.href, filepath))
const paths = root.map(root => path.resolve(root, filepath))
return anySeries(paths, async path => {
try {
await statFileAsync(path)
@@ -18,13 +21,6 @@ function lookup (filepath, root, options) {
})
}
export async function resolve (filepath, root, options) {
if (!path.extname(filepath)) {
filepath += options.extname
}
return lookup(filepath, root, options)
}
export async function read (filepath) {
return readFileAsync(filepath)
}
+4
View File
@@ -89,4 +89,8 @@ describe('filter', function () {
expect(f.name).to.equal('foo')
expect(f.args).to.deep.equal(['\'a\'', '"a"'])
})
it('should not throw undefined filter by default', function () {
expect(filter.construct('undefined').render('foo', scope)).to.equal('foo')
})
})
+41
View File
@@ -0,0 +1,41 @@
import Liquid from '../../src/index.js'
import mock from 'mock-fs'
import chai from 'chai'
const expect = chai.expect
describe('Liquid', function () {
describe('#constructor()', function () {
it('should throw on illegal root', function () {
expect(() => {
new Liquid({root: {}}) // eslint-disable-line
}).to.throw(/illegal root/)
})
})
describe('#express()', function () {
const liquid = new Liquid({root: '/root'})
const render = liquid.express()
before(function () {
mock({
'/root/foo': 'foo'
})
})
after(function () {
mock.restore()
})
it('should render single template', function (done) {
render.call({root: '.'}, 'foo', null, (err, result) => {
if (err) return done(err)
expect(result).to.equal('foo')
done()
})
})
it('should render single template with Array-typed root', function (done) {
render.call({root: ['.']}, 'foo', null, (err, result) => {
if (err) return done(err)
expect(result).to.equal('foo')
done()
})
})
})
})
+25
View File
@@ -0,0 +1,25 @@
import {resolve} from '../../src/template.js'
import mock from 'mock-fs'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(chaiAsPromised)
describe('template', function () {
before(function () {
mock({
'/foo/bar.html': 'bar'
})
})
describe('#resolve()', function () {
it('should resolve based on root', function () {
const filepath = resolve('bar.html', '/foo', {root: []})
return expect(filepath).to.eventually.equal('/foo/bar.html')
})
it('should resolve based on root', function () {
return expect(resolve('foo.html', '/foo', {root: []}))
.to.rejectedWith(/Failed to lookup foo.html in: \/foo/)
})
})
})
+45
View File
@@ -0,0 +1,45 @@
import {read} from '../../src/template-browser.js'
import sinon from 'sinon'
import chai from 'chai'
chai.use(require('chai-as-promised'))
const expect = chai.expect
describe('template-browser', () => {
if (process.version.match(/^v(\d+)/)[1] < 8) {
console.info('jsdom not supported, skipping xhr...')
return
}
let server
beforeEach(() => {
server = sinon.createFakeServer()
server.autoRespond = true
server.respondWith('GET', 'https://example.com/views/hello.html',
[200, {'Content-Type': 'text/plain'}, 'hello {{name}}'])
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest()
})
afterEach(() => {
server.restore()
delete global.XMLHttpRequest
})
describe('#read()', () => {
it('should get corresponding text', () => {
return expect(read('https://example.com/views/hello.html'))
.to.eventually.equal('hello {{name}}')
})
it('should throw 404', () => {
return expect(read('https://example.com/not/exist.html'))
.to.be.rejectedWith('Not Found')
})
it('should throw error', function (done) {
read('https://example.com/views/hello.html')
.then(() => done('should not be resolved'))
.catch(function (e) {
expect(e.message).to.equal('An error occurred whilst receiving the response.')
done()
})
server.requests[0].error()
})
})
})