You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2017/09/07 03:22:34 UTC
[3/4] cordova-lib git commit: CB-12361: added main function unit
tests for plugin add.spec.js
CB-12361: added main function unit tests for plugin add.spec.js
Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/7e136aba
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/7e136aba
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/7e136aba
Branch: refs/heads/master
Commit: 7e136aba2099767c0cf24376b068bf513d89e21c
Parents: 6f5be6a
Author: Steve Gill <st...@gmail.com>
Authored: Mon Jun 26 12:32:45 2017 -0600
Committer: Steve Gill <st...@gmail.com>
Committed: Wed Sep 6 00:32:59 2017 -0700
----------------------------------------------------------------------
spec/cordova/plugin/add.spec.js | 158 ++++++++++++++++++++++++++++++++---
spec/cordova/prepare.spec.js | 9 +-
src/cordova/plugin/add.js | 1 +
3 files changed, 154 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/7e136aba/spec/cordova/plugin/add.spec.js
----------------------------------------------------------------------
diff --git a/spec/cordova/plugin/add.spec.js b/spec/cordova/plugin/add.spec.js
index 55c228b..9abced8 100644
--- a/spec/cordova/plugin/add.spec.js
+++ b/spec/cordova/plugin/add.spec.js
@@ -21,16 +21,69 @@
/* globals fail */
var Q = require('q');
-var add = require('../../../src/cordova/plugin/add');
+var rewire = require('rewire');
+var add = rewire('../../src/cordova/plugin/add');
+var plugman = require('../../src/plugman/plugman');
+var cordova_util = require('../../src/cordova/util');
+var path = require('path');
+var fs = require('fs');
+var config = require('../../src/cordova/config');
describe('cordova/plugin/add', function () {
var projectRoot = '/some/path';
var hook_mock;
+ var cfg_parser_mock = function () {};
+ var cfg_parser_revert_mock;
+ var plugin_info_provider_mock = function () {};
+ var plugin_info_provider_revert_mock;
+ var plugin_info;
+
beforeEach(function () {
hook_mock = jasmine.createSpyObj('hooks runner mock', ['fire']);
hook_mock.fire.and.returnValue(Q());
+ cfg_parser_mock.prototype = jasmine.createSpyObj('config parser prototype mock', ['getPlugin', 'removePlugin', 'addPlugin', 'write']);
+ cfg_parser_mock.prototype.getPlugin.and.callFake(function (pluginId) {});
+ cfg_parser_mock.prototype.removePlugin.and.callFake(function () {});
+ cfg_parser_mock.prototype.addPlugin.and.callFake(function () {});
+ cfg_parser_mock.prototype.write.and.callFake(function () {});
+ cfg_parser_revert_mock = add.__set__('ConfigParser', cfg_parser_mock);
+ plugin_info = jasmine.createSpyObj('pluginInfo', ['getPreferences']);
+ plugin_info.getPreferences.and.returnValue({});
+ plugin_info.dir = 'some\plugin\path';
+ plugin_info.id = 'cordova-plugin-device';
+ plugin_info.version = '1.0.0';
+ plugin_info_provider_mock.prototype = jasmine.createSpyObj('plugin info provider mock', ['get']);
+ plugin_info_provider_mock.prototype.get = function (directory) {
+ console.log('fake get');
+ //id version dir getPreferences() engines engines.cordovaDependencies name versions
+ return plugin_info;
+ };
+ plugin_info_provider_revert_mock = add.__set__('PluginInfoProvider', plugin_info_provider_mock);
+ spyOn(fs,'existsSync').and.returnValue(false);
+ spyOn(fs,'writeFileSync').and.returnValue(false);
+ //requireNoCache is used to require package.json
+ spyOn(cordova_util, 'requireNoCache').and.returnValue({});
+ });
+ afterEach(function () {
+ cfg_parser_revert_mock();
+ plugin_info_provider_revert_mock();
});
describe('main method', function () {
+
+ beforeEach(function () {
+ spyOn(add,'determinePluginTarget').and.callFake(function(projRoot, cfg, target, opts) {
+ return Q(target);
+ });
+ spyOn(plugman, 'fetch').and.callFake(function (target, pluginPath, opts) {
+ return Q(target);
+ });
+ spyOn(plugman, 'install').and.returnValue(Q(true));
+ spyOn(cordova_util, 'listPlatforms').and.callFake(function () {
+ return ['android'];
+ });
+ spyOn(cordova_util,'findPlugins').and.returnValue({plugins:[]});
+ spyOn(config, 'read').and.returnValue({});
+ });
describe('error/warning conditions', function () {
it('should error out if at least one plugin is not specified', function (done) {
add(projectRoot, hook_mock, {plugins: []}).then(function () {
@@ -39,17 +92,102 @@ describe('cordova/plugin/add', function () {
expect(e.message).toContain('No plugin specified');
}).done(done);
});
- it('should error out if any mandatory plugin variables are not provided');
+ it('should error out if any mandatory plugin variables are not provided', function (done) {
+ plugin_info.getPreferences.and.returnValue({'some':undefined});
+
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device']}).then(function () {
+ fail('success handler unexpectedly invoked');
+ }).fail(function (e) {
+ expect(e.message).toContain('Variable(s) missing (use: --variable');
+ }).done(done);
+ });
});
describe('happy path', function () {
- it('should fire the before_plugin_add hook');
- it('should determine where to fetch a plugin from using determinePluginTarget and invoke plugman.fetch with the resolved target');
- it('should retrieve any variables for the plugin from config.xml and provide them as cli variables only when the cli variables are not already provided via options');
- it('should invoke plugman.install for each platform added to the project');
- it('should save plugin variable information to package.json file (if exists)');
- it('should overwrite plugin information in config.xml after a successful installation');
- it('should invoke preparePlatforms if plugman.install returned a truthy value');
- it('should fire after_plugin_add hook');
+ it('should fire the before_plugin_add hook', function (done) {
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device']}).then(function () {
+ expect(hook_mock.fire).toHaveBeenCalledWith('before_plugin_add', jasmine.any(Object));
+ }).fail(function (e) {
+ fail('fail handler unexpectedly invoked');
+ console.log(e);
+ }).done(done);
+ });
+ it('should determine where to fetch a plugin from using determinePluginTarget and invoke plugman.fetch with the resolved target', function (done) {
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device']}).then(function () {
+ expect(add.determinePluginTarget).toHaveBeenCalledWith(projectRoot, jasmine.any(Object), 'cordova-plugin-device', jasmine.any(Object));
+ expect(plugman.fetch).toHaveBeenCalledWith('cordova-plugin-device', path.join(projectRoot,'plugins'), jasmine.any(Object));
+ }).fail(function (e) {
+ fail('fail handler unexpectedly invoked');
+ console.log(e);
+ }).done(done);
+ });
+ it('should retrieve any variables for the plugin from config.xml and add them as cli variables only when the variables were not already provided via options', function (done) {
+ var cfg_plugin_variables = {'some':'variable'};
+ cfg_parser_mock.prototype.getPlugin.and.callFake(function (plugin_id) {
+ return {'variables': cfg_plugin_variables};
+ });
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device']}).then(function () {
+ //confirm cli_variables are undefind
+ expect(add.determinePluginTarget.calls.argsFor(0)[3]['variables']).toBeUndefined;
+ expect(plugman.install).toHaveBeenCalled();
+ //check that the plugin variables from config.xml got added to cli_variables
+ expect(plugman.install.calls.argsFor(0)[4]['cli_variables']).toEqual(cfg_plugin_variables);
+ }).fail(function (e) {
+ fail('fail handler unexpectedly invoked');
+ console.log(e);
+ }).done(done);
+ });
+ it('should invoke plugman.install for each platform added to the project', function (done) {
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device']}).then(function () {
+ expect(plugman.install).toHaveBeenCalledWith('android', jasmine.any(String), jasmine.any(String), jasmine.any(String), jasmine.any(Object));
+ }).fail(function (e) {
+ fail('fail handler unexpectedly invoked');
+ console.log(e);
+ }).done(done);
+ });
+ it('should save plugin variable information to package.json file (if exists)', function (done) {
+ var cli_plugin_variables = {'some':'variable'};
+
+ fs.existsSync.and.returnValue(true);
+
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device'], cli_variables: cli_plugin_variables, save:'true'}).then(function () {
+ expect(fs.writeFileSync).toHaveBeenCalledWith(jasmine.any(String), JSON.stringify({'cordova':{'plugins':{'cordova-plugin-device':cli_plugin_variables}}}, null, 2), 'utf8');
+ }).fail(function (e) {
+ fail('fail handler unexpectedly invoked');
+ console.log(e);
+ }).done(done);
+ });
+ it('should overwrite plugin information in config.xml after a successful installation', function (done) {
+ var cfg_plugin_variables = {'some':'variable'};
+ var cli_plugin_variables = {'some':'new_variable'};
+ cfg_parser_mock.prototype.getPlugin.and.callFake(function (plugin_id) {
+ return {'variables': cfg_plugin_variables};
+ });
+
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device'], cli_variables: cli_plugin_variables, save:'true'}).then(function () {
+ //confirm cli_variables got passed through
+ expect(add.determinePluginTarget.calls.argsFor(0)[3]['variables']).toEqual(cli_plugin_variables);
+ //check that the plugin variables from config.xml got added to cli_variables
+ expect(plugman.install.calls.argsFor(0)[4]['cli_variables']).toEqual(cli_plugin_variables);
+ expect(cfg_parser_mock.prototype.removePlugin).toHaveBeenCalledWith('cordova-plugin-device');
+ expect(cfg_parser_mock.prototype.addPlugin).toHaveBeenCalledWith(jasmine.any(Object), cli_plugin_variables);
+ expect(cfg_parser_mock.prototype.write).toHaveBeenCalled();
+ }).fail(function (e) {
+ fail('fail handler unexpectedly invoked');
+ console.log(e);
+ }).done(done);
+ });
+ //can't test the following due to inline require of preparePlatforms
+ xit('should invoke preparePlatforms if plugman.install returned a falsey value', function () {
+ plugman.install.and.returnValue(false);
+ });
+ it('should fire after_plugin_add hook', function (done) {
+ add(projectRoot, hook_mock, {plugins: ['cordova-plugin-device']}).then(function () {
+ expect(hook_mock.fire).toHaveBeenCalledWith('after_plugin_add', jasmine.any(Object));
+ }).fail(function (e) {
+ fail('fail handler unexpectedly invoked');
+ console.log(e);
+ }).done(done);
+ });
});
});
describe('determinePluginTarget helper method', function () {
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/7e136aba/spec/cordova/prepare.spec.js
----------------------------------------------------------------------
diff --git a/spec/cordova/prepare.spec.js b/spec/cordova/prepare.spec.js
index 1467587..c15497a 100644
--- a/spec/cordova/prepare.spec.js
+++ b/spec/cordova/prepare.spec.js
@@ -77,7 +77,7 @@ describe('cordova/prepare', function () {
throw new Error('preProcessOption error');
});
prepare({}).then(function () {
- fail('unexpected failure handler invoked');
+ fail('unexpected success handler invoked');
}).fail(function (e) {
expect(e.message).toBe('preProcessOption error');
expect(HooksRunner.prototype.fire).toHaveBeenCalledWith('before_prepare', jasmine.any(Object));
@@ -87,11 +87,12 @@ describe('cordova/prepare', function () {
util.cdProjectRoot.and.callFake(function () {
throw new Error('cdProjectRoot error');
});
- prepare({}).then(function () {
- fail('unexpected failure handler invoked');
+
+ prepare({}).then(function () {
+ fail('unexpected success handler invoked');
}).fail(function (e) {
expect(e.message).toBe('cdProjectRoot error');
- expect(HooksRunner.prototype.fire).not.toHaveBeenCalledWith();
+ expect(HooksRunner.prototype.fire).not.toHaveBeenCalled();
}).done(done);
});
});
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/7e136aba/src/cordova/plugin/add.js
----------------------------------------------------------------------
diff --git a/src/cordova/plugin/add.js b/src/cordova/plugin/add.js
index 66bec9c..5db0805 100644
--- a/src/cordova/plugin/add.js
+++ b/src/cordova/plugin/add.js
@@ -138,6 +138,7 @@ function add (projectRoot, hooksRunner, opts) {
var pkgJsonPath = path.join(projectRoot, 'package.json');
// save to config.xml
+ // TODO: no need to have saveToConfigXMLOn anymore. Should just check opts.save instead
if (plugin_util.saveToConfigXmlOn(config_json, opts)) {
// If statement to see if pkgJsonPath exists in the filesystem
if (fs.existsSync(pkgJsonPath)) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org