100% coverage

This commit is contained in:
ChenL
2018-01-12 10:53:43 +08:00
committed by Jun Yang
parent 7b696ed0d2
commit 925df9a9b5
3 changed files with 48 additions and 40 deletions
+1 -1
View File
@@ -128,7 +128,7 @@ var _engine = {
} }
} }
xhr.onerror = () => { xhr.onerror = () => {
reject(new Error(xhr.statusText)) reject(new Error('An error occurred whilst sending the response.'))
} }
xhr.open('GET', fullUrl) xhr.open('GET', fullUrl)
xhr.send() xhr.send()
+1 -1
View File
@@ -4,7 +4,7 @@ const url = require('../../src/util/url.js')
describe('util/url', function () { describe('util/url', function () {
if (process.version.match(/^v(\d+)/)[1] < 8) { if (process.version.match(/^v(\d+)/)[1] < 8) {
return; return
} }
const JSDOM = require('jsdom').JSDOM const JSDOM = require('jsdom').JSDOM
var dom var dom
+46 -38
View File
@@ -5,35 +5,32 @@ const expect = chai.expect
describe('xhr', () => { describe('xhr', () => {
if (process.version.match(/^v(\d+)/)[1] < 8) { if (process.version.match(/^v(\d+)/)[1] < 8) {
return; return
} }
const JSDOM = require('jsdom').JSDOM const JSDOM = require('jsdom').JSDOM
var server, engine, dom var server, engine, dom
beforeEach(() => { beforeEach(() => {
server = sinon.fakeServer.create() server = sinon.createFakeServer()
server.autoRespond = true server.autoRespond = true
server.respondWith('GET', 'https://example.com/views/hello.html', server.respondWith('GET', 'https://example.com/views/hello.html',
[200, {'Content-Type': 'text/plain'}, 'hello {{name}}']) [200, {'Content-Type': 'text/plain'}, 'hello {{name}}'])
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest() dom = new JSDOM('', {
dom = new JSDOM(``, { url: 'https://example.com/foo/bar.html',
url: 'https://example.com/',
referrer: 'https://example.com/',
contentType: 'text/html', contentType: 'text/html',
includeNodeLocations: true includeNodeLocations: true
}) })
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest()
global.document = dom.window.document global.document = dom.window.document
engine = Liquid({ engine = Liquid({
root: 'https://example.com/views', root: 'https://example.com/views/',
extname: '.html' extname: '.html'
}) })
}) })
afterEach(() => { afterEach(() => {
server.restore() server.restore()
delete global.XMLHttpRequest delete global.XMLHttpRequest
delete global.document delete global.document
}) })
describe('#renderFile()', () => { describe('#renderFile()', () => {
it('should support without extname', () => { it('should support without extname', () => {
return expect(engine.renderFile('hello', {name: 'alice1'})) return expect(engine.renderFile('hello', {name: 'alice1'}))
@@ -65,32 +62,26 @@ describe('xhr', () => {
return expect(engine.renderFile('/not/exist.html')) return expect(engine.renderFile('/not/exist.html'))
.to.be.rejectedWith('Not Found') .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', () => { describe('root', () => {
it('should support with null', () => { 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({ engine = Liquid({
extname: '.html' 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}}']) [200, {'Content-Type': 'text/plain'}, 'hello {{name}}'])
return expect(engine.renderFile('hello.html', {name: 'alice5'})) return expect(engine.renderFile('hello.html', {name: 'alice5'}))
.to.eventually.equal('hello alice5') .to.eventually.equal('hello alice5')
}) })
it('should support with empty', () => { 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({ engine = Liquid({
root: '', root: '',
extname: '.html' extname: '.html'
@@ -101,13 +92,6 @@ describe('xhr', () => {
.to.eventually.equal('hello alice5') .to.eventually.equal('hello alice5')
}) })
it('should support with relative path', () => { 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({ engine = Liquid({
root: './views/', root: './views/',
extname: '.html' extname: '.html'
@@ -118,13 +102,6 @@ describe('xhr', () => {
.to.eventually.equal('hello alice5') .to.eventually.equal('hello alice5')
}) })
it('should support with absolute path', () => { 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({ engine = Liquid({
root: '/views/', root: '/views/',
extname: '.html' extname: '.html'
@@ -145,4 +122,35 @@ describe('xhr', () => {
.to.eventually.equal('hello alice5') .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'))
})
})
}) })