Add test for resources listing

This commit is contained in:
Samy Pessé
2016-02-22 10:36:28 +01:00
parent de23494409
commit 1e243281c7
4 changed files with 66 additions and 3 deletions
+23 -3
View File
@@ -1,5 +1,6 @@
var _ = require('lodash');
var path = require('path');
var url = require('url');
var resolve = require('resolve');
var mergeDefaults = require('merge-defaults');
var jsonschema = require('jsonschema');
@@ -191,10 +192,10 @@ BookPlugin.prototype.hook = function(name, input) {
// Return resources without normalization
BookPlugin.prototype._getResources = function(base) {
base = base;
var book = this.infos[base];
var book = this.content[base];
// Compatibility with version 1.x.x
if (base == 'website') book = book || this.infos.book;
if (base == 'website') book = book || this.content.book;
// Nothing specified, fallback to default
if (!book) {
@@ -211,6 +212,25 @@ BookPlugin.prototype._getResources = function(base) {
return Promise(_.cloneDeep(book));
};
// Normalize a specific resource
BookPlugin.prototype.normalizeResource = function(resource) {
// Parse the resource path
var parsed = url.parse(resource);
// This is a remote resource
// so we will simply link to using it's URL
if (parsed.protocol) {
return {
'url': resource
};
}
// This will be copied over from disk
// and shipped with the book's build
return { 'path': this.npmId+'/'+resource };
};
// Normalize resources and return them
BookPlugin.prototype.getResources = function(base) {
var that = this;
@@ -219,7 +239,7 @@ BookPlugin.prototype.getResources = function(base) {
.then(function(resources) {
_.each(RESOURCES, function(resourceType) {
resources[resourceType] = (resources[resourceType] || []).map(that.normalizeResource);
resources[resourceType] = _.map(resources[resourceType] || [], that.normalizeResource);
});
return resources;
+12
View File
@@ -0,0 +1,12 @@
module.exports = {
book: {
assets: './assets',
js: [
'myfile.js',
'https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js'
],
css: [
'myfile.css'
]
}
};
+7
View File
@@ -0,0 +1,7 @@
{
"name": "gitbook-plugin-test-resources",
"version": "1.0.0",
"engines": {
"gitbook": "*"
}
}
+24
View File
@@ -100,5 +100,29 @@ describe('Plugins', function() {
});
});
});
describe('Resources', function() {
var plugin;
before(function() {
plugin = new BookPlugin(book, 'test-resources');
return plugin.load(PLUGINS_ROOT);
});
it('should list all resources for website', function() {
return plugin.getResources('website')
.then(function(resources) {
resources.assets.should.equal('./assets');
resources.js.should.have.lengthOf(2);
resources.js[0].path.should.equal('gitbook-plugin-test-resources/myfile.js');
resources.js[1].url.should.equal('https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js');
resources.css.should.have.lengthOf(1);
resources.css[0].path.should.equal('gitbook-plugin-test-resources/myfile.css');
});
});
});
});