From bfcef27c473308046d581879f290fec483b50add Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 14 Oct 2016 21:48:38 +0300 Subject: [PATCH] plugin tests --- src/database.js | 2 +- src/plugins.js | 6 +--- src/plugins/hooks.js | 1 - src/plugins/load.js | 23 +++++++------- test/plugins.js | 76 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 test/plugins.js diff --git a/src/database.js b/src/database.js index 8021836fde..65a5453d09 100644 --- a/src/database.js +++ b/src/database.js @@ -5,7 +5,7 @@ var databaseName = nconf.get('database'); var winston = require('winston'); if (!databaseName) { - winston.info('Database type not set! Run ./nodebb setup'); + winston.error(new Error('Database type not set! Run ./nodebb setup')); process.exit(); } diff --git a/src/plugins.js b/src/plugins.js index 1274b829ef..10bda11b31 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -33,6 +33,7 @@ var middleware; Plugins.customLanguageFallbacks = {}; Plugins.libraryPaths = []; Plugins.versionWarning = []; + Plugins.languageCodes = []; Plugins.initialized = false; @@ -83,11 +84,6 @@ var middleware; Plugins.acpScripts.length = 0; Plugins.libraryPaths.length = 0; - // Plugins.registerHook('core', { - // hook: 'static:app.load', - // method: addLanguages - // }); - async.waterfall([ function (next) { // Build language code list diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js index 9e08acde78..b8e5af0e46 100644 --- a/src/plugins/hooks.js +++ b/src/plugins/hooks.js @@ -112,7 +112,6 @@ module.exports = function (Plugins) { } hookObj.method(params, next); - }, function (err, values) { if (err) { winston.error('[plugins] ' + hook + ', ' + err.message); diff --git a/src/plugins/load.js b/src/plugins/load.js index 836e0d2c4a..7027c31d73 100644 --- a/src/plugins/load.js +++ b/src/plugins/load.js @@ -1,16 +1,16 @@ 'use strict'; -var fs = require('fs'), - path = require('path'), - semver = require('semver'), - async = require('async'), - winston = require('winston'), - nconf = require('nconf'), - _ = require('underscore'), - file = require('../file'); +var fs = require('fs'); +var path = require('path'); +var semver = require('semver'); +var async = require('async'); +var winston = require('winston'); +var nconf = require('nconf'); +var _ = require('underscore'); +var file = require('../file'); -var utils = require('../../public/src/utils'), - meta = require('../meta'); +var utils = require('../../public/src/utils'); +var meta = require('../meta'); module.exports = function (Plugins) { @@ -217,8 +217,7 @@ module.exports = function (Plugins) { return callback(); } - var pathToFolder = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages), - fallbackMap = {}; + var pathToFolder = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages); utils.walk(pathToFolder, function (err, languages) { if (err) { diff --git a/test/plugins.js b/test/plugins.js new file mode 100644 index 0000000000..8d1b0842f6 --- /dev/null +++ b/test/plugins.js @@ -0,0 +1,76 @@ +'use strict'; +/*global require*/ + +var assert = require('assert'); +var path = require('path'); +var nconf = require('nconf'); + +var db = require('./mocks/databasemock'); +var plugins = require('../src/plugins'); + +describe('Plugins', function () { + + it('should load plugin data', function (done) { + var pluginId = 'nodebb-plugin-markdown'; + plugins.loadPlugin(path.join(nconf.get('base_dir'), 'node_modules/' + pluginId), function (err) { + assert.ifError(err); + assert(plugins.libraries[pluginId]); + assert(plugins.loadedHooks['static:app.load']); + assert(plugins.staticDirs['nodebb-plugin-markdown/js']); + assert.notEqual(plugins.lessFiles.indexOf('nodebb-plugin-markdown/public/less/default.less'), -1); + + done(); + }); + }); + + it('should return true of hook has listeners', function (done) { + assert(plugins.hasListeners('filter:parse.post')); + done(); + }); + + it('should register and fire a filter hook', function (done) { + function filterMethod1(data, callback) { + data.foo ++; + callback(null, data); + } + function filterMethod2(data, callback) { + data.foo += 5; + callback(null, data); + } + + plugins.registerHook('test-plugin', {hook: 'filter:test.hook', method: filterMethod1}); + plugins.registerHook('test-plugin', {hook: 'filter:test.hook', method: filterMethod2}); + + plugins.fireHook('filter:test.hook', {foo: 1}, function (err, data) { + assert.ifError(err); + assert.equal(data.foo, 7); + done(); + }); + + }); + + it('should register and fire an action hook', function (done) { + function actionMethod(data) { + assert.equal(data.bar, 'test'); + done(); + } + + plugins.registerHook('test-plugin', {hook: 'action:test.hook', method: actionMethod}); + plugins.fireHook('action:test.hook', {bar: 'test'}); + }); + + it('should register and fire a static hook', function (done) { + function actionMethod(data, callback) { + assert.equal(data.bar, 'test'); + callback(); + } + + plugins.registerHook('test-plugin', {hook: 'static:test.hook', method: actionMethod}); + plugins.fireHook('static:test.hook', {bar: 'test'}, function (err) { + assert.ifError(err); + done() + }); + }); + +}); +