From 925df9a9b5f3e01fcf4fceb4d2850c183828a995 Mon Sep 17 00:00:00 2001 From: ChenL Date: Fri, 12 Jan 2018 10:22:22 +0800 Subject: [PATCH] 100% coverage --- index.js | 2 +- test/util/url.js | 2 +- test/xhr.js | 84 ++++++++++++++++++++++++++---------------------- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/index.js b/index.js index 897f28496..20fe1c917 100644 --- a/index.js +++ b/index.js @@ -128,7 +128,7 @@ var _engine = { } } xhr.onerror = () => { - reject(new Error(xhr.statusText)) + reject(new Error('An error occurred whilst sending the response.')) } xhr.open('GET', fullUrl) xhr.send() diff --git a/test/util/url.js b/test/util/url.js index 47eed7cad..bf3ac742e 100644 --- a/test/util/url.js +++ b/test/util/url.js @@ -4,7 +4,7 @@ const url = require('../../src/util/url.js') describe('util/url', function () { if (process.version.match(/^v(\d+)/)[1] < 8) { - return; + return } const JSDOM = require('jsdom').JSDOM var dom diff --git a/test/xhr.js b/test/xhr.js index 07bb69138..2d8e37d2a 100644 --- a/test/xhr.js +++ b/test/xhr.js @@ -5,35 +5,32 @@ const expect = chai.expect describe('xhr', () => { if (process.version.match(/^v(\d+)/)[1] < 8) { - return; + return } const JSDOM = require('jsdom').JSDOM var server, engine, dom beforeEach(() => { - server = sinon.fakeServer.create() + 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() - dom = new JSDOM(``, { - url: 'https://example.com/', - referrer: 'https://example.com/', + dom = new JSDOM('', { + url: 'https://example.com/foo/bar.html', contentType: 'text/html', includeNodeLocations: true }) + global.XMLHttpRequest = sinon.useFakeXMLHttpRequest() global.document = dom.window.document engine = Liquid({ - root: 'https://example.com/views', + root: 'https://example.com/views/', extname: '.html' }) }) - afterEach(() => { server.restore() delete global.XMLHttpRequest delete global.document }) - describe('#renderFile()', () => { it('should support without extname', () => { return expect(engine.renderFile('hello', {name: 'alice1'})) @@ -65,32 +62,26 @@ describe('xhr', () => { return expect(engine.renderFile('/not/exist.html')) .to.be.rejectedWith('Not Found') }) + it('should throw error', function (done) { + engine.renderFile('hello.html') + .catch(function (e) { + expect(e.message).to.equal('An error occurred whilst sending the response.') + done() + }) + server.requests[0].error() + }) }) describe('root', () => { it('should support with null', () => { - dom = new JSDOM(``, { - url: 'https://example.com/bar/', - referrer: 'https://example.com/bar/', - contentType: 'text/html', - includeNodeLocations: true - }) - global.document = dom.window.document engine = Liquid({ extname: '.html' }) - server.respondWith('GET', 'https://example.com/bar/hello.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') }) it('should support with empty', () => { - dom = new JSDOM(``, { - url: 'https://example.com/foo/', - referrer: 'https://example.com/foo/', - contentType: 'text/html', - includeNodeLocations: true - }) - global.document = dom.window.document engine = Liquid({ root: '', extname: '.html' @@ -101,13 +92,6 @@ describe('xhr', () => { .to.eventually.equal('hello alice5') }) it('should support with relative path', () => { - dom = new JSDOM(``, { - url: 'https://example.com/foo/', - referrer: 'https://example.com/foo/', - contentType: 'text/html', - includeNodeLocations: true - }) - global.document = dom.window.document engine = Liquid({ root: './views/', extname: '.html' @@ -118,13 +102,6 @@ describe('xhr', () => { .to.eventually.equal('hello alice5') }) it('should support with absolute path', () => { - dom = new JSDOM(``, { - url: 'https://example.com/foo/', - referrer: 'https://example.com/foo/', - contentType: 'text/html', - includeNodeLocations: true - }) - global.document = dom.window.document engine = Liquid({ root: '/views/', extname: '.html' @@ -145,4 +122,35 @@ describe('xhr', () => { .to.eventually.equal('hello alice5') }) }) + describe('cache options', () => { + it('should be disabled by default', () => { + server.respondWith('GET', 'https://example.com/views/foo.html', + [200, {'Content-Type': 'text/plain'}, 'foo1']) + return engine.renderFile('foo.html') + .then((html) => { + expect(html).to.equal('foo1') + server.respondWith('GET', 'https://example.com/views/foo.html', + [200, {'Content-Type': 'text/plain'}, 'foo2']) + return engine.renderFile('foo.html') + }) + .then(html => expect(html).to.equal('foo2')) + }) + it('should respect cache=true option', () => { + engine = Liquid({ + root: '/views/', + extname: '.html', + cache: true + }) + server.respondWith('GET', 'https://example.com/views/foo.html', + [200, {'Content-Type': 'text/plain'}, 'foo1']) + return engine.renderFile('foo.html') + .then((html) => { + expect(html).to.equal('foo1') + server.respondWith('GET', 'https://example.com/views/foo.html', + [200, {'Content-Type': 'text/plain'}, 'foo2']) + return engine.renderFile('foo.html') + }) + .then(html => expect(html).to.equal('foo1')) + }) + }) })