Skip to content
Permalink
Browse files
Promise handling cleanup in specs (#79)
* Proper expected rejection check in ActionStack spec

* Clean up Promise handling in PluginManager spec

* Remove usage of jasmine's done in sync specs

* Clean up Promise handling in superspawn spec
  • Loading branch information
raphinesse committed Jun 18, 2019
1 parent 37f480c commit fe684d2a0fa7eddd986add87e4a90f8c22010d69
Showing 4 changed files with 28 additions and 48 deletions.
@@ -41,7 +41,7 @@ describe('action-stack', function () {
expect(second_spy).toHaveBeenCalledWith(second_args[0]);
expect(third_spy).toHaveBeenCalledWith(third_args[0]);
});
it('Test 002 : should revert processed actions if an exception occurs', function (done) {
it('Test 002 : should revert processed actions if an exception occurs', function () {
spyOn(console, 'log');
var first_spy = jasmine.createSpy();
var first_args = [1];
@@ -57,21 +57,20 @@ describe('action-stack', function () {
stack.push(stack.createAction(first_spy, first_args, first_reverter, first_reverter_args));
stack.push(stack.createAction(second_spy, second_args, function () {}, []));
stack.push(stack.createAction(third_spy, third_args, function () {}, []));

// process should throw
var error;
stack.process('android', android_one_project)
.then(function () {
expect(false).toBe(true);
}).fail(function (err) {
error = err;
return stack.process('android', android_one_project)
.then(() => {
fail('Expected promise to be rejected');
}, error => {
expect(error).toEqual(process_err);
// first two actions should have been called, but not the third
expect(first_spy).toHaveBeenCalledWith(first_args[0]);
expect(second_spy).toHaveBeenCalledWith(second_args[0]);
expect(third_spy).not.toHaveBeenCalledWith(third_args[0]);
// first reverter should have been called after second action exploded
expect(first_reverter).toHaveBeenCalledWith(first_reverter_args[0]);
}).fin(done);
});
});
});
});
@@ -58,7 +58,6 @@ describe('PluginManager class', function () {
describe('instance', function () {
var actions, manager;
var FAKE_PROJECT;
var fail = jasmine.createSpy('fail');
var ActionStackOrig = PluginManager.__get__('ActionStack');

beforeEach(function () {
@@ -84,52 +83,37 @@ describe('PluginManager class', function () {
expect(manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), {})).not.toHaveBeenRejected(done);
});

it('Test 004 : should iterate through all plugin\'s files and frameworks', function (done) {
manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), {})
it('Test 004 : should iterate through all plugin\'s files and frameworks', () => {
return manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), {})
.then(function () {
expect(FAKE_PROJECT.getInstaller.calls.count()).toBe(16);
expect(FAKE_PROJECT.getUninstaller.calls.count()).toBe(16);

expect(actions.push.calls.count()).toBe(16);
expect(actions.process).toHaveBeenCalled();
expect(FAKE_PROJECT.write).toHaveBeenCalled();
})
.fail(fail)
.done(function () {
expect(fail).not.toHaveBeenCalled();
done();
});
});

it('Test 005 : should save plugin metadata to www directory', function (done) {
it('Test 005 : should save plugin metadata to www directory', () => {
var metadataPath = path.join(manager.locations.www, 'cordova_plugins.js');
var platformWwwMetadataPath = path.join(manager.locations.platformWww, 'cordova_plugins.js');

manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), {})
return manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), {})
.then(function () {
expect(fs.writeFileSync).toHaveBeenCalledWith(metadataPath, jasmine.any(String), 'utf-8');
expect(fs.writeFileSync).not.toHaveBeenCalledWith(platformWwwMetadataPath, jasmine.any(String), 'utf-8');
})
.fail(fail)
.done(function () {
expect(fail).not.toHaveBeenCalled();
done();
});
});

it('Test 006 : should save plugin metadata to both www ans platform_www directories when options.usePlatformWww is specified', function (done) {
it('Test 006 : should save plugin metadata to both www ans platform_www directories when options.usePlatformWww is specified', () => {
var metadataPath = path.join(manager.locations.www, 'cordova_plugins.js');
var platformWwwMetadataPath = path.join(manager.locations.platformWww, 'cordova_plugins.js');

manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), { usePlatformWww: true })
return manager.addPlugin(new PluginInfo(DUMMY_PLUGIN), { usePlatformWww: true })
.then(function () {
expect(fs.writeFileSync).toHaveBeenCalledWith(metadataPath, jasmine.any(String), 'utf-8');
expect(fs.writeFileSync).toHaveBeenCalledWith(platformWwwMetadataPath, jasmine.any(String), 'utf-8');
})
.fail(fail)
.done(function () {
expect(fail).not.toHaveBeenCalled();
done();
});
});
});
@@ -43,7 +43,7 @@ describe('prunePLIST', function () {

var selector = 'FirstConfigKey';

it('Test 01: should remove property from plist file using provided selector', function (done) {
it('Test 01: should remove property from plist file using provided selector', () => {
var pruneStatus = plistHelpers.prunePLIST(doc, xml, selector);

expect(pruneStatus).toBeTruthy();
@@ -54,8 +54,6 @@ describe('prunePLIST', function () {
}
}
);

done();
});
});

@@ -74,7 +72,7 @@ describe('plistGraft', function () {

let selector = 'keychain-access-groups';

it('Test 01: should not mangle existing plist entries', function (done) {
it('Test 01: should not mangle existing plist entries', () => {
var graftStatus = plistHelpers.graftPLIST(doc, xml, selector);

expect(graftStatus).toBeTruthy();
@@ -86,7 +84,5 @@ describe('plistGraft', function () {
]
}
);

done();
});
});
@@ -36,25 +36,25 @@ describe('spawn method', function () {
expect(Q.isPromise(superspawn.spawn('invalid_command'))).toBe(true);
});

it('Test 002 : should notify about stdout "data" events', function (done) {
superspawn.spawn(LS, [], { stdio: 'pipe' })
it('Test 002 : should notify about stdout "data" events', () => {
return superspawn.spawn(LS, [], { stdio: 'pipe' })
.progress(progressSpy)
.fin(function () {
.then(function () {
expect(progressSpy).toHaveBeenCalledWith({ 'stdout': jasmine.any(String) });
done();
});
});

it('Test 003 : should notify about stderr "data" events', function (done) {
superspawn.spawn(LS, ['doesnt-exist'], { stdio: 'pipe' })
it('Test 003 : should notify about stderr "data" events', () => {
return superspawn.spawn(LS, ['doesnt-exist'], { stdio: 'pipe' })
.progress(progressSpy)
.fin(function () {
.then(() => {
fail('Expected promise to be rejected');
}, () => {
expect(progressSpy).toHaveBeenCalledWith({ 'stderr': jasmine.any(String) });
done();
});
});

it('Test 004 : reject handler should pass in Error object with stdout and stderr properties', function (done) {
it('Test 004 : reject handler should pass in Error object with stdout and stderr properties', () => {
var cp = require('child_process');
spyOn(cp, 'spawn').and.callFake(function (cmd, args, opts) {
return {
@@ -80,12 +80,13 @@ describe('spawn method', function () {
removeListener: function () {}
};
});
superspawn.spawn('this aggression', ['will', 'not', 'stand', 'man'], {})
.catch(function (err) {
return superspawn.spawn('this aggression', ['will', 'not', 'stand', 'man'], {})
.then(() => {
fail('Expected promise to be rejected');
}, err => {
expect(err).toBeDefined();
expect(err.stdout).toContain('usual');
expect(err.stderr).toContain('mayday');
done();
});
});

0 comments on commit fe684d2

Please sign in to comment.