Skip to content

Commit

Permalink
Changes from code review and more
Browse files Browse the repository at this point in the history
  • Loading branch information
raphinesse authored and Menardi committed Jun 18, 2018
1 parent 22e1ecc commit 98de909
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 64 deletions.
4 changes: 3 additions & 1 deletion spec/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
env:
jasmine: true
jasmine: true
rules:
prefer-promise-reject-errors: off
41 changes: 14 additions & 27 deletions spec/unit/retry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,35 @@
*/

const retry = require('../../bin/templates/cordova/lib/retry');
const Q = require('q');

describe('retry', () => {
describe('retryPromise method', () => {
let deferred;
let promiseFn;

beforeEach(() => {
deferred = Q.defer();
promiseFn = jasmine.createSpy().and.returnValue(deferred.promise);
promiseFn = jasmine.createSpy().and.returnValue(Promise.resolve());
});

it('should pass all extra arguments the the promise', () => {
retry.retryPromise(0, promiseFn, 'test1', 'test2', 'test3');
expect(promiseFn).toHaveBeenCalledWith('test1', 'test2', 'test3');
it('should pass all extra arguments to the promise', () => {
const args = ['test1', 'test2', 'test3'];

retry.retryPromise(0, promiseFn, ...args);
expect(promiseFn).toHaveBeenCalledWith(...args);
});

it('should retry the function up to specified number of times', done => {
it('should retry the function up to specified number of times', () => {
const attempts = 3;
promiseFn.and.returnValue(Promise.reject());

retry.retryPromise(attempts, promiseFn)
.then(() => {
done.fail('Unexpectedly resolved');
})
.fail(() => {
expect(promiseFn).toHaveBeenCalledTimes(attempts);
done();
});

for (let i = 0; i < attempts + 1; i++) {
deferred.reject();
}
return retry.retryPromise(attempts, promiseFn).then(
() => fail('Unexpectedly resolved'),
() => expect(promiseFn).toHaveBeenCalledTimes(attempts)
);
});

it('should not call the function again if it succeeds', () => {
retry.retryPromise(1, promiseFn);
promiseFn.calls.reset();

deferred.resolve();

return deferred.promise.then(() => {
expect(promiseFn).not.toHaveBeenCalled();
return retry.retryPromise(42, promiseFn).then(() => {
expect(promiseFn).toHaveBeenCalledTimes(1);
});
});
});
Expand Down
64 changes: 28 additions & 36 deletions spec/unit/run.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

const rewire = require('rewire');
const Q = require('q');

describe('run', () => {
let run;
Expand Down Expand Up @@ -56,7 +55,7 @@ describe('run', () => {
eventsSpyObj = jasmine.createSpyObj('eventsSpy', ['emit']);
getInstallTargetSpy = jasmine.createSpy('getInstallTargetSpy');

buildSpyObj.run.and.returnValue(Q.resolve());
buildSpyObj.run.and.returnValue(Promise.resolve());

run.__set__({
build: buildSpyObj,
Expand All @@ -71,7 +70,7 @@ describe('run', () => {
const deviceList = ['testDevice1', 'testDevice2'];

getInstallTargetSpy.and.returnValue(null);
deviceSpyObj.list.and.returnValue(Q.resolve(deviceList));
deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));

return run.run().then(() => {
expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(deviceList[0]);
Expand All @@ -82,8 +81,8 @@ describe('run', () => {
const deviceList = [];

getInstallTargetSpy.and.returnValue(null);
deviceSpyObj.list.and.returnValue(Q.resolve(deviceList));
emulatorSpyObj.list_started.and.returnValue(Q.resolve([]));
deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));

return run.run().then(() => {
expect(emulatorSpyObj.list_started).toHaveBeenCalled();
Expand All @@ -103,7 +102,7 @@ describe('run', () => {
const emulatorList = ['emulator1', 'emulator2'];

getInstallTargetSpy.and.returnValue('--emulator');
emulatorSpyObj.list_started.and.returnValue(Q.resolve(emulatorList));
emulatorSpyObj.list_started.and.returnValue(Promise.resolve(emulatorList));

return run.run().then(() => {
expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(emulatorList[0]);
Expand All @@ -115,11 +114,8 @@ describe('run', () => {
const defaultEmulator = 'default-emu';

getInstallTargetSpy.and.returnValue('--emulator');
emulatorSpyObj.list_started.and.returnValue(Q.resolve(emulatorList));

const startDeferred = Q.defer();
startDeferred.resolve(defaultEmulator);
emulatorSpyObj.start.and.returnValue(startDeferred.promise);
emulatorSpyObj.list_started.and.returnValue(Promise.resolve(emulatorList));
emulatorSpyObj.start.and.returnValue(Promise.resolve(defaultEmulator));

return run.run().then(() => {
expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(defaultEmulator);
Expand All @@ -130,7 +126,7 @@ describe('run', () => {
const deviceList = ['device1', 'device2', 'device3'];
getInstallTargetSpy.and.returnValue(deviceList[1]);

deviceSpyObj.list.and.returnValue(Q.resolve(deviceList));
deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));

return run.run().then(() => {
expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(deviceList[1]);
Expand All @@ -141,8 +137,8 @@ describe('run', () => {
const startedEmulatorList = ['emu1', 'emu2', 'emu3'];
getInstallTargetSpy.and.returnValue(startedEmulatorList[2]);

deviceSpyObj.list.and.returnValue(Q.resolve([]));
emulatorSpyObj.list_started.and.returnValue(Q.resolve(startedEmulatorList));
deviceSpyObj.list.and.returnValue(Promise.resolve([]));
emulatorSpyObj.list_started.and.returnValue(Promise.resolve(startedEmulatorList));

return run.run().then(() => {
expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(startedEmulatorList[2]);
Expand All @@ -157,34 +153,31 @@ describe('run', () => {
];
getInstallTargetSpy.and.returnValue(emulatorList[2].name);

deviceSpyObj.list.and.returnValue(Q.resolve([]));
emulatorSpyObj.list_started.and.returnValue(Q.resolve([]));
emulatorSpyObj.list_images.and.returnValue(Q.resolve(emulatorList));
emulatorSpyObj.start.and.returnValue(Q.resolve(emulatorList[2].id));
deviceSpyObj.list.and.returnValue(Promise.resolve([]));
emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
emulatorSpyObj.list_images.and.returnValue(Promise.resolve(emulatorList));
emulatorSpyObj.start.and.returnValue(Promise.resolve(emulatorList[2].id));

return run.run().then(() => {
expect(emulatorSpyObj.start).toHaveBeenCalledWith(emulatorList[2].name);
expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(emulatorList[2].id);
});
});

it('should throw an error if target is specified but does not exist', done => {
it('should throw an error if target is specified but does not exist', () => {
const emulatorList = [{ name: 'emu1', id: 1 }];
const deviceList = ['device1'];
const target = 'nonexistentdevice';
getInstallTargetSpy.and.returnValue(target);

deviceSpyObj.list.and.returnValue(Q.resolve(deviceList));
emulatorSpyObj.list_started.and.returnValue(Q.resolve([]));
emulatorSpyObj.list_images.and.returnValue(Q.resolve(emulatorList));
deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
emulatorSpyObj.list_images.and.returnValue(Promise.resolve(emulatorList));

run.run().then(() => {
done.fail('Expected error to be thrown');
})
.fail(err => {
expect(err).toContain(target);
done();
});
return run.run().then(
() => fail('Expected error to be thrown'),
err => expect(err).toContain(target)
);
});

it('should build the app if a target has been found', () => {
Expand All @@ -209,9 +202,9 @@ describe('run', () => {
it('should install on emulator after build', () => {
const emulatorTarget = { target: 'emu1', isEmulator: true };
getInstallTargetSpy.and.returnValue('--emulator');
emulatorSpyObj.list_started.and.returnValue(Q.resolve([emulatorTarget.target]));
emulatorSpyObj.list_started.and.returnValue(Promise.resolve([emulatorTarget.target]));
emulatorSpyObj.resolveTarget.and.returnValue(emulatorTarget);
emulatorSpyObj.wait_for_boot.and.returnValue(Q.resolve());
emulatorSpyObj.wait_for_boot.and.returnValue(Promise.resolve());

return run.run().then(() => {
expect(emulatorSpyObj.install).toHaveBeenCalledWith(emulatorTarget, undefined);
Expand All @@ -221,13 +214,12 @@ describe('run', () => {

describe('help', () => {
it('should print out usage and help', () => {
let message = '';
spyOn(console, 'log').and.callFake(msg => { message += msg; });
spyOn(process, 'exit');
const logSpy = jasmine.createSpy();
const procStub = { exit: _ => null, cwd: _ => '', argv: ['', ''] };
run.__set__({ console: { log: logSpy }, process: procStub });

run.help();

expect(message.indexOf('Usage:') > -1);
expect(logSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/));
});
});
});

0 comments on commit 98de909

Please sign in to comment.