Skip to content
Permalink
Browse files
Jasmine 3: Use Spy#calls to access call data
Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>
  • Loading branch information
audreyso and raphinesse committed Sep 28, 2018
1 parent 5e341be commit 7453d126fbcf7d25fab76a9720f8b87dcb2126e9
Showing 3 changed files with 14 additions and 17 deletions.
@@ -31,8 +31,8 @@ describe('android exec.processMessages', function () {
};

beforeEach(function () {
nativeApi.exec.reset();
nativeApi.retrieveJsMessages.reset();
nativeApi.exec.calls.reset();
nativeApi.retrieveJsMessages.calls.reset();
// Avoid a log message warning about the lack of _nativeApi.
exec.setJsToNativeBridgeMode(exec.jsToNativeModes.PROMPT);
nativeApiProvider.set(nativeApi);
@@ -119,7 +119,7 @@ describe('android exec.processMessages', function () {
var callbackSpy = jasmine.createSpy('callbackFromNative');

beforeEach(function () {
callbackSpy.reset();
callbackSpy.calls.reset();
cordova.callbackFromNative = callbackSpy;
});

@@ -216,7 +216,7 @@ describe('android exec.processMessages', function () {
var message2 = createCallbackMessage(true, true, 1, 'id', 'f');
nativeApi.retrieveJsMessages.and.callFake(function () {
expect(callbackSpy).toHaveBeenCalledWith('id', false, 3, ['foo'], false);
callbackSpy.reset();
callbackSpy.calls.reset();
return message2;
});
performExecAndReturn(message1 + '*');
@@ -242,10 +242,10 @@ describe('android exec.processMessages', function () {
}, 200);

runs(function () {
expect(callbackSpy.argsForCall.length).toEqual(3);
expect(callbackSpy.argsForCall[0]).toEqual(['id', false, 3, ['call1'], false]);
expect(callbackSpy.argsForCall[1]).toEqual(['id', false, 3, ['call2'], false]);
expect(callbackSpy.argsForCall[2]).toEqual(['id', false, 3, ['call3'], false]);
expect(callbackSpy).toHaveBeenCalledTimes(3);
expect(callbackSpy.calls.argsFor(0)).toEqual(['id', false, 3, ['call1'], false]);
expect(callbackSpy.calls.argsFor(1)).toEqual(['id', false, 3, ['call2'], false]);
expect(callbackSpy.calls.argsFor(2)).toEqual(['id', false, 3, ['call3'], false]);
});
});
});
@@ -26,8 +26,8 @@ describe('iOS exec', function () {
var origUserAgent = navigator.userAgent;

beforeEach(function () {
winSpy.reset();
failSpy.reset();
winSpy.calls.reset();
failSpy.calls.reset();
});

afterEach(function () {
@@ -24,11 +24,8 @@ describe('channel', function () {
var multiChannel;
var stickyChannel;

function callCount (spy) {
return spy.argsForCall.length;
}
function expectCallCount (spy, count) {
expect(callCount(spy)).toEqual(count);
expect(spy).toHaveBeenCalledTimes(count);
}
beforeEach(function () {
multiChannel = channel.create('multiChannel');
@@ -187,7 +184,7 @@ describe('channel', function () {

testChannel.subscribe(handler);
testChannel.fire(1, 2, 3);
expect(handler.argsForCall[0]).toEqual({0: 1, 1: 2, 2: 3});
expect(handler.calls.argsFor(0)).toEqual([ 1, 2, 3 ]);
});
it('Test#013 : should not fire a handler that was unsubscribed', function () {
var testChannel = multi ? multiChannel : stickyChannel;
@@ -250,7 +247,7 @@ describe('channel', function () {

expectCallCount(before, 1);
expectCallCount(after, 1);
expect(after.argsForCall[0]).toEqual({0: 1, 1: 2, 2: 3});
expect(after.calls.argsFor(0)).toEqual([ 1, 2, 3 ]);
});
it('Test#018 : should instantly trigger the callback if the event is currently being fired.', function () {
var handler1 = jasmine.createSpy().and.callFake(function () { stickyChannel.subscribe(handler2); });
@@ -338,7 +335,7 @@ describe('channel', function () {
describe('onHasSubscribersChange', function () {
it('Test#027 : should be called only when the first subscriber is added and last subscriber is removed.', function () {
var handler = jasmine.createSpy().and.callFake(function () {
if (callCount(handler) === 1) {
if (handler.calls.count() === 1) {
expect(this.numHandlers).toEqual(1);
} else {
expect(this.numHandlers).toEqual(0);

0 comments on commit 7453d12

Please sign in to comment.