diff options
Diffstat (limited to 'test/plugins.js')
-rw-r--r-- | test/plugins.js | 363 |
1 files changed, 114 insertions, 249 deletions
diff --git a/test/plugins.js b/test/plugins.js index 1600d0d..4d9cdf1 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -1,84 +1,103 @@ var _ = require('lodash'); -var fs = require('fs'); -var should = require('should'); var path = require('path'); -var Plugin = require('../lib/plugin'); -var parsers = require('gitbook-parsers'); -var PLUGINS_ROOT = path.resolve(__dirname, 'plugins'); +var mock = require('./mock'); +var registry = require('../lib/plugins/registry'); +var Output = require('../lib/output/base'); +var PluginsManager = require('../lib/plugins'); +var BookPlugin = require('../lib/plugins/plugin'); -describe('Plugins', function () { +var PLUGINS_ROOT = path.resolve(__dirname, 'node_modules'); + +describe('Plugins', function() { var book; before(function() { - return books.parse('basic') + return mock.setupBook({}) .then(function(_book) { book = _book; }); }); - describe('Invalid', function() { - var plugin; - - before(function() { - plugin = new Plugin(book, 'invalid'); - plugin.load('./invalid', PLUGINS_ROOT); - }); - - it('should be detected', function() { - should(plugin.isValid()).be.exactly(false); + describe('Resolve Version', function() { + it('should resolve a plugin version', function() { + return registry.resolve('ga') + .should.be.fulfilled(); }); }); - describe('Empty', function() { - var plugin; - - before(function() { - plugin = new Plugin(book, 'empty'); - plugin.load('./empty', PLUGINS_ROOT); + describe('Installation', function() { + it('should install a plugin from NPM without a specific version', function() { + return registry.install(book, 'ga') + .should.be.fulfilled(); }); - it('should valid a plugin', function() { - should(plugin.isValid()).be.exactly(true); + it('should install a plugin from NPM with a specific version', function() { + return registry.install(book, 'ga', '1.0.0') + .should.be.fulfilled(); }); - it('should return an empty list of resources', function() { - return plugin.getResources() - .then(function(resources) { - _.each(Plugin.RESOURCES, function(resName) { - resources[resName].should.have.lengthOf(0); - }); + it('should correctly install all dependencies (if none)', function() { + return mock.setupBook({}) + .then(function(book) { + var plugins = new PluginsManager(book); + return plugins.install() + .should.be.fulfilledWith(0); }); }); - }); - describe('Configuration', function() { - var plugin; - - before(function() { - plugin = new Plugin(book, 'testconfig'); - plugin.load('./config', PLUGINS_ROOT); - }); - - it('should throw error for invalid configuration', function() { - return plugin.validateConfig({}) - .should.be.rejectedWith('Configuration Error: pluginsConfig.testconfig.testRequired is required'); + it('should correctly install all dependencies (if any)', function() { + return mock.setupBook({ + 'book.json': { + plugins: ['ga'] + } + }) + .then(function(book) { + return book.config.load() + .then(function() { + var plugins = new PluginsManager(book); + return plugins.install(); + }); + }) + .should.be.fulfilledWith(1); }); + }); - it('should throw error for invalid types', function() { - return plugin.validateConfig({ - testRequired: 'hello' - }) - .should.be.rejectedWith('Configuration Error: pluginsConfig.testconfig.testRequired is not of a type(s) number'); + describe('Loading', function() { + it('should load default plugins', function() { + return mock.outputDefaultBook(Output) + .then(function(output) { + output.plugins.count().should.be.greaterThan(0); + }); }); + }); - it('should extend with default values', function() { - return plugin.validateConfig({ - testRequired: 12 + describe('Configuration', function() { + it('should fail loading a plugin with an invalid configuration', function() { + var plugin = new BookPlugin(book, 'test-config'); + return plugin.load(PLUGINS_ROOT) + .should.be.rejectedWith('Error with book\'s configuration: pluginsConfig.test-config.myProperty is required'); + }); + + it('should extend configuration with default properties', function() { + return mock.setupBook({ + 'book.json': { + pluginsConfig: { + 'test-config': { + 'myProperty': 'world' + } + } + } }) - .should.be.fulfilledWith({ - hello: 'world', - testRequired: 12 + .then(function(book2) { + return book2.config.load() + .then(function() { + var plugin = new BookPlugin(book2, 'test-config'); + return plugin.load(PLUGINS_ROOT); + }) + .then(function() { + book2.config.get('pluginsConfig.test-config.myDefaultProperty', '').should.equal('hello'); + }); }); }); }); @@ -87,243 +106,89 @@ describe('Plugins', function () { var plugin; before(function() { - plugin = new Plugin(book, 'resources'); - plugin.load('./resources', PLUGINS_ROOT); - - return book.plugins.load(plugin); - }); - - it('should valid a plugin', function() { - should(plugin.isValid()).be.exactly(true); + plugin = new BookPlugin(book, 'test-resources'); + return plugin.load(PLUGINS_ROOT); }); - describe('Website', function() { - it('should return a valid list of resources', function() { - return plugin.getResources('website') - .then(function(resources) { - resources.js.should.have.lengthOf(1); - }); - }); - it('should extend books plugins', function() { - var resources = book.plugins.resources('website'); - resources.js.should.have.lengthOf(5); - }); - }); - - describe('eBook', function() { - it('should return a valid list of resources', function() { - return plugin.getResources('ebook') - .then(function(resources) { - resources.css.should.have.lengthOf(1); - }); - }); + it('should list all resources for website', function() { + return plugin.getResources('website') + .then(function(resources) { + resources.assets.should.equal('./assets'); - it('should extend books plugins', function() { - var resources = book.plugins.resources('ebook'); + 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'); - // There is resources from highlight plugin and this plugin - resources.css.should.have.lengthOf(2); - should.exist(_.find(resources.css, { - path: 'gitbook-plugin-resources/test' - })); + resources.css.should.have.lengthOf(1); + resources.css[0].path.should.equal('gitbook-plugin-test-resources/myfile.css'); }); }); }); describe('Filters', function() { - var plugin; + var plugin, filters; before(function() { - plugin = new Plugin(book, 'filters'); - plugin.load('./filters', PLUGINS_ROOT); + plugin = new BookPlugin(book, 'test-filters'); + return plugin.load(PLUGINS_ROOT) - return book.plugins.load(plugin); - }); - - it('should valid a plugin', function() { - should(plugin.isValid()).be.exactly(true); + .then(function() { + filters = plugin.getFilters(); + }); }); - it('should return a map of filters', function() { - var filters = plugin.getFilters(); - + it('should list all filters', function() { _.size(filters).should.equal(2); - filters.should.have.property('hello'); - filters.should.have.property('helloCtx'); }); - it('should correctly extend template filters', function() { - return book.template.renderString('{{ \'World\'|hello }}') - .then(function(content) { - content.should.equal('Hello World'); - }); + it('should correctly execute a filter', function() { + filters.hello('World').should.equal('Hello World!'); }); - it('should correctly set book as context', function() { - return book.template.renderString('{{ \'root\'|helloCtx }}') - .then(function(content) { - content.should.equal('root:'+book.root); - }); + it('should correctly set contexts for filter', function() { + filters.testContext('Hello'); }); }); describe('Blocks', function() { - var plugin; + var plugin, blocks; before(function() { - plugin = new Plugin(book, 'blocks'); - plugin.load('./blocks', PLUGINS_ROOT); - - return book.plugins.load(plugin); - }); - - var testTpl = function(str, args, options) { - return book.template.renderString(str, args, options) - .then(book.template.postProcess); - }; - - it('should valid a plugin', function() { - should(plugin.isValid()).be.exactly(true); - }); + plugin = new BookPlugin(book, 'test-blocks'); + return plugin.load(PLUGINS_ROOT) - it('should correctly extend template blocks', function() { - return testTpl('{% test %}hello{% endtest %}') - .then(function(content) { - content.should.equal('testhellotest'); - }); - }); - - describe('Shortcuts', function() { - it('should correctly accept shortcuts', function() { - return testTpl('$$hello$$', {}, { - type: 'markdown' - }) - .then(function(content) { - content.should.equal('testhellotest'); - }); - }); - - it('should correctly apply shortcuts to included file', function() { - return books.generate('conrefs', 'website', { - testId: 'include-plugins', - prepare: function(bookConref) { - plugin = new Plugin(bookConref, 'blocks'); - plugin.load('./blocks', PLUGINS_ROOT); - - return bookConref.plugins.load(plugin); - } - }) - .then(function(bookConref) { - var readme = fs.readFileSync( - path.join(bookConref.options.output, 'index.html'), - { encoding: 'utf-8' } - ); - - readme.should.be.html({ - '.page-inner p#test-plugin-block-shortcuts-1': { - count: 1, - text: 'testtest_block1test', - trim: true - }, - '.page-inner p#test-plugin-block-shortcuts-2': { - count: 1, - text: 'testtest_block2test', - trim: true - } - }); - }); + .then(function() { + blocks = plugin.getBlocks(); }); }); - - it('should correctly extend template blocks with defined end', function() { - return testTpl('{% test2 %}hello{% endtest2end %}') - .then(function(content) { - content.should.equal('test2hellotest2'); - }); + it('should list all blocks', function() { + _.size(blocks).should.equal(2); }); - it('should correctly extend template blocks with sub-blocks', function() { - return testTpl('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}') - .then(function(content) { - content.should.equal('hello;world'); - }); + it('should correctly normalize block', function() { + blocks.hello.process({ body: 'World' }).should.equal('Hello World!'); }); - it('should correctly extend template blocks with different sub-blocks', function() { - return testTpl('{% test4join separator=";" %}hello{% also %}the{% finally %}world{% endtest4join %}') - .then(function(content) { - content.should.equal('hello;the;world'); - }); - }); - - it('should correctly extend template blocks with arguments (1)', function() { - return testTpl('{% test5args "a" %}{% endtest5args %}') - .then(function(content) { - content.should.equal('test5atest5'); - }); - }); - - it('should correctly extend template blocks with arguments (2)', function() { - return testTpl('{% test5args "a", "b" %}{% endtest5args %}') - .then(function(content) { - content.should.equal('test5a,btest5'); - }); - }); - - it('should correctly extend template blocks with arguments (3)', function() { - return testTpl('{% test5args "a", "b", "c" %}{% endtest5args %}') - .then(function(content) { - content.should.equal('test5a,b,ctest5'); - }); - }); - - it('should correctly extend template blocks with args and kwargs', function() { - return testTpl('{% test5kwargs "a", "b", "c", d="test", e="test2" %}{% endtest5kwargs %}') - .then(function(content) { - content.should.equal('test5a,b,c,d:test,e:test2,__keywords:truetest5'); - }); - }); - - it('should correctly extend template blocks with access to context', function() { - return testTpl('{% set name = "john" %}{% test6context %}{% endtest6context %}', {}) - .then(function(content) { - content.should.equal('test6johntest6'); - }); + it('should correctly set contexts for filter', function() { + blocks.testContext.process({ body: 'Hello' }); }); }); - describe('Blocks without parsing', function() { + describe('Hooks', function() { var plugin; before(function() { - plugin = new Plugin(book, 'blocks'); - plugin.load('./blocks', PLUGINS_ROOT); - - return book.plugins.load(plugin); + plugin = new BookPlugin(book, 'test-hooks'); + return plugin.load(PLUGINS_ROOT); }); - var testTpl = function(markup, str, args, options) { - var filetype = parsers.get(markup); - - return book.template.renderString(str, args, options) - .then(filetype.page).get('sections').get(0).get('content') - .then(book.template.postProcess); - }; - - it('should correctly process unparsable for markdown', function() { - return testTpl('.md', '{% test %}**hello**{% endtest %}') - .then(function(content) { - content.should.equal('<p>test**hello**test</p>\n'); - }); - }); - - it('should correctly process unparsable for asciidoc', function() { - return testTpl('.adoc', '{% test %}**hello**{% endtest %}') - .then(function(content) { - content.should.equal('<div class="paragraph">\n<p>test**hello**test</p>\n</div>'); - }); + it('can call a hook', function() { + return plugin.hook('init') + .then(function() { + global._hooks.should.deepEqual(['init']); + }); }); }); }); |