Making good progress on async

This commit is contained in:
Tim Hardy
2016-09-25 00:13:29 -05:00
parent 87e284159d
commit bb8468708a
8 changed files with 255 additions and 233 deletions
+50 -30
View File
@@ -1,8 +1,10 @@
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const should = chai.should();
const expect = chai.expect;
const should = chai.should;
const Liquid = require('..');
const mock = require('mock-fs');
chai.use(chaiAsPromised);
describe('liquid', function() {
var engine, ctx;
@@ -27,13 +29,13 @@ describe('liquid', function() {
mock.restore();
});
it('should output object', function() {
engine.parseAndRender('{{obj}}', ctx).should.equal('{"foo":"bar"}');
return engine.parseAndRender('{{obj}}', ctx).should.eventually.equal('{"foo":"bar"}');
});
it('should output array', function() {
engine.parseAndRender('{{arr}}', ctx).should.equal('[-2,"a"]');
return engine.parseAndRender('{{arr}}', ctx).should.eventually.equal('[-2,"a"]');
});
it('should output undefined to empty', function() {
engine.parseAndRender('foo{{zzz}}bar', ctx).should.equal('foobar');
return engine.parseAndRender('foo{{zzz}}bar', ctx).should.eventually.equal('foobar');
});
it('should parse html', function() {
(function() {
@@ -45,46 +47,55 @@ describe('liquid', function() {
});
it('should render template multiple times', function() {
var template = engine.parse('{{obj}}');
engine.render(template, ctx).should.equal('{"foo":"bar"}');
engine.render(template, ctx).should.equal('{"foo":"bar"}');
return engine.render(template, ctx)
.then((result) => {
expect(result).to.equal('{"foo":"bar"}');
return engine.render(template, ctx);
})
.then((result) => {
return expect(result).to.equal('{"foo":"bar"}');
});
// engine.render(template, ctx).should.equal('{"foo":"bar"}');
// engine.render(template, ctx).should.equal('{"foo":"bar"}');
});
it('should render filters', function() {
var template = engine.parse('<p>{{arr | join: "_"}}</p>');
engine.render(template, ctx).should.equal('<p>-2_a</p>');
return engine.render(template, ctx).should.eventually.equal('<p>-2_a</p>');
});
describe('#renderFile()', function(){
it('should render file', function() {
engine.renderFile('/root/files/foo.html', ctx).should.equal('foo');
return engine.renderFile('/root/files/foo.html', ctx).should.eventually.equal('foo');
});
it('should render file relative to root', function() {
engine.renderFile('files/foo.html', ctx).should.equal('foo');
return engine.renderFile('files/foo.html', ctx).should.eventually.equal('foo');
});
it('should render file with context', function() {
engine.renderFile('/root/files/name.html', ctx).should.equal('My name is harttle.');
return engine.renderFile('/root/files/name.html', ctx).should.eventually.equal('My name is harttle.');
});
it('should render file with default extname', function() {
engine.renderFile('files/name', ctx).should.equal('My name is harttle.');
});
});
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');
});
return engine.renderFile('files/name', ctx).should.eventually.equal('My name is harttle.');
});
});
// todo: make these async
// 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('cache', function() {
it('should be disabled by default', function() {
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('bar');
return engine.renderFile('files/foo', ctx).should.eventually.equal('bar');
});
it('should respect cache=true option', function() {
engine = Liquid({
@@ -92,11 +103,20 @@ describe('liquid', function() {
extname: '.html',
cache: true
});
engine.renderFile('files/foo', ctx).should.equal('foo');
mock({
'/root/files/foo.html': 'bar'
});
engine.renderFile('files/foo', ctx).should.equal('foo');
return engine.renderFile('files/foo', ctx)
.then((result) => {
return expect(result).to.equal('foo');
})
.then((result) => {
mock({
'/root/files/foo.html': 'bar'
});
return engine.renderFile('files/foo', ctx);
})
.then((result) => {
return expect(result).to.equal('foo');
});
});
});
});