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

Fix, simplify and improve tests #702

Merged
merged 8 commits into from
Oct 29, 2019
85 changes: 8 additions & 77 deletions tests/spec/unit/Api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

var path = require('path');
var fs = require('fs');
const EventEmitter = require('events');
var PluginManager = require('cordova-common').PluginManager;
var events = require('cordova-common').events;
var Api = require('../../../bin/templates/scripts/cordova/Api');
var check_reqs = require('../../../bin/templates/scripts/cordova/lib/check_reqs');

Expand Down Expand Up @@ -49,97 +49,29 @@ function compareListWithoutOrder (list1, list2) {
describe('Platform Api', function () {

describe('constructor', function () {
beforeEach(function () {
events.removeAllListeners();
});

it('Test 001 : should throw if provided directory does not contain an xcodeproj file', function () {
expect(function () { new Api('ios', path.join(FIXTURES, '..')); }).toThrow(); /* eslint no-new : 0 */
expect(() =>
new Api('ios', path.join(FIXTURES, '..'), new EventEmitter())
).toThrow();
});
it('Test 002 : should create an instance with path, pbxproj, xcodeproj, originalName and cordovaproj properties', function () {
expect(function () {
var p = new Api('ios', iosProjectFixture);
var p = new Api('ios', iosProjectFixture, new EventEmitter());
expect(p.locations.root).toEqual(iosProjectFixture);
expect(p.locations.pbxproj).toEqual(path.join(iosProjectFixture, 'SampleApp.xcodeproj', 'project.pbxproj'));
expect(p.locations.xcodeProjDir).toEqual(path.join(iosProjectFixture, 'SampleApp.xcodeproj'));
expect(p.locations.www).toEqual(path.join(iosProjectFixture, 'www'));
expect(p.locations.configXml).toEqual(path.join(iosProjectFixture, 'SampleApp', 'config.xml'));
}).not.toThrow();
});
it('Test 003 : test cocoapods check_reqs, on darwin (macOS)', function (done) {
// the purpose of this test is not to actually test whether CocoaPods is installed
// it is to test check_reqs can run and be covered (we mock the actual checking, simple check)

check_reqs.check_os()
.then(function (message) {
// supported os
function fail () {
done.fail('check_reqs fail (' + message + ')');
}
function success () {
done();
}
var toolsChecker = {
success: function () {
return Q.resolve('CocoaPods found');
},
fail: function () {
return Q.reject('CocoaPods NOT found');
}
};

// success expected
check_reqs.check_cocoapods(toolsChecker.success)
.then(success, fail)
.catch(fail);

// fail expected
check_reqs.check_cocoapods(toolsChecker.fail)
.then(fail, success)
.catch(success);

}, function () {
// unsupported os, do nothing
done();
});
});
it('Test 004 : test cocoapods check_reqs, expected success on non-darwin (macOS)', function (done) {
// the purpose of this test is not to actually test whether CocoaPods is installed
// it is to test check_reqs can run and be covered (we mock the actual checking, simple check)
check_reqs.check_os()
.then(function () {
// supported os, do nothing
done();
}, function (message) {
// unsupported os, check_reqs should be ignored
function fail () {
done.fail('check_reqs fail (' + message + ')');
}
function success (toolOptions) {
expect(toolOptions.ignore).toBeDefined();
expect(toolOptions.ignoreMessage).toBeDefined();
done();
}
var toolsChecker = function () {
done.fail(); // this function should not ever be called if non-darwin
return Q.reject('CocoaPods NOT found');
};

// success expected
check_reqs.check_cocoapods(toolsChecker)
.then(success, fail)
.catch(fail);
});

});
});

describe('.prototype', function () {
var api;
var api, events;
var projectRoot = iosProjectFixture;
beforeEach(function () {
events.removeAllListeners();
api = new Api('ios', projectRoot);
events = new EventEmitter();
api = new Api('ios', projectRoot, events);
spyOn(fs, 'readdirSync').and.returnValue([api.locations.xcodeProjDir]);
spyOn(projectFile, 'parse').and.returnValue({
getPackageName: function () { return 'ios.cordova.io'; }
Expand Down Expand Up @@ -447,7 +379,6 @@ describe('Platform Api', function () {
}).done(done);
});
it('if two frameworks with the same name are added, should honour the spec of the first-installed plugin', function (done) {
spyOn(events, 'emit');
podsjson_mock.getLibrary.and.returnValue({
spec: 'something different from ' + my_pod_json.spec
});
Expand Down
3 changes: 2 additions & 1 deletion tests/spec/unit/Plugman/pluginHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var fs = require('fs');
var path = require('path');
var rewire = require('rewire');
var shell = require('shelljs');
const EventEmitter = require('events');

var PluginInfo = require('cordova-common').PluginInfo;
var Api = require('../../../../bin/templates/scripts/cordova/Api');
Expand Down Expand Up @@ -344,7 +345,7 @@ describe('ios plugin handler', function () {
});

it('Test 028 : of two plugins should apply xcode file changes from both', function (done) {
var api = new Api('ios', temp);
var api = new Api('ios', temp, new EventEmitter());
var fail = jasmine.createSpy('fail');

api.addPlugin(dummyPluginInfo)
Expand Down
28 changes: 10 additions & 18 deletions tests/spec/unit/build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,11 @@ describe('build', function () {

describe('help method', () => {
it('should log a bunch of options', () => {
const logSpy = jasmine.createSpy();
const procStub = { exit: _ => null, cwd: _ => '', argv: ['', ''] };
build.__set__({ console: { log: logSpy }, process: procStub });
spyOn(console, 'log');
spyOn(process, 'exit');

build.help();
expect(logSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/));
expect(console.log).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/));
});
});

Expand Down Expand Up @@ -416,25 +415,18 @@ describe('build', function () {
// This method will require a module that supports the run method.
build.__set__('require', () => {
return {
run: () => {
return new Promise((resolve, reject) => {
resolve(mockedEmulators);
});
}
run: () => Promise.resolve(mockedEmulators)
};
});

const getDefaultSimulatorTarget = build.__get__('getDefaultSimulatorTarget');
const exec = getDefaultSimulatorTarget();

const expected = {
name: 'iPhone X',
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-X',
simIdentifier: 'iPhone-X'
};

exec.then((actual) => {
expect(actual).toEqual(expected);
getDefaultSimulatorTarget().then((actual) => {
expect(actual).toEqual({
name: 'iPhone X',
identifier: 'com.apple.CoreSimulator.SimDeviceType.iPhone-X',
simIdentifier: 'iPhone-X'
});
done();
});
});
Expand Down
128 changes: 64 additions & 64 deletions tests/spec/unit/lib/check_reqs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,97 +17,97 @@
under the License.
*/

var rewire = require('rewire');
var checkReqs = rewire('../../../../bin/templates/scripts/cordova/lib/check_reqs');
const rewire = require('rewire');
const shell = require('shelljs');
const versions = require('../../../../bin/templates/scripts/cordova/lib/versions');

describe('check_reqs', function () {
let checkReqs;
beforeEach(() => {
checkReqs = rewire('../../../../bin/templates/scripts/cordova/lib/check_reqs');
});

describe('checkTool method', () => {
const originalVersion = checkReqs.__get__('versions');
let shellWhichSpy;
let rejectSpy;
let resolveSpy;
let getToolVersionSpy;
let checkTool;

beforeEach(() => {
// Shell Spy
shellWhichSpy = jasmine.createSpy('shellWhichSpy');
checkReqs.__set__('shell', {
which: shellWhichSpy
});

// Q Spy
rejectSpy = jasmine.createSpy('rejectSpy');
resolveSpy = jasmine.createSpy('resolveSpy');
checkReqs.__set__('Q', {
reject: rejectSpy,
resolve: resolveSpy
});
checkTool = checkReqs.__get__('checkTool');

// Versions Spy
getToolVersionSpy = jasmine.createSpy('rejectSpy');
spyOn(shell, 'which').and.returnValue('/bin/node');
spyOn(versions, 'get_tool_version').and.returnValue(Promise.resolve('1.0.0'));
});

it('should not have found tool.', () => {
shellWhichSpy.and.returnValue(false);
const checkTool = checkReqs.__get__('checkTool');

checkTool('node', '1.0.0');
shell.which.and.returnValue(false);

expect(rejectSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^node was not found./));
return checkTool('node', '1.0.0').then(
() => fail('Expected promise to be rejected'),
reason => expect(reason).toContain('node was not found.')
);
});

it('should throw error because version is not following semver-notated.', (done) => {
shellWhichSpy.and.returnValue('/bin/node');
const checkTool = checkReqs.__get__('checkTool');
it('should throw error because version is not following semver-notated.', () => {
return checkTool('node', 'a.b.c').then(
() => fail('Expected promise to be rejected'),
err => expect(err).toEqual(new TypeError('Invalid Version: a.b.c'))
);
});

checkReqs.__set__('versions', {
get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => {
return resolve('1.0.0');
}).catch((error) => { console.log(error); })),
compareVersions: originalVersion.compareVersions
it('should resolve passing back tool version.', () => {
return checkTool('node', '1.0.0').then(result => {
expect(result).toEqual({ version: '1.0.0' });
});
});

checkTool('node', 'a.b.c').catch(err => {
expect(err).toEqual(new TypeError('Invalid Version: a.b.c'));
done();
});
it('should reject because tool does not meet minimum requirement.', () => {
return checkTool('node', '1.0.1').then(
() => fail('Expected promise to be rejected'),
reason => expect(reason).toContain('version 1.0.1 or greater, you have version 1.0.0')
);
});
});

it('should resolve passing back tool version.', (done) => {
shellWhichSpy.and.returnValue('/bin/node');
const checkTool = checkReqs.__get__('checkTool');
describe('check_cocoapods method', () => {
let toolsChecker;
beforeEach(() => {
toolsChecker = jasmine.createSpy('toolsChecker')
.and.returnValue(Promise.resolve({}));
});

checkReqs.__set__('versions', {
get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => {
return resolve('1.0.0');
})),
compareVersions: originalVersion.compareVersions
it('should resolve when on an unsupported platform', () => {
checkReqs.__set__({
os_platform_is_supported: () => false
});

checkTool('node', '1.0.0').then(() => {
let actual = resolveSpy.calls.argsFor(0)[0];
expect(actual).toEqual({ version: '1.0.0' });
done();
return checkReqs.check_cocoapods(toolsChecker).then(toolOptions => {
expect(toolsChecker).not.toHaveBeenCalled();
expect(toolOptions.ignore).toBeDefined();
expect(toolOptions.ignoreMessage).toBeDefined();
});
});

it('should reject because tool does not meet minimum requirement.', (done) => {
shellWhichSpy.and.returnValue('/bin/node');
const checkTool = checkReqs.__get__('checkTool');
it('should resolve when toolsChecker resolves', () => {
checkReqs.__set__({
os_platform_is_supported: () => true
});
spyOn(shell, 'exec').and.returnValue({ code: 1 });

checkReqs.__set__('versions', {
get_tool_version: getToolVersionSpy.and.returnValue(new Promise((resolve) => {
return resolve('1.0.0');
})),
compareVersions: originalVersion.compareVersions
return checkReqs.check_cocoapods(toolsChecker).then(() => {
expect(shell.exec).toHaveBeenCalled();
});
});

checkTool('node', '1.0.1').then(() => {
let actual = rejectSpy.calls.argsFor(0)[0];
expect(actual).toContain('version 1.0.1 or greater');
expect(actual).toContain('you have version 1.0.0');
done();
it('should reject when toolsChecker rejects', () => {
checkReqs.__set__({
os_platform_is_supported: () => true
});
const testError = new Error();
toolsChecker.and.callFake(() => Promise.reject(testError));

return checkReqs.check_cocoapods(toolsChecker).then(
() => fail('Expected promise to be rejected'),
err => expect(err).toBe(testError)
);
});
});
});
14 changes: 8 additions & 6 deletions tests/spec/unit/lib/run.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ if (process.platform === 'darwin') {
spyOn(run, 'listEmulators').and.returnValue(deferred.promise);
});
it('should delegate to listDevices method if `options.device` specified', function () {
run.run({ list: true, device: true });
expect(run.listDevices).toHaveBeenCalled();
expect(run.listEmulators).not.toHaveBeenCalled();
return run.run({ list: true, device: true }).then(() => {
expect(run.listDevices).toHaveBeenCalled();
expect(run.listEmulators).not.toHaveBeenCalled();
});
});
it('should delegate to listEmulators method if `options.device` specified', function () {
run.run({ list: true, emulator: true });
expect(run.listDevices).not.toHaveBeenCalled();
expect(run.listEmulators).toHaveBeenCalled();
return run.run({ list: true, emulator: true }).then(() => {
expect(run.listDevices).not.toHaveBeenCalled();
expect(run.listEmulators).toHaveBeenCalled();
});
});
it('should delegate to to both listEmulators and listDevices methods if neither `options.device` nor `options.emulator` are specified', function (done) {
run.run({ list: true })
Expand Down
Loading