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

CB-12361 : added tests for plugin/save.js #584

Merged
merged 1 commit into from
Sep 25, 2017
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
4 changes: 2 additions & 2 deletions integration-tests/HooksRunner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ describe('HooksRunner', function () {
}).then(function () {
done();
});
});
}, 60000);

it('Test 006 : should execute hook scripts serially from config.xml', function (done) {
var test_event = 'before_build';
Expand Down Expand Up @@ -636,7 +636,7 @@ describe('HooksRunner', function () {
hooksRunner.fire(test_event, hookOptions).then(function () {
done();
});
}, 60000);
}, 80000);

it('Test 022 : should pass data object that fire calls into sync handlers', function (done) {
var async = function (opts) {
Expand Down
142 changes: 133 additions & 9 deletions spec/cordova/plugin/save.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,30 @@ var rewire = require('rewire');
var fs = require('fs');
var save = rewire('../../../src/cordova/plugin/save');
var cordova_util = require('../../../src/cordova/util');
var semver = require('semver');

describe('cordova/plugin/save', function () {
var projectRoot = '/some/path';
var cfg_parser_mock = function () {};
var cfg_parser_revert_mock;
var fake_plugin_list = ['VRPlugin', 'MastodonSocialPlugin'];
var fake_fetch_json = {'VRPlugin': {}, 'MastodonSocialPlugin': {}};
var plugin_info_provider_mock = function () {};
var plugin_info_provider_revert_mock;

beforeEach(function () {
cfg_parser_mock.prototype = jasmine.createSpyObj('config parser protytpe mock', ['getPluginIdList', 'removePlugin']);
cfg_parser_mock.prototype = jasmine.createSpyObj('config parser protytpe mock', ['getPluginIdList', 'removePlugin', 'write', 'addPlugin']);
cfg_parser_mock.prototype.getPluginIdList.and.returnValue(fake_plugin_list);
cfg_parser_revert_mock = save.__set__('ConfigParser', cfg_parser_mock);
spyOn(cordova_util, 'projectConfig').and.returnValue(projectRoot + '/config.xml');
spyOn(fs, 'readFileSync').and.returnValue(JSON.stringify(fake_fetch_json));
spyOn(save, 'versionString');
plugin_info_provider_mock.prototype = jasmine.createSpyObj('plugin info provider mock', ['get']);
plugin_info_provider_revert_mock = save.__set__('PluginInfoProvider', plugin_info_provider_mock);
});
afterEach(function () {
cfg_parser_revert_mock();
plugin_info_provider_revert_mock();
});
describe('error conditions', function () {
it('should explode if there was an issue parsing or reading from fetch.json file', function (done) {
Expand All @@ -54,15 +62,131 @@ describe('cordova/plugin/save', function () {
});
});
describe('happy path', function () {
it('should remove all plugins from config.xml and re-add new ones based on those retrieved from fetch.json');
it('should only add top-level plugins to config.xml');
it('should write individual plugin specs to config.xml');
it('should write individual plugin variables to config.xml');
it('check that existing plugins are getting removed', function (done) {
save(projectRoot).then(function () {
expect(cfg_parser_mock.prototype.removePlugin).toHaveBeenCalledWith('VRPlugin');
expect(cfg_parser_mock.prototype.removePlugin).toHaveBeenCalledWith('MastodonSocialPlugin');
}).fail(function (e) {
expect(e).toBeUndefined();
fail('did not expect fail handler to be invoked');
}).done(done);
});

it('plugins are being removed first and then only top level plugins are being restored', function (done) {
var fake_fetch_json =
{'VRPlugin': {'source': {
'type': 'registry',
'id': 'id'
},
'is_top_level': true
},
'MastodonSocialPlugin': { 'source': {
'type': 'registry',
'id': 'id'
},
'is_top_level': false }};

fs.readFileSync.and.returnValue(JSON.stringify(fake_fetch_json));
save(projectRoot).then(function () {
expect(cfg_parser_mock.prototype.removePlugin).toHaveBeenCalledWith('VRPlugin');
expect(cfg_parser_mock.prototype.removePlugin).toHaveBeenCalledWith('MastodonSocialPlugin');
expect(cfg_parser_mock.prototype.addPlugin).toHaveBeenCalledWith(Object({ name: 'VRPlugin' }), [ ]);
expect(cfg_parser_mock.prototype.addPlugin).not.toHaveBeenCalledWith(Object({ name: 'MastodonSocialPlugin' }), [ ]);
expect(cfg_parser_mock.prototype.write).toHaveBeenCalled();
}).fail(function (e) {
expect(e).toBeUndefined();
fail('did not expect fail handler to be invoked');
}).done(done);
});

it('should write individual plugin specs to config.xml', function (done) {
var fake_fetch_json =
{'VRPlugin': {'source': {
'type': 'registry',
'id': 'id'
},
'is_top_level': true }};
fs.readFileSync.and.returnValue(JSON.stringify(fake_fetch_json));
spyOn(save, 'getSpec').and.returnValue('1.0.0');
save(projectRoot).then(function () {
expect(cfg_parser_mock.prototype.addPlugin).toHaveBeenCalledWith(Object({ name: 'VRPlugin', spec: '1.0.0' }), jasmine.any(Object));
expect(cfg_parser_mock.prototype.write).toHaveBeenCalled();
}).fail(function (e) {
expect(e).toBeUndefined();
fail('did not expect fail handler to be invoked');
}).done(done);
});

it('should write individual plugin variables to config.xml', function (done) {
var fake_fetch_json =
{'VRPlugin': {'source': {
'type': 'registry',
'id': 'id'
},
'is_top_level': true,
'variables': {
'var 1': ' '
}}};
fs.readFileSync.and.returnValue(JSON.stringify(fake_fetch_json));
save(projectRoot).then(function () {
expect(cfg_parser_mock.prototype.addPlugin).toHaveBeenCalledWith(jasmine.any(Object), [ Object({ name: 'var 1', value: ' ' }) ]);
expect(cfg_parser_mock.prototype.write).toHaveBeenCalled();
}).fail(function (e) {
expect(e).toBeUndefined();
fail('did not expect fail handler to be invoked');
}).done(done);
});
});
describe('getSpec helper method', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with all of these getSpec tests, you want to test getSpec directly. Right now, you are doing save(projectRoot) but instead you want to do save.getSpec({ url: 'https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git' }, '/some/path', 'VRPlugin')

it('should return a plugin source\'s url or path property immediately');
it('should return a version if a version was provided to plugin id');
it('should return a version that includes scope if scope was part of plugin id');
it('should fall back to using PluginInfoProvider to retrieve a version as last resort');
it('should return a plugin source\'s url or path property immediately', function () {
spyOn(save, 'getSpec').and.callThrough();
save.getSpec({ url: 'https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git' }, '/some/path', 'VRPlugin');
expect(save.getSpec).toHaveBeenCalledWith(Object({ url: 'https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git' }), '/some/path', 'VRPlugin');
expect(save.getSpec({ url: 'https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git' }, '/some/path', 'VRPlugin')).toEqual('https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git');
});

it('getSpec should return a version if a version was provided to plugin id', function () {
save.versionString.and.callThrough();
expect(save.getSpec({id: 'cordova-plugin-camera@^1.1.0'}, '/some/path', 'cordova-plugin-camera')).toEqual('^1.1.0');
});

it('should return a version that includes scope if scope was part of plugin id', function () {
save.versionString.and.callThrough();
expect(save.getSpec({ type: 'registry', id: '@scoped/package@^1.0.0' }, '/some/path', 'cordova-plugin-camera')).toEqual('@scoped/package@^1.0.0');
});

it('should fall back to using PluginInfoProvider to retrieve a version as last resort', function () {
expect(save.getSpec({ id: 'cordova-plugin-camera' }, '/some/path', 'cordova-plugin-camera')).toEqual(null);
expect(plugin_info_provider_mock.prototype.get).toHaveBeenCalled();
});
});

describe('getPluginVariables helper method', function () {
it('if no variables are passed in, should return empty', function () {
expect(save.getPluginVariables()).toEqual([]);
});
it('if variables are passed in, should return result & get added to name and value', function () {
expect(save.getPluginVariables({ variable: 'var 1' })).toEqual([ { name: 'variable', value: 'var 1' } ]);
});
});

describe('versionString helper method', function () {
it('if no version, should return null', function () {
save.versionString.and.callThrough();
spyOn(semver, 'valid').and.returnValue(null);
spyOn(semver, 'validRange').and.returnValue(null);
expect(save.versionString()).toBe(null);
});
it('return version passed in, if it is within the valid range', function () {
save.versionString.and.callThrough();
spyOn(semver, 'valid').and.returnValue(null);
spyOn(semver, 'validRange').and.returnValue('^1.3.2');
expect(save.versionString('^1.3.2')).toBe('^1.3.2');
});
it('should check and return a valid version', function () {
save.versionString.and.callThrough();
spyOn(semver, 'valid').and.returnValue('1.3.2');
expect(save.versionString('1.3.2')).toBe('~1.3.2');
});
});
});