mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Improve testing for sortPlugins
This commit is contained in:
+43
-39
@@ -1,9 +1,10 @@
|
||||
var Immutable = require('immutable');
|
||||
|
||||
var TemplateBlock = require('./templateBlock');
|
||||
var DEFAULT_VERSION = '*';
|
||||
|
||||
var PluginDependency = require('./pluginDependency');
|
||||
var THEME_PREFIX = require('../constants/themePrefix');
|
||||
|
||||
var DEFAULT_VERSION = '*';
|
||||
|
||||
var Plugin = Immutable.Record({
|
||||
name: String(),
|
||||
@@ -49,37 +50,43 @@ Plugin.prototype.getDepth = function() {
|
||||
};
|
||||
|
||||
/**
|
||||
Return the ID on NPM for this plugin
|
||||
|
||||
@return {String}
|
||||
*/
|
||||
* Return the ID on NPM for this plugin
|
||||
* @return {String}
|
||||
*/
|
||||
Plugin.prototype.getNpmID = function() {
|
||||
return PluginDependency.nameToNpmID(this.getName());
|
||||
};
|
||||
|
||||
/**
|
||||
Check if a plugin is loaded
|
||||
|
||||
@return {Boolean}
|
||||
*/
|
||||
* Check if a plugin is loaded
|
||||
* @return {Boolean}
|
||||
*/
|
||||
Plugin.prototype.isLoaded = function() {
|
||||
return Boolean(this.getPackage().size > 0);
|
||||
};
|
||||
|
||||
/**
|
||||
Return map of hooks
|
||||
@return {Map<String:Function>}
|
||||
*/
|
||||
* Check if a plugin is a theme given its name
|
||||
* @return {Boolean}
|
||||
*/
|
||||
Plugin.prototype.isTheme = function() {
|
||||
var name = this.getName();
|
||||
return (name && name.indexOf(THEME_PREFIX) === 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return map of hooks
|
||||
* @return {Map<String:Function>}
|
||||
*/
|
||||
Plugin.prototype.getHooks = function() {
|
||||
return this.getContent().get('hooks') || Immutable.Map();
|
||||
};
|
||||
|
||||
/**
|
||||
Return infos about resources for a specific type
|
||||
|
||||
@param {String} type
|
||||
@return {Map<String:Mixed>}
|
||||
*/
|
||||
* Return infos about resources for a specific type
|
||||
* @param {String} type
|
||||
* @return {Map<String:Mixed>}
|
||||
*/
|
||||
Plugin.prototype.getResources = function(type) {
|
||||
if (type != 'website' && type != 'ebook') {
|
||||
throw new Error('Invalid assets type ' + type);
|
||||
@@ -92,17 +99,17 @@ Plugin.prototype.getResources = function(type) {
|
||||
};
|
||||
|
||||
/**
|
||||
Return map of filters
|
||||
@return {Map<String:Function>}
|
||||
*/
|
||||
* Return map of filters
|
||||
* @return {Map<String:Function>}
|
||||
*/
|
||||
Plugin.prototype.getFilters = function() {
|
||||
return this.getContent().get('filters');
|
||||
};
|
||||
|
||||
/**
|
||||
Return map of blocks
|
||||
@return {Map<String:TemplateBlock>}
|
||||
*/
|
||||
* Return map of blocks
|
||||
* @return {Map<String:TemplateBlock>}
|
||||
*/
|
||||
Plugin.prototype.getBlocks = function() {
|
||||
var blocks = this.getContent().get('blocks');
|
||||
blocks = blocks || Immutable.Map();
|
||||
@@ -114,21 +121,19 @@ Plugin.prototype.getBlocks = function() {
|
||||
};
|
||||
|
||||
/**
|
||||
Return a specific hook
|
||||
|
||||
@param {String} name
|
||||
@return {Function|undefined}
|
||||
*/
|
||||
* Return a specific hook
|
||||
* @param {String} name
|
||||
* @return {Function|undefined}
|
||||
*/
|
||||
Plugin.prototype.getHook = function(name) {
|
||||
return this.getHooks().get(name);
|
||||
};
|
||||
|
||||
/**
|
||||
Create a plugin from a string
|
||||
|
||||
@param {String}
|
||||
@return {Plugin}
|
||||
*/
|
||||
* Create a plugin from a string
|
||||
* @param {String}
|
||||
* @return {Plugin}
|
||||
*/
|
||||
Plugin.createFromString = function(s) {
|
||||
var parts = s.split('@');
|
||||
var name = parts[0];
|
||||
@@ -141,11 +146,10 @@ Plugin.createFromString = function(s) {
|
||||
};
|
||||
|
||||
/**
|
||||
Create a plugin from a dependency
|
||||
|
||||
@param {PluginDependency}
|
||||
@return {Plugin}
|
||||
*/
|
||||
* Create a plugin from a dependency
|
||||
* @param {PluginDependency}
|
||||
* @return {Plugin}
|
||||
*/
|
||||
Plugin.createFromDep = function(dep) {
|
||||
return new Plugin({
|
||||
name: dep.getName(),
|
||||
|
||||
@@ -1,17 +1,6 @@
|
||||
var PluginDependency = require('../../models/pluginDependency');
|
||||
var sortPlugins = require('../sortPlugins');
|
||||
var listAll = require('../listAll');
|
||||
var THEME_PREFIX = require('../../constants/themePrefix');
|
||||
|
||||
|
||||
/**
|
||||
Check if a plugin is a theme given its name
|
||||
|
||||
@return {Boolean}
|
||||
*/
|
||||
function isTheme(name) {
|
||||
return (name && name.indexOf(THEME_PREFIX) === 0);
|
||||
}
|
||||
|
||||
describe('sortPlugins', function() {
|
||||
it('must load themes after plugins', function() {
|
||||
@@ -24,11 +13,11 @@ describe('sortPlugins', function() {
|
||||
themes = sorted.slice(-2);
|
||||
|
||||
var pluginsOk = plugins.every(function(plugin) {
|
||||
return !isTheme(plugin.getName());
|
||||
return !plugin.isTheme();
|
||||
});
|
||||
|
||||
var themesOk = themes.every(function(theme) {
|
||||
return isTheme(theme.getName());
|
||||
return theme.isTheme();
|
||||
});
|
||||
|
||||
expect(pluginsOk).toBe(true);
|
||||
@@ -38,6 +27,9 @@ describe('sortPlugins', function() {
|
||||
expect(themes.size).toBe(2);
|
||||
expect(themes.has('theme-faq')).toBe(true);
|
||||
expect(themes.has('theme-default')).toBe(true);
|
||||
|
||||
// theme-default should be last
|
||||
expect(themes.last().getName()).toBe('theme-default');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,11 +43,11 @@ describe('sortPlugins', function() {
|
||||
themes = sorted.slice(-2);
|
||||
|
||||
var pluginsOk = plugins.every(function(plugin) {
|
||||
return !isTheme(plugin.getName());
|
||||
return !plugin.isTheme();
|
||||
});
|
||||
|
||||
var themesOk = themes.every(function(theme) {
|
||||
return isTheme(theme.getName());
|
||||
return theme.isTheme();
|
||||
});
|
||||
|
||||
expect(pluginsOk).toBe(true);
|
||||
|
||||
Reference in New Issue
Block a user