feature: root option allows array of strings

This commit is contained in:
harttle
2016-10-31 23:21:28 +08:00
parent 377c805230
commit 6dd836f157
18 changed files with 313 additions and 106 deletions
+61
View File
@@ -0,0 +1,61 @@
const chai = require("chai");
const expect = chai.expect;
const mock = require('mock-fs');
const request = require('supertest');
const express = require('express');
const Liquid = require('..');
describe('engine#express()', function() {
var app, engine;
before(function() {
mock({
'/root/foo.html': 'foo',
'/views/name.html': 'My name is {{name}}.',
'/views/include.html': '{% include file %}',
'/partials/bar.html': 'bar'
});
app = express();
engine = Liquid({
root: '/root',
extname: '.html'
});
app.set('views', ['/views', '/partials']);
app.set('view engine', 'html');
app.engine('html', engine.express());
app.get('/name', function(req, res) {
res.render('name', {
name: 'harttle'
});
});
app.get('/include/:file', function(req, res) {
res.render('include', {
file: req.params.file
});
});
});
it('should render templates', function(done) {
request(app).get('/name')
.expect('My name is harttle.')
.expect(200, done);
});
it('should pass error when file not found', function(done) {
var view = {
root: []
};
var file = '/not-exist.html';
var ctx = {};
engine.express().call(view, file, ctx, function(err) {
expect(err.code).to.equal('ENOENT');
console.log(err.message);
done();
});
});
it('should respect root option when lookup', function(done) {
request(app).get('/include/foo')
.expect('foo')
.expect(200, done);
});
});
+33 -36
View File
@@ -1,10 +1,9 @@
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const should = chai.should();
const expect = chai.expect;
const Liquid = require('..');
const mock = require('mock-fs');
chai.use(chaiAsPromised);
chai.use(require("chai-as-promised"));
describe('liquid', function() {
var engine, ctx;
@@ -28,24 +27,26 @@ describe('liquid', function() {
afterEach(function() {
mock.restore();
});
it('should output object', function() {
return engine.parseAndRender('{{obj}}', ctx).should.eventually.equal('{"foo":"bar"}');
});
it('should output array', function() {
return engine.parseAndRender('{{arr}}', ctx).should.eventually.equal('[-2,"a"]');
});
it('should output undefined to empty', function() {
return engine.parseAndRender('foo{{zzz}}bar', ctx).should.eventually.equal('foobar');
});
it('should render as null when filter undefined', function() {
return engine.parseAndRender('{{arr | filter1}}', ctx).should.eventually.equal('');
});
it('should throw upon undefined filter when strict_filters set', function() {
var opts = {
strict_filters: true
};
return expect(engine.parseAndRender('{{arr | filter1}}', ctx, opts)).to
.be.rejectedWith(/undefined filter: filter1/);
describe('{{output}}', function() {
it('should output object', function() {
return engine.parseAndRender('{{obj}}', ctx).should.eventually.equal('{"foo":"bar"}');
});
it('should output array', function() {
return engine.parseAndRender('{{arr}}', ctx).should.eventually.equal('[-2,"a"]');
});
it('should output undefined to empty', function() {
return engine.parseAndRender('foo{{zzz}}bar', ctx).should.eventually.equal('foobar');
});
it('should render as null when filter undefined', function() {
return engine.parseAndRender('{{arr | filter1}}', ctx).should.eventually.equal('');
});
it('should throw upon undefined filter when strict_filters set', function() {
var opts = {
strict_filters: true
};
return expect(engine.parseAndRender('{{arr | filter1}}', ctx, opts)).to
.be.rejectedWith(/undefined filter: filter1/);
});
});
it('should parse html', function() {
(function() {
@@ -77,10 +78,20 @@ describe('liquid', function() {
});
describe('#renderFile()', function() {
it('should render file', function() {
return engine.renderFile('/root/files/foo.html', ctx).should.eventually.equal('foo');
return expect(engine.renderFile('/root/files/foo.html', ctx))
.to.eventually.equal('foo');
});
it('should accept relative path', function() {
return expect(engine.renderFile('files/foo.html')).to.eventually.equal('foo');
return expect(engine.renderFile('files/foo.html'))
.to.eventually.equal('foo');
});
it('should resolve array as root', function(){
engine = Liquid({
root: ['/boo', '/root/'],
extname: '.html'
});
return expect(engine.renderFile('files/foo.html'))
.to.eventually.equal('foo');
});
it('should render file with context', function() {
return engine.renderFile('/root/files/name.html', ctx).should.eventually.equal('My name is harttle.');
@@ -102,19 +113,6 @@ describe('liquid', function() {
return expect(engine.renderFile('files/foo/../foo.html')).to.eventually.equal('foo');
});
});
describe('#express()', function() {
it('should render templates', function() {
engine.express()('/root/files/name.html', ctx, function(err, html) {
expect(err).to.equal(null);
expect(html).to.equal('My name is harttle.');
});
});
it('should pass error when file not found', function() {
engine.express()('/root/files/name1.html', ctx, function(err, html) {
expect(err.code).to.equal('ENOENT');
});
});
});
describe('strict', function() {
it('should not throw when strict_variables false (default)', function() {
return expect(engine.parseAndRender('before{{notdefined}}after', ctx)).to
@@ -163,7 +161,6 @@ describe('liquid', function() {
.then((result) => {
return expect(result).to.equal('foo');
});
});
});
});
+51
View File
@@ -0,0 +1,51 @@
const chai = require("chai");
const sinon = require('sinon');
const expect = chai.expect;
chai.use(require("chai-as-promised"));
chai.use(require("sinon-chai"));
var P = require('../../src/util/promise.js');
describe('util/promise', function() {
describe('.someSeries()', function() {
it('should resolve in series', function() {
var spy1 = sinon.spy(),
spy2 = sinon.spy();
return P
.someSeries(
['first', 'second'],
(item, idx) => new Promise(function(resolve, reject) {
if (idx === 0) {
setTimeout(function() {
spy1();
reject(new Error('first cb'));
}, 10);
} else {
spy2();
resolve('foo');
}
}))
.then(() => expect(spy2).to.have.been.calledAfter(spy1));
});
it('should reject when all rejected', function() {
var p = P.someSeries(['first', 'second', 'third'],
item => Promise.reject(new Error(item)));
return expect(p).to.be.rejectedWith("third");
});
it('should resolve the value that first callback resolved', () => {
var p = P.someSeries(['first', 'second'],
item => Promise.resolve(item));
return expect(p).to.eventually.equal('first');
});
it('should not call rest of callbacks once resolved', () => {
var spy = sinon.spy();
return P.someSeries(['first', 'second'], (item, idx) => {
if (idx > 0) {
spy();
}
return Promise.resolve(item);
})
.then(() => expect(spy).to.not.have.been.called);
});
});
});
+8
View File
@@ -45,4 +45,12 @@ describe('util/underscore', function() {
expect(spy).to.have.been.calledOnce;
});
});
describe('.isArray()', function() {
it('should return true for []', function() {
expect(_.isArray([])).to.be.true;
});
it('should return false for "foo"', function() {
expect(_.isArray("foo")).to.be.false;
});
});
});