Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock plugins tests #131

Merged
merged 4 commits into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cli/domain/get-mocked-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
var fs = require('fs-extra');
var path = require('path');
var _ = require('underscore');
var colors = require('colors/safe');
var strings = require('../../resources/');
var settings = require('../../resources/settings');

var registerStaticMocks = function(mocks, logger){
return _.map(mocks, function(mockedValue, pluginName){
logger.log('├── '.green + pluginName + ' () => ' + mockedValue);
logger.log(colors.green('├── ' + pluginName + ' () => ' + mockedValue));
return {
name: pluginName,
register: {
Expand All @@ -29,11 +30,16 @@ var registerDynamicMocks = function(mocks, logger){
try {
p = require(path.resolve(source));
} catch(er) {
logger.log(er.toString().red);
logger.log(colors.red(er.toString()));
return;
}

logger.log('├── '.green + pluginName + ' () => [Function]');
if(!_.isFunction(p)){
logger.log(colors.red(strings.errors.cli.MOCK_PLUGIN_IS_NOT_A_FUNCTION));
return;
}

logger.log(colors.green('├── ' + pluginName + ' () => [Function]'));
return {
name: pluginName,
register: {
Expand All @@ -43,7 +49,7 @@ var registerDynamicMocks = function(mocks, logger){
execute: p
}
};
});
}).filter(function(p){ return p; });
};

module.exports = function(logger){
Expand All @@ -59,7 +65,7 @@ module.exports = function(logger){
return plugins;
}

logger.log(strings.messages.cli.REGISTERING_MOCKED_PLUGINS.yellow);
logger.log(colors.yellow(strings.messages.cli.REGISTERING_MOCKED_PLUGINS));

plugins = plugins.concat(registerStaticMocks(content.mocks.plugins.static, logger));
plugins = plugins.concat(registerDynamicMocks(content.mocks.plugins.dynamic, logger));
Expand Down
1 change: 1 addition & 0 deletions resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
DEV_FAIL: 'An error happened when initialising the dev runner: {0}',
INIT_FAIL: 'An error happened when initialising the component: {0}',
INVALID_CREDENTIALS: 'Invalid credentials',
MOCK_PLUGIN_IS_NOT_A_FUNCTION: 'Looks like you are trying to register a dynamic mock plugin but the file you specified is not a function',
NAME_NOT_VALID: 'the name is not valid. Allowed characters are alphanumeric, _, -',
NODE_CLI_VERSION_NEEDS_UPGRADE: 'the version of used node is invalid. Try to upgrade node to version matching \'{0}\'',
OC_CLI_VERSION_NEEDS_UPGRADE: 'the version of used OC CLI is invalid. Try to upgrade OC CLI running {0}',
Expand Down
202 changes: 202 additions & 0 deletions test/unit/cli-domain-get-mocked-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
'use strict';

var expect = require('chai').expect;
var injectr = require('injectr');
var path = require('path');
var sinon = require('sinon');
var _ = require('underscore');
var dynamicPluginModule = function(a){ return a ? 'blarg' : 'flarg'; };
var notAFunctionModule = { 'foo' : 'bar' };

var fsMock,
getMockedPlugins;

var initialise = function(fs){

fsMock = _.extend({
existsSync: sinon.stub().returns(true),
readFileSync: sinon.stub().returns('file content'),
readJsonSync: sinon.stub().returns({ content: true }),
writeFile: sinon.stub().yields(null, 'ok')
}, fs || {});

getMockedPlugins = injectr('../../cli/domain/get-mocked-plugins.js', {
'fs-extra': fsMock,
path: {
resolve: function(){
return _.toArray(arguments).join('/');
}
},
'./dynamic-plugin.js': dynamicPluginModule,
'./not-a-function.js': notAFunctionModule
});
};

describe('cli : domain : get-mocked-plugins', function(){

describe('when setting up mocked plugins', function(){

describe('when oc.json is missing', function(){
var result;
beforeEach(function(){
initialise({ existsSync: sinon.stub().returns(false) });
result = getMockedPlugins(console);
});

it('should return an empty array', function(){
expect(result.length).to.equal(0);
});
});

describe('when no plugins are specified', function(){
var result;
var ocJson = {
registries: [],
mocks: {
plugins: {}
}
};

beforeEach(function(){
initialise({
existsSync: sinon.stub().returns(true),
readJsonSync: sinon.stub().returns(ocJson)
});
result = getMockedPlugins({log: sinon.stub()});
});

it('should return an empty array', function(){
expect(result.length).to.equal(0);
});
});

describe('when a static plugin is specified', function(){
var result;
var ocJson = {
registries: [],
mocks: {
plugins: {
static: {
foo: 'bar'
}
}
}
};

beforeEach(function(){
initialise({
existsSync: sinon.stub().returns(true),
readJsonSync: sinon.stub().returns(ocJson)
});
result = getMockedPlugins({log: sinon.stub()});
});

it('should return the static plugin', function(){
expect(result.length).to.equal(1);
expect(result[0].name).to.equal('foo');
});

it('should set up the execute method to return the specified value', function(){
expect(result[0].register.execute()).to.equal('bar');
});
});

describe('when a dynamic plugin is specified', function(){
var result;
var ocJson = {
registries: [],
mocks: {
plugins: {
dynamic: {
foo: './dynamic-plugin.js'
}
}
}
};

beforeEach(function(){
initialise({
existsSync: sinon.stub().returns(true),
readJsonSync: sinon.stub().returns(ocJson)
});
result = getMockedPlugins({ log: sinon.stub() });
});

it('should return the dynamic plugin', function(){
expect(result.length).to.equal(1);
expect(result[0].name).to.equal('foo');
});

it('should set up the execute method to run the module', function(){
expect(result[0].register.execute(false)).to.equal('flarg');
expect(result[0].register.execute(true)).to.equal('blarg');
});
});

describe('when a dynamic plugin is specified and the referenced file is missing', function(){
var result;
var ocJson = {
registries: [],
mocks: {
plugins: {
dynamic: {
foo: './not-exist.js'
}
}
}
};

var logger = { log: sinon.stub() };

beforeEach(function(){
initialise({
existsSync: sinon.stub().returns(true),
readJsonSync: sinon.stub().returns(ocJson)
});
result = getMockedPlugins(logger);
});

it('should log an error', function(){
expect(logger.log.secondCall.args[0]).to.contain(
'Error: Cannot find module');
});

it('should omit the broken plugin from the results', function(){
expect(result.length).to.equal(0);
});
});

describe('when a dynamic plugin is specified and the module is not a function', function(){
var result;
var ocJson = {
registries: [],
mocks: {
plugins: {
dynamic: {
foo: './not-a-function.js'
}
}
}
};

var logger = { log: sinon.stub() };

beforeEach(function(){
initialise({
existsSync: sinon.stub().returns(true),
readJsonSync: sinon.stub().returns(ocJson)
});
result = getMockedPlugins(logger);
});

it('should log an error', function(){
expect(logger.log.secondCall.args[0]).to.contain(
'Looks like you are trying to register a dynamic mock plugin but the file you specified is not a function');
});

it('should omit the broken plugin from the results', function(){
expect(result.length).to.equal(0);
});
});
});
});