Skip to content

Commit

Permalink
test: make use of expectAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
raphinesse committed Jan 12, 2020
1 parent 214dca1 commit 5572140
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 304 deletions.
8 changes: 3 additions & 5 deletions integration-tests/HooksRunner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,9 @@ describe('HooksRunner', function () {
`;
addHooksToConfig(FAIL_HOOK);

return hooksRunner.fire('fail').then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
});
return expectAsync(
hooksRunner.fire('fail')
).toBeRejectedWithError();
});

it('Test 024 : should not error if the hook is unrecognized', function () {
Expand Down
10 changes: 3 additions & 7 deletions integration-tests/plugman_fetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,9 @@ describe('fetch', function () {
});
it('Test 028 : should fail when locally-available plugin is missing pacakge.json', function () {
test_plugin = path.join(plugins_dir, 'org.test.androidonly');
return fetch(test_plugin, temp)
.then(function () {
fail();
}, function (err) {
expect(err).toBeDefined();
expect(err.message).toContain('needs a valid package.json');
});
return expectAsync(
fetch(test_plugin, temp)
).toBeRejectedWithError(/needs a valid package\.json/);
});
});

Expand Down
51 changes: 21 additions & 30 deletions integration-tests/plugman_uninstall.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,17 @@ describe('plugman/uninstall', () => {

describe('failure ', function () {
it('Test 004 : should throw if platform is unrecognized', function () {
return uninstall.uninstallPlatform('atari', project, 'SomePlugin')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Platform "atari" not supported.');
});
return expectAsync(
uninstall.uninstallPlatform('atari', project, 'SomePlugin')
).toBeRejectedWithError('Platform "atari" not supported.');
});

it('Test 005 : should throw if plugin is missing', function () {
return uninstall.uninstallPlatform('android', project, 'SomePluginThatDoesntExist')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?');
});
return expectAsync(
uninstall.uninstallPlatform('android', project, 'SomePluginThatDoesntExist')
).toBeRejectedWithError(
'Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?'
);
});
});
});
Expand All @@ -192,12 +188,11 @@ describe('plugman/uninstall', () => {
it('Test 007 : should fail if plugin is a required dependency', function () {
setupProject('uninstall.test');

return uninstall.uninstallPlugin('C', plugins_install_dir)
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toEqual('Plugin "C" is required by (A) and cannot be removed (hint: use -f or --force)');
});
return expectAsync(
uninstall.uninstallPlugin('C', plugins_install_dir)
).toBeRejectedWithError(
'Plugin "C" is required by (A) and cannot be removed (hint: use -f or --force)'
);
});

it('Test 008 : allow forcefully removing a plugin', function () {
Expand Down Expand Up @@ -245,21 +240,17 @@ describe('plugman/uninstall', () => {

describe('failure', function () {
it('Test 011 : should throw if platform is unrecognized', function () {
return uninstall('atari', project, 'SomePlugin')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Platform "atari" not supported.');
});
return expectAsync(
uninstall('atari', project, 'SomePlugin')
).toBeRejectedWithError('Platform "atari" not supported.');
});

it('Test 012 : should throw if plugin is missing', function () {
return uninstall('android', project, 'SomePluginThatDoesntExist')
.then(function (result) {
fail('Expected promise to be rejected');
}, function err (errMsg) {
expect(errMsg.toString()).toContain('Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?');
});
return expectAsync(
uninstall('android', project, 'SomePluginThatDoesntExist')
).toBeRejectedWithError(
'Plugin "SomePluginThatDoesntExist" not found. Already uninstalled?'
);
});
});
});
Expand Down
42 changes: 16 additions & 26 deletions spec/cordova/build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,21 @@ describe('build command', function () {
describe('failure', function () {
it('Test 001 : should not run inside a project with no platforms', function () {
util.listPlatforms.and.returnValue([]);
return cordovaBuild()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toEqual(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
return expectAsync(
cordovaBuild()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});

it('Test 002 : should not run outside of a Cordova-based project', function () {
util.isCordova.and.returnValue(false);

return cordovaBuild()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toEqual(
'Current working directory is not a Cordova-based project.'
);
});
return expectAsync(
cordovaBuild()
).toBeRejectedWithError(
'Current working directory is not a Cordova-based project.'
);
});
});

Expand Down Expand Up @@ -101,14 +93,12 @@ describe('build command', function () {
describe('with no platforms added', function () {
it('Test 008 : should not fire the hooker', function () {
util.listPlatforms.and.returnValue([]);
return Promise.resolve().then(cordovaBuild).then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toEqual(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});

return expectAsync(
cordovaBuild()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
});
});
Expand Down
41 changes: 17 additions & 24 deletions spec/cordova/compile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,17 @@ describe('compile command', function () {
describe('failure', function () {
it('Test 001 : should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function () {
list_platforms.and.returnValue([]);
return cordova.compile()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toContain('No platforms added to this project. Please use `cordova platform add <platform>`.');
});
return expectAsync(
cordova.compile()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
it('Test 002 : should not run outside of a Cordova-based project', function () {
is_cordova.and.returnValue(false);
return cordova.compile()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
});
return expectAsync(
cordova.compile()
).toBeRejectedWithError();
});
});

Expand Down Expand Up @@ -97,18 +92,16 @@ describe('compile command', function () {
});

describe('with no platforms added', function () {
it('Test 008 : should not fire the hooker', function () {
it('Test 008 : should not fire the hooker', async function () {
list_platforms.and.returnValue([]);
return Promise.resolve().then(cordova.compile)
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(fire).not.toHaveBeenCalled();
expect(err.message).toContain(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});

await expectAsync(
cordova.compile()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);

expect(fire).not.toHaveBeenCalled();
});
});
});
Expand Down
38 changes: 17 additions & 21 deletions spec/cordova/emulate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,17 @@ describe('emulate command', function () {
describe('failure', function () {
it('Test 001 : should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function () {
util.listPlatforms.and.returnValue([]);
return cordovaEmulate()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toContain('No platforms added to this project. Please use `cordova platform add <platform>`.');
});
return expectAsync(
cordovaEmulate()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);
});
it('Test 002 : should not run outside of a Cordova-based project', function () {
util.isCordova.and.returnValue(false);
return cordovaEmulate()
.then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
});
return expectAsync(
cordovaEmulate()
).toBeRejectedWithError();
});
});

Expand Down Expand Up @@ -144,15 +139,16 @@ describe('emulate command', function () {
});

describe('with no platforms added', function () {
it('Test 011 : should not fire the hooker', function () {
it('Test 011 : should not fire the hooker', async function () {
util.listPlatforms.and.returnValue([]);
return Promise.resolve().then(cordovaEmulate).then(function () {
fail('Expected promise to be rejected');
}, function (err) {
expect(err).toEqual(jasmine.any(Error));
expect(err.message).toContain('No platforms added to this project. Please use `cordova platform add <platform>`.');
expect(HooksRunner.prototype.fire).not.toHaveBeenCalled();
});

await expectAsync(
cordovaEmulate()
).toBeRejectedWithError(
'No platforms added to this project. Please use `cordova platform add <platform>`.'
);

expect(HooksRunner.prototype.fire).not.toHaveBeenCalled();
});
});
});
Expand Down
28 changes: 13 additions & 15 deletions spec/cordova/platform/addHelper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,17 @@ describe('cordova/platform/addHelper', function () {

it('should throw if platform was already added before adding', function () {
fs.existsSync.and.returnValue('/some/path/platforms/ios');
return platform_addHelper('add', hooks_mock, projectRoot, ['ios']).then(function () {
fail('addHelper success handler unexpectedly invoked');
}, function (e) {
expect(e.message).toContain('already added.');
});
return expectAsync(
platform_addHelper('add', hooks_mock, projectRoot, ['ios'])
).toBeRejectedWithError(/already added\./);
});

it('should throw if platform was not added before updating', function () {
return platform_addHelper('update', hooks_mock, projectRoot, ['atari']).then(function () {
fail('addHelper success handler unexpectedly invoked');
}, function (e) {
expect(e.message).toContain('Platform "atari" is not yet added. See `cordova platform list`.');
});
return expectAsync(
platform_addHelper('update', hooks_mock, projectRoot, ['atari'])
).toBeRejectedWithError(
'Platform "atari" is not yet added. See `cordova platform list`.'
);
});
});
describe('happy path (success conditions)', function () {
Expand Down Expand Up @@ -272,11 +270,11 @@ describe('cordova/platform/addHelper', function () {
describe('errors', function () {
it('should reject the promise should fetch fail', function () {
fetch_mock.and.returnValue(Promise.reject(new Error('fetch has failed, rejecting promise')));
return platform_addHelper.downloadPlatform(projectRoot, 'android', '67').then(function () {
fail('success handler unexpectedly invoked');
}, function (e) {
expect(e.message).toContain('fetch has failed, rejecting promise');
});
return expectAsync(
platform_addHelper.downloadPlatform(projectRoot, 'android', '67')
).toBeRejectedWithError(
/fetch has failed, rejecting promise/
);
});
});
describe('happy path', function () {
Expand Down
22 changes: 10 additions & 12 deletions spec/cordova/platform/getPlatformDetailsFromDir.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,20 @@ describe('cordova/platform/getPlatformDetailsFromDir', function () {
});

it('should throw if no config.xml or pkgJson', function () {
return platform_getPlatformDetails('dir', ['ios'])
.then(function () {
fail('Expected promise to be rejected');
}, function (reason) {
expect(reason).toMatch(/does not seem to contain a valid package.json or a valid Cordova platform/);
});
return expectAsync(
platform_getPlatformDetails('dir', ['ios'])
).toBeRejectedWithError(
/does not seem to contain a valid package.json or a valid Cordova platform/
);
});

it('should throw if no platform is provided', function () {
cordova_util.requireNoCache.and.returnValue({});
return platform_getPlatformDetails('dir')
.then(function () {
fail('Expected promise to be rejected');
}, function (reason) {
expect(reason).toMatch(/does not seem to contain a Cordova platform:/);
});
return expectAsync(
platform_getPlatformDetails('dir')
).toBeRejectedWithError(
/does not seem to contain a Cordova platform:/
);
});

it('should return a promise with platform and version', function () {
Expand Down
Loading

0 comments on commit 5572140

Please sign in to comment.