Skip to content
Browse files

Extract mock commander out of cli to mock module.

  • Loading branch information...
1 parent 7f730a9 commit 883383a7ca51127e4b8371c5a4223769d90511fc @cliffano committed
Showing with 112 additions and 31 deletions.
  1. +55 −0 lib/mock.js
  2. +1 −31 test/cli.js
  3. +56 −0 test/mock.js
View
55 lib/mock.js
@@ -45,6 +45,60 @@ function childProcess(checks, mocks) {
}
/**
+ * mock#commander(checks[, mocks])
+ * - checks (Object): exec values storage, to be asserted in test client
+ * - mocks (Object): mock values storage
+ *
+ * Mock commander module.
+ **/
+function commander(checks, mocks) {
+
+ checks.commander_actions = [];
+ checks.commander_commands = [];
+ checks.commander_descs = [];
+ checks.commander_options = [];
+
+ function action(act) {
+ checks.commander_actions.push(act);
+ }
+
+ function command(name) {
+ checks.commander_commands.push(name);
+ return this;
+ }
+
+ function description(desc) {
+ checks.commander_descs.push(desc);
+ return this;
+ }
+
+ function option(short, long, desc) {
+ checks.commander_options.push({
+ short: short,
+ long: long,
+ desc: desc
+ });
+ }
+
+ function parse(argv) {
+ checks.commander_parse = argv;
+ }
+
+ function version(ver) {
+ checks.commander_version = ver;
+ }
+
+ return {
+ action: action,
+ command: command,
+ description: description,
+ option: option,
+ parse: parse,
+ version: version
+ };
+}
+
+/**
* mock#console(checks)
* - checks (Object): exec values storage, to be asserted in test client
*
@@ -153,6 +207,7 @@ function socket(checks, mocks) {
}
exports.childProcess = childProcess;
+exports.commander = commander;
exports.console = console;
exports.fs = fs;
exports.process = process;
View
32 test/cli.js
@@ -148,42 +148,12 @@ describe('cli', function () {
describe('parse', function () {
it('should set command details when commander module is used', function () {
- checks.commander_actions = [];
- checks.commander_commands = [];
- checks.commander_descs = [];
- checks.commander_options = [];
- var commander = {
- action: function (action) {
- checks.commander_actions.push(action);
- },
- command: function (name) {
- checks.commander_commands.push(name);
- return this;
- },
- description: function (desc) {
- checks.commander_descs.push(desc);
- return this;
- },
- option: function (short, long, desc) {
- checks.commander_options.push({
- short: short,
- long: long,
- desc: desc
- });
- },
- parse: function (argv) {
- checks.commander_parse = argv;
- },
- version: function (version) {
- checks.commander_version = version;
- }
- };
mocks = {
'fs_readFileSync_/app/foo/package.json': '{ "version": "1.2.3" }',
'process_argv': { argv: [ 'node', '/somedir', 'cmd1' ] }
};
mocks.requires = {
- commander: commander,
+ commander: bag.mock.commander(checks, mocks),
fs: bag.mock.fs (checks, mocks)
};
cli = create(checks, mocks);
View
56 test/mock.js
@@ -37,6 +37,62 @@ describe('mock', function () {
});
});
+ describe('commander', function () {
+
+ it('should register action functions when action is called multiple times', function () {
+ var commander = mock.commander(checks, mocks);
+ commander.action(function () {});
+ commander.action(function () {});
+ checks.commander_actions.length.should.equal(2);
+ checks.commander_actions[0].should.be.a('function');
+ checks.commander_actions[1].should.be.a('function');
+ });
+
+ it('should register command names when command is called multiple times in a chain', function () {
+ var commander = mock.commander(checks, mocks);
+ commander.command('cmd1').command('cmd2');
+ checks.commander_commands.length.should.equal(2);
+ checks.commander_commands[0].should.equal('cmd1');
+ checks.commander_commands[1].should.equal('cmd2');
+ });
+
+ it('should register descriptions when description is called multiple times in a chain', function () {
+ var commander = mock.commander(checks, mocks);
+ commander.description('command 1').description('command 2');
+ checks.commander_descs.length.should.equal(2);
+ checks.commander_descs[0].should.equal('command 1');
+ checks.commander_descs[1].should.equal('command 2');
+ });
+
+ it('should register option details when option is called multiple times', function () {
+ var commander = mock.commander(checks, mocks);
+ commander.option('-a', '--aa', 'aaa');
+ commander.option('-b', '--bb', 'bbb');
+ checks.commander_options.length.should.equal(2);
+ checks.commander_options[0].short.should.equal('-a');
+ checks.commander_options[0].long.should.equal('--aa');
+ checks.commander_options[0].desc.should.equal('aaa');
+ checks.commander_options[1].short.should.equal('-b');
+ checks.commander_options[1].long.should.equal('--bb');
+ checks.commander_options[1].desc.should.equal('bbb');
+ });
+
+ it('should set process arguments when parse is called', function () {
+ var commander = mock.commander(checks, mocks);
+ commander.parse(['node', '/somedir', 'cmd1']);
+ checks.commander_parse.length.should.equal(3);
+ checks.commander_parse[0].should.equal('node');
+ checks.commander_parse[1].should.equal('/somedir');
+ checks.commander_parse[2].should.equal('cmd1');
+ });
+
+ it('should set version number when version is called', function () {
+ var commander = mock.commander(checks, mocks);
+ commander.version('1.2.3');
+ checks.commander_version.should.equal('1.2.3');
+ });
+ });
+
describe('console', function () {
it('should set message when console error is called', function () {

0 comments on commit 883383a

Please sign in to comment.
Something went wrong with that request. Please try again.