diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-06-08 14:57:54 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-06-08 14:57:54 +0200 |
commit | e6c6ec4293fdf0bd225f1c6613ea484469fa98fd (patch) | |
tree | 31cfe8c67936b5f7713755aa7ea64c6d090ee06f /lib | |
parent | a1d3acf065677607928eb71a11c89f6c6c7bb85b (diff) | |
download | gitbook-e6c6ec4293fdf0bd225f1c6613ea484469fa98fd.zip gitbook-e6c6ec4293fdf0bd225f1c6613ea484469fa98fd.tar.gz gitbook-e6c6ec4293fdf0bd225f1c6613ea484469fa98fd.tar.bz2 |
Improve testing for sortPlugins
Diffstat (limited to 'lib')
-rw-r--r-- | lib/models/plugin.js | 82 | ||||
-rw-r--r-- | lib/plugins/__tests__/sortPlugins.js | 22 |
2 files changed, 50 insertions, 54 deletions
diff --git a/lib/models/plugin.js b/lib/models/plugin.js index e3d6c4d..c8bb2f7 100644 --- a/lib/models/plugin.js +++ b/lib/models/plugin.js @@ -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(), diff --git a/lib/plugins/__tests__/sortPlugins.js b/lib/plugins/__tests__/sortPlugins.js index 2e1b594..0f54646 100644 --- a/lib/plugins/__tests__/sortPlugins.js +++ b/lib/plugins/__tests__/sortPlugins.js @@ -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); |