From f7db55fedd38ea831cd22828391df815dc1996a7 Mon Sep 17 00:00:00 2001 From: La Baik Date: Sun, 28 May 2017 18:31:53 +0200 Subject: [PATCH] test: 3rd party plugins --- test/integration/force-gzip-test.js | 4 +- test/integration/handle-404-test.js | 4 +- .../integration/handle-hoodie-request-test.js | 4 +- test/integration/plugins-test.js | 23 +++ test/integration/server-test.js | 32 ++-- test/integration/smoke-test.js | 4 +- test/unit/cli/app-defaults-test.js | 17 +++ test/unit/cli/hoodie-default-test.js | 3 + test/unit/cli/options-test.js | 38 ++++- .../client/bundle-handler-factory-test.js | 8 +- .../bundle-test.js} | 53 ++++--- test/unit/client/client-test.js | 16 +- test/unit/server/loader-test.js | 46 ------ test/unit/server/plugins-test.js | 131 ++++++++++++++++ test/unit/server/public-test.js | 144 +++++++++++++++--- 15 files changed, 407 insertions(+), 120 deletions(-) create mode 100644 test/integration/plugins-test.js rename test/unit/{plugins-client-bundle-test.js => client/bundle-test.js} (80%) delete mode 100644 test/unit/server/loader-test.js create mode 100644 test/unit/server/plugins-test.js diff --git a/test/integration/force-gzip-test.js b/test/integration/force-gzip-test.js index 24c426b2c..7ae17ba8a 100644 --- a/test/integration/force-gzip-test.js +++ b/test/integration/force-gzip-test.js @@ -19,7 +19,9 @@ var hapiPluginOptions = { inMemory: true, loglevel: 'error', paths: {}, - PouchDB: PouchDB + PouchDB: PouchDB, + plugins: [], + app: {} } } diff --git a/test/integration/handle-404-test.js b/test/integration/handle-404-test.js index 6f243a128..ee4df639e 100644 --- a/test/integration/handle-404-test.js +++ b/test/integration/handle-404-test.js @@ -11,7 +11,9 @@ var hapiPluginOptions = { inMemory: true, loglevel: 'error', paths: {}, - PouchDB: PouchDB + PouchDB: PouchDB, + plugins: [], + app: {} } } diff --git a/test/integration/handle-hoodie-request-test.js b/test/integration/handle-hoodie-request-test.js index 6495446d4..fdc098986 100644 --- a/test/integration/handle-hoodie-request-test.js +++ b/test/integration/handle-hoodie-request-test.js @@ -11,7 +11,9 @@ var hapiPluginOptions = { inMemory: true, loglevel: 'error', paths: {}, - PouchDB: PouchDB + PouchDB: PouchDB, + plugins: [], + app: {} } } diff --git a/test/integration/plugins-test.js b/test/integration/plugins-test.js new file mode 100644 index 000000000..4dcf9999b --- /dev/null +++ b/test/integration/plugins-test.js @@ -0,0 +1,23 @@ +var simple = require('simple-mock') +var test = require('tap').test + +var hapiPlugin = require('../../server') +var serverMock = { + register: simple.stub().callbackWith(null), + ext: simple.stub() +} + +require('npmlog').level = 'error' + +test('plugins', function (t) { + hapiPlugin.register(serverMock, { + paths: { + data: '.' + }, + plugins: ['hoodie-plugin-foobar'], + app: {} + }, function (error) { + t.error(error) + t.end() + }) +}) diff --git a/test/integration/server-test.js b/test/integration/server-test.js index b492782c6..26fe9d913 100644 --- a/test/integration/server-test.js +++ b/test/integration/server-test.js @@ -14,18 +14,28 @@ test('server', function (t) { hapiPlugin.register(serverMock, { paths: { data: '.' - } - }, function (error, config) { + }, + plugins: [], + app: {} + }, function (error) { t.error(error) t.end() }) }) -test('server with options without options.path (#554)', function (t) { - hapiPlugin.register(serverMock, {}, function (error, config) { - t.error(error) - t.end() - }) +test('does not modify the passed options object', function (t) { + var options = { + db: { + url: 'http://admin:admin@localhost:5984' + }, + plugins: [], + app: {} + } + + hapiPlugin.register(serverMock, options, function () {}) + + t.is(options.db.url, 'http://admin:admin@localhost:5984') + t.end() }) test('error on register is passed to callback', function (t) { @@ -37,7 +47,9 @@ test('error on register is passed to callback', function (t) { hapiPlugin.register(serverErrorMock, { paths: { data: '.' - } + }, + plugins: [], + app: {} }, function (error, server, options) { t.ok(error) t.end() @@ -48,7 +60,9 @@ test('application root with valid server module', function (t) { var options = { paths: { public: 'public' - } + }, + plugins: [], + app: {} } var savedPath = process.cwd() t.tearDown(function () { diff --git a/test/integration/smoke-test.js b/test/integration/smoke-test.js index 3257796ae..00c5fe127 100644 --- a/test/integration/smoke-test.js +++ b/test/integration/smoke-test.js @@ -18,7 +18,9 @@ var hapiPluginOptions = { inMemory: true, loglevel: 'error', paths: {}, - PouchDB: PouchDB + PouchDB: PouchDB, + plugins: [], + app: {} } } diff --git a/test/unit/cli/app-defaults-test.js b/test/unit/cli/app-defaults-test.js index 0c9d1bcd8..ac46fb8ad 100644 --- a/test/unit/cli/app-defaults-test.js +++ b/test/unit/cli/app-defaults-test.js @@ -37,5 +37,22 @@ test('app options', function (group) { t.end() }) + group.test('with hoodie.name', function (t) { + var getAppDefaults = proxyquire('../../../cli/app-defaults', { + 'path': pathMock, + './package.json': { + name: 'pkg-name', + hoodie: { + name: 'foo' + } + } + }) + var options = getAppDefaults() + + t.deepEqual(options.name, 'foo', 'sets name from pkg.hoodie.name') + + t.end() + }) + group.end() }) diff --git a/test/unit/cli/hoodie-default-test.js b/test/unit/cli/hoodie-default-test.js index 00fe6afb3..367aeb6bc 100644 --- a/test/unit/cli/hoodie-default-test.js +++ b/test/unit/cli/hoodie-default-test.js @@ -8,6 +8,7 @@ test('hoodie defaults test', function (group) { t.deepEqual(defaults, { address: '127.0.0.1', adminPassword: undefined, + app: {}, client: {}, data: '.hoodie', dbAdapter: 'pouchdb-adapter-fs', @@ -16,7 +17,9 @@ test('hoodie defaults test', function (group) { dbUrlUsername: undefined, inMemory: false, loglevel: 'warn', + name: undefined, port: 8080, + plugins: [], public: 'public', url: undefined }, 'setting hoodie defaults') diff --git a/test/unit/cli/options-test.js b/test/unit/cli/options-test.js index 76916a85b..cea659e0d 100644 --- a/test/unit/cli/options-test.js +++ b/test/unit/cli/options-test.js @@ -38,17 +38,20 @@ var packageJsonMock = { var mockWebrootLocator = simple.stub() +var hoodieDefaults = { + port: 'hoodie-default-port', + public: 'hoodie-default-public', + dbUrl: 'hoodie-default-dbUrl', + dbAdapter: 'hoodie-default-dbAdapter', + address: 'hoodie-localhost', + plugins: [] +} + function createCliOptionsProxy (yargsApi) { return proxyquire('../../../cli/options', { 'npmlog': { warn: simple.spy() }, './hoodie-defaults': function () { - return { - port: 'hoodie-default-port', - public: 'hoodie-default-public', - dbUrl: 'hoodie-default-dbUrl', - dbAdapter: 'hoodie-default-dbAdapter', - address: 'hoodie-localhost' - } + return hoodieDefaults }, './app-defaults': mockAppDefaults(), './webroot-locator': mockWebrootLocator, @@ -127,3 +130,24 @@ test('bindAddress', function (t) { t.end() }) + +test('app plugin', function (t) { + var getOptions = proxyquire('../../../cli/options', { + 'fs': { + existsSync: simple.stub().returnWith(true) + }, + 'npmlog': { warn: simple.spy() }, + './hoodie-defaults': function () { + return hoodieDefaults + }, + './app-defaults': mockAppDefaults(), + './webroot-locator': mockWebrootLocator, + 'yargs': createYargsMock({}), + '../package.json': packageJsonMock + }) + + var options = getOptions('project-path') + t.deepEqual(options.plugins, ['project-path']) + + t.end() +}) diff --git a/test/unit/client/bundle-handler-factory-test.js b/test/unit/client/bundle-handler-factory-test.js index 265582449..16dc37696 100644 --- a/test/unit/client/bundle-handler-factory-test.js +++ b/test/unit/client/bundle-handler-factory-test.js @@ -29,12 +29,12 @@ test('bundle-handler-factory', function (group) { done() }) - group.test('passes clientPath, targetPath and config to bundleClient', function (t) { + group.test('passes clientPath, targetPath and options to bundleClient', function (t) { var clientPath = '/client/path' var targetPath = '/target/path' - var config = {} + var options = {} - var handler = createHandler(clientPath, targetPath, config) + var handler = createHandler(clientPath, targetPath, options) handler() t.is(bundleStub.callCount, 1, 'bundleClient gets called') @@ -42,7 +42,7 @@ test('bundle-handler-factory', function (group) { t.is(bundleStubArgs[0], clientPath, 'passes clientPath as first argument') t.is(bundleStubArgs[1], targetPath, 'passes targetPath as second argument') - t.equals(bundleStubArgs[2], config, 'passes config as third argument') + t.equals(bundleStubArgs[2], options, 'passes options as third argument') t.end() }) diff --git a/test/unit/plugins-client-bundle-test.js b/test/unit/client/bundle-test.js similarity index 80% rename from test/unit/plugins-client-bundle-test.js rename to test/unit/client/bundle-test.js index 2672829f7..acb42f8bd 100644 --- a/test/unit/plugins-client-bundle-test.js +++ b/test/unit/client/bundle-test.js @@ -8,14 +8,16 @@ require('npmlog').level = 'error' test('bundle client', function (group) { group.test('client & bundle with same mtime', function (t) { var readFileMock = simple.stub().callbackWith(null, Buffer.from('bundle content')) - var bundleClient = proxyquire('../../server/plugins/client/bundle', { + var bundleClient = proxyquire('../../../server/plugins/client/bundle', { fs: { readFile: readFileMock, stat: simple.stub().callbackWith(null, {mtime: new Date()}) } }) - bundleClient('client.js', 'bundle.js', {}, function (error) { + bundleClient('client.js', 'bundle.js', {plugins: [ + 'hoodie-plugin-foobar' + ]}, function (error) { t.error(error) t.is(readFileMock.callCount, 1, 'readFile called once') @@ -32,7 +34,7 @@ test('bundle client', function (group) { var streamStub = { push: simple.stub() } - var bundleClient = proxyquire('../../server/plugins/client/bundle', { + var bundleClient = proxyquire('../../../server/plugins/client/bundle', { browserify: function () { return { bundle: bundleMock, @@ -53,7 +55,7 @@ test('bundle client', function (group) { } }) - bundleClient('client.js', 'bundle.js', {}, function (error, buffer) { + bundleClient('client.js', 'bundle.js', {plugins: []}, function (error, buffer) { t.error(error) t.is(bundleMock.callCount, 1, 'bundle called once') @@ -77,7 +79,7 @@ test('bundle client', function (group) { var streamStub = { push: simple.stub() } - var bundleClient = proxyquire('../../server/plugins/client/bundle', { + var bundleClient = proxyquire('../../../server/plugins/client/bundle', { browserify: function () { return { bundle: bundleMock, @@ -99,10 +101,11 @@ test('bundle client', function (group) { }) bundleClient('client.js', 'bundle.js', { - url: 'https://myapp.com', client: { foo: 'bar' - } + }, + plugins: [], + url: 'https://myapp.com' }, function (error, buffer) { t.error(error) @@ -120,14 +123,14 @@ test('bundle client', function (group) { }) group.test('fs.readFile fails', function (t) { - var bundleClient = proxyquire('../../server/plugins/client/bundle', { + var bundleClient = proxyquire('../../../server/plugins/client/bundle', { fs: { readFile: simple.stub().callbackWith(new Error('boom')), stat: simple.stub().callbackWith(null, {mtime: new Date()}) } }) - bundleClient('client.js', 'bundle.js', {}, function (error) { + bundleClient('client.js', 'bundle.js', {plugins: []}, function (error) { t.is(error.message, 'boom', 'passes error') t.end() @@ -137,7 +140,7 @@ test('bundle client', function (group) { group.test('browserify.build failing throws error', function (t) { var testError = new Error('TestError') - var bundleClient = proxyquire('../../server/plugins/client/bundle', { + var bundleClient = proxyquire('../../../server/plugins/client/bundle', { browserify: function () { return { bundle: simple.stub().callbackWith(testError), @@ -149,7 +152,7 @@ test('bundle client', function (group) { } }) - bundleClient('client.js', 'bundle.js', {}, function (error) { + bundleClient('client.js', 'bundle.js', {plugins: []}, function (error) { t.ok(error) t.equal(error, testError) t.end() @@ -162,11 +165,11 @@ test('bundle client', function (group) { var unspecifiedError = new Error('UNSPECIFIED_ERROR') var savedPath = process.cwd() var resolverMock = simple.stub().callFn(function (path) { - if (path === pathResolve(__dirname, '../fixture/app-dir-with-server/hoodie/client')) { - return pathResolve(__dirname, '../fixture/app-dir-with-server/hoodie/client.js') + if (path === pathResolve(__dirname, '../../fixture/app-dir-with-server/hoodie/client')) { + return pathResolve(__dirname, '../../fixture/app-dir-with-server/hoodie/client.js') } - if (path === pathResolve(__dirname, '../fixture/hoodie/client')) { + if (path === pathResolve(__dirname, '../../fixture/hoodie/client')) { throw unspecifiedError } @@ -183,7 +186,7 @@ test('bundle client', function (group) { readFile: undefined, stat: undefined } - var bundleClient = proxyquire('../../server/plugins/client/bundle', { + var bundleClient = proxyquire('../../../server/plugins/client/bundle', { browserify: function () { return { bundle: bundleMock, @@ -204,7 +207,7 @@ test('bundle client', function (group) { t.test('client module exists and is newer', function (t) { streamStub.push = simple.stub() fsMock.stat = simple.stub().callFn(function (path, callback) { - if (path === pathResolve(__dirname, '../fixture/app-dir-with-server/hoodie/client.js')) { + if (path === pathResolve(__dirname, '../../fixture/app-dir-with-server/hoodie/client.js')) { return callback(null, {mtime: currentDate}) } if (path === 'client.js') { @@ -214,8 +217,9 @@ test('bundle client', function (group) { return callback(null, {mtime: oneHourAgo}) }) fsMock.readFile = simple.stub() - process.chdir(pathResolve(__dirname, '../fixture/app-dir-with-server/')) + process.chdir(pathResolve(__dirname, '../../fixture/app-dir-with-server/')) bundleClient('client.js', 'bundle.js', { + plugins: [], url: 'https://myapp.com' }, function (error, buffer) { t.error(error) @@ -229,7 +233,7 @@ test('bundle client', function (group) { ' PouchDB: require("pouchdb-browser")\n' + '}\n' + 'module.exports = new Hoodie(options)\n' + - ' .plugin(require("' + pathResolve(__dirname, '../fixture/app-dir-with-server/hoodie/client') + '"))\n' + ' .plugin(require("' + pathResolve(__dirname, '../../fixture/app-dir-with-server/hoodie/client') + '"))\n' t.is(streamStub.push.calls[0].arg, expected) t.end() @@ -239,7 +243,7 @@ test('bundle client', function (group) { t.test('client module exists and is older', function (t) { streamStub.push = simple.stub() fsMock.stat = simple.stub().callFn(function (path, callback) { - if (path === pathResolve(__dirname, '../fixture/app-dir-with-server/hoodie/client.js')) { + if (path === pathResolve(__dirname, '../../fixture/app-dir-with-server/hoodie/client.js')) { return callback(null, {mtime: oneHourAgo}) } if (path === 'client.js') { @@ -249,8 +253,9 @@ test('bundle client', function (group) { return callback(null, {mtime: currentDate}) }) fsMock.readFile = simple.stub().callbackWith(null, Buffer.from('bundle content')) - process.chdir(pathResolve(__dirname, '../fixture/app-dir-with-server/')) + process.chdir(pathResolve(__dirname, '../../fixture/app-dir-with-server/')) bundleClient('client.js', 'bundle.js', { + plugins: [], url: 'https://myapp.com' }, function (error, buffer) { t.error(error) @@ -266,7 +271,7 @@ test('bundle client', function (group) { t.test('client module doesn\'t exist', function (t) { streamStub.push = simple.stub() fsMock.stat = simple.stub().callFn(function (path, callback) { - if (path === pathResolve(__dirname, '../fixture/app-dir-without-server/hoodie/client.js')) { + if (path === pathResolve(__dirname, '../../fixture/app-dir-without-server/hoodie/client.js')) { throw new Error('Boom') } if (path === 'client.js') { @@ -276,8 +281,9 @@ test('bundle client', function (group) { callback(new Error('Boom')) }) fsMock.readFile = simple.stub() - process.chdir(pathResolve(__dirname, '../fixture/app-dir-without-server/')) + process.chdir(pathResolve(__dirname, '../../fixture/app-dir-without-server/')) bundleClient('client.js', 'bundle.js', { + plugins: [], url: 'https://myapp.com' }, function (error, buffer) { t.error(error) @@ -299,9 +305,10 @@ test('bundle client', function (group) { t.test('require.resolve rethrows other errors', function (t) { streamStub.push = simple.stub() fsMock.readFile = simple.stub() - process.chdir(pathResolve(__dirname, '../fixture/')) + process.chdir(pathResolve(__dirname, '../../fixture/')) t.throws(function () { bundleClient('client.js', 'bundle.js', { + plugins: [], url: 'https://myapp.com' }, function () { t.fail() diff --git a/test/unit/client/client-test.js b/test/unit/client/client-test.js index d3142653a..f815c2067 100644 --- a/test/unit/client/client-test.js +++ b/test/unit/client/client-test.js @@ -26,25 +26,25 @@ test('client', function (group) { group.test('always calls next()', function (t) { var next = simple.stub() - client.register(mockServer, {config: {}}, next) + client.register(mockServer, {}, next) t.is(next.callCount, 1, 'Calls next') t.end() }) - group.test('passes options.config on to the handlerFactory', function (t) { - var configMock = {} + group.test('passes options on to the handlerFactory', function (t) { + var optionsMock = {} var next = simple.stub() - client.register(mockServer, {config: configMock}, next) + client.register(mockServer, optionsMock, next) var handlerArgs = createBundleHandlerStub.lastCall.args - t.equals(handlerArgs[2], configMock) + t.equals(handlerArgs[2], optionsMock) t.end() }) - group.test('builds targetPath from config.data + "client.js"', function (t) { - var optionsMock = {config: {data: '/example/path'}} + group.test('builds targetPath from options.data + "client.js"', function (t) { + var optionsMock = {data: '/example/path'} client.register(mockServer, optionsMock, simple.stub()) var handlerArgs = createBundleHandlerStub.lastCall.args @@ -53,7 +53,7 @@ test('client', function (group) { }) group.test('builds targetPath from folder ".hoodie" by default', function (t) { - client.register(mockServer, {config: {}}, simple.stub()) + client.register(mockServer, {}, simple.stub()) var handlerArgs = createBundleHandlerStub.lastCall.args t.is(handlerArgs[1], path.join('.hoodie', 'client.js')) diff --git a/test/unit/server/loader-test.js b/test/unit/server/loader-test.js deleted file mode 100644 index b03a85862..000000000 --- a/test/unit/server/loader-test.js +++ /dev/null @@ -1,46 +0,0 @@ -var simple = require('simple-mock') -var test = require('tap').test -var proxyquire = require('proxyquire').noCallThru() - -var mockResolver = simple.stub() -var loader = proxyquire('../../../server/plugins', { - './resolver': mockResolver -}) -var registerPluginsError = new Error('Plugin Register Error') -var serverMock = { - register: simple.stub().callbackWith(registerPluginsError) -} - -test('when require.resolve errors', function (t) { - var options = { - paths: { - public: 'public' - } - } - - var error = new Error('Unspecified error') - mockResolver.throwWith(error) - - t.throws(function () { - loader(serverMock, options, function () { - t.fail('the error has not been rethrown') - }) - }, error, 'the error is rethrown') - t.end() -}) - -test('when registerPlugins errors', function (t) { - var options = { - paths: { - public: 'public' - } - } - var error = new Error('Module Not Found Error') - error.code = 'MODULE_NOT_FOUND' - mockResolver.throwWith(error) - loader(serverMock, options, function (error) { - t.ok(error) - t.equal(error, registerPluginsError) - t.end() - }) -}) diff --git a/test/unit/server/plugins-test.js b/test/unit/server/plugins-test.js new file mode 100644 index 000000000..c5576e518 --- /dev/null +++ b/test/unit/server/plugins-test.js @@ -0,0 +1,131 @@ +var simple = require('simple-mock') +var test = require('tap').test +var proxyquire = require('proxyquire').noCallThru() + +var mockResolver = simple.stub() +var registerPlugins = proxyquire('../../../server/plugins', { + './resolver': mockResolver, + 'hoodie-plugin-exists/package.json': { + name: 'exists' + }, + 'hoodie-plugin-exists/hoodie/server': {}, + 'hoodie-plugin-exists-with-hoodie-option/package.json': { + name: 'exists-with-hoodie-option', + hoodie: {} + }, + 'hoodie-plugin-exists-with-hoodie-option/hoodie/server': {}, + 'hoodie-plugin-exists-with-custom-name/package.json': { + name: 'exists-with-hoodie-option', + hoodie: { + name: 'custom-name' + } + }, + 'hoodie-plugin-exists-with-custom-name/hoodie/server': {} +}) +var registerPluginsError = new Error('Plugin Register Error') +var serverMock = { + register: simple.stub().callbackWith(registerPluginsError) +} + +test('when require.resolve errors', function (t) { + var options = { + paths: { + public: 'public' + }, + plugins: [ + 'hoodie-plugin-foobar' + ] + } + + var error = new Error('Unspecified error') + mockResolver.throwWith(error) + + t.throws(function () { + registerPlugins(serverMock, options, function () { + t.fail('the error has not been rethrown') + }) + }, error, 'the error is rethrown') + t.end() +}) + +test('when requiring plugin succeeds', function (t) { + var options = { + paths: { + public: 'public' + }, + plugins: [ + 'hoodie-plugin-exists' + ] + } + + mockResolver.callFn(function (path) { + return path + }) + + registerPlugins(serverMock, options, function (error) { + t.ok(error) + t.equal(error, registerPluginsError) + t.end() + }) +}) + +test('when requiring plugin with hoodie', function (t) { + var options = { + paths: { + public: 'public' + }, + plugins: [ + 'hoodie-plugin-exists-with-hoodie-option' + ] + } + + mockResolver.callFn(function (path) { + return path + }) + + registerPlugins(serverMock, options, function (error) { + t.ok(error) + t.equal(error, registerPluginsError) + t.end() + }) +}) + +test('when requiring plugin with custom name', function (t) { + var options = { + paths: { + public: 'public' + }, + plugins: [ + 'hoodie-plugin-exists-with-custom-name' + ] + } + + mockResolver.callFn(function (path) { + return path + }) + + registerPlugins(serverMock, options, function (error) { + t.ok(error) + t.equal(error, registerPluginsError) + t.end() + }) +}) + +test('when requiring plugin errors', function (t) { + var options = { + paths: { + public: 'public' + }, + plugins: [ + 'hoodie-plugin-error' + ] + } + var error = new Error('Module Not Found Error') + error.code = 'MODULE_NOT_FOUND' + mockResolver.throwWith(error) + registerPlugins(serverMock, options, function (error) { + t.ok(error) + t.equal(error, registerPluginsError) + t.end() + }) +}) diff --git a/test/unit/server/public-test.js b/test/unit/server/public-test.js index 4d9fa763d..821b34f95 100644 --- a/test/unit/server/public-test.js +++ b/test/unit/server/public-test.js @@ -1,33 +1,139 @@ +var _ = require('lodash') +var proxyquire = require('proxyquire').noCallThru() var simple = require('simple-mock') var test = require('tap').test +var serverMock = { + register: simple.stub(), + route: simple.stub(), + ext: simple.stub() +} + +function loadPublicPlugin (options, resolver, done) { + var mergedOptions = _.merge({ + paths: { + public: 'my-custom-public-directory' + }, + plugins: [] + }, options) + + proxyquire('../../../server/plugins/public', { + './resolver': resolver, + 'hoodie-plugin-with-public/package.json': { + name: 'plugin-public1' + }, + 'hoodie-plugin-with-public-and-custom-name/package.json': { + name: 'plugin-public2', + hoodie: { + name: 'custom-name' + } + }, + 'hoodie-plugin-with-public-and-hoodie-option/package.json': { + name: 'plugin-public3', + hoodie: {} + } + }).register(serverMock, mergedOptions, done) +} + test('public', function (group) { - var serverMock + group.test('configured public route', function (t) { + function resolver (path) { + return path + } + + loadPublicPlugin({}, resolver, function (error) { + t.error(error) + t.equal(serverMock.route.callCount, 1, 'route was called') + t.type(serverMock.route.lastCall.arg, Array, 'route was called with an array') + t.contains(serverMock.route.lastCall.arg[0], + {'handler': {'directory': {'path': /^my-custom-public-directory$/}}}, + 'route was configured to correct public directory') + t.end() + }) + }) + + group.test('plugin with public path', function (t) { + function resolver (path) { + return path + } + + loadPublicPlugin({ + plugins: ['hoodie-plugin-with-public'] + }, resolver, function (error) { + t.error(error) + + var pluginRouteOptions = serverMock.route.lastCall.arg.pop() + t.is(pluginRouteOptions.path, '/hoodie/plugin-public1/{p*}') + t.end() + }) + }) + + group.test('plugin with custom name and public path', function (t) { + function resolver (path) { + return path + } + + loadPublicPlugin({ + plugins: ['hoodie-plugin-with-public-and-custom-name'] + }, resolver, function (error) { + t.error(error) + + var pluginRouteOptions = serverMock.route.lastCall.arg.pop() + t.is(pluginRouteOptions.path, '/hoodie/custom-name/{p*}') + t.end() + }) + }) - group.beforeEach(function (done) { - serverMock = { - register: simple.stub(), - route: simple.stub(), - ext: simple.stub() + group.test('plugin with custom name and public path', function (t) { + function resolver (path) { + return path } - require('../../../server/plugins/public').register(serverMock, { - config: { - paths: { - public: 'my-custom-public-directory' - } + loadPublicPlugin({ + plugins: ['hoodie-plugin-with-public-and-hoodie-option'] + }, resolver, function (error) { + t.error(error) + + var pluginRouteOptions = serverMock.route.lastCall.arg.pop() + t.is(pluginRouteOptions.path, '/hoodie/plugin-public3/{p*}') + t.end() + }) + }) + + group.test('plugin without public path', function (t) { + var error = new Error('Module Not Found Error') + error.code = 'MODULE_NOT_FOUND' + + function resolver (path) { + if (/hoodie-plugin-without-public/.test(path)) { + throw error } - }, function () { }) - done() + return path + } + + loadPublicPlugin({ + plugins: ['hoodie-plugin-without-public'] + }, resolver, function (error) { + t.error(error) + t.end() + }) }) - group.test('configured public route', function (t) { - t.equal(serverMock.route.callCount, 1, 'route was called') - t.type(serverMock.route.lastCall.args[0], Array, 'route was called with an array') - t.contains(serverMock.route.lastCall.args[0][0], - {'handler': {'directory': {'path': /^my-custom-public-directory$/}}}, - 'route was configured to correct public directory') + group.test('require error', function (t) { + function resolver (path) { + if (/hoodie-plugin-foobar/.test(path)) { + throw new Error('Oops') + } + + return path + } + + t.throws(function () { + loadPublicPlugin({ + plugins: ['hoodie-plugin-foobar'] + }, resolver, function () {}) + }) t.end() })