Skip to content

Commit

Permalink
Favor using toHaveBeenCalledTimes.
Browse files Browse the repository at this point in the history
This gives better error messages than using toBe() or toEqual() with
the number of calls.

Change-Id: Ice353f1ebcc715da69416da2d983e919597f82d1
  • Loading branch information
TheModMaker committed Jun 26, 2019
1 parent 2d4904c commit e2ba179
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 140 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ module.exports = {
'selector': 'CatchClause',
'message': 'Use expect.toFail or expectAsync.toBeRejected',
},
{
'selector': 'CallExpression[callee.name=expect] >' +
'CallExpression[callee.property.name=count]' +
'[callee.object.property.name=calls]',
'message': 'Use expect.toHaveBeenCalledTimes',
},
...commonNoRestrictedSyntax,
],
},
Expand Down
4 changes: 2 additions & 2 deletions test/cast/cast_receiver_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ filterDescribe('CastReceiver', castReceiverSupport, () => {

mockGenericMessageBus.broadcast.calls.reset();
fakeIncomingMessage(message, mockGenericMessageBus);
expect(mockGenericMessageBus.broadcast.calls.count()).toEqual(1);
expect(mockGenericMessageBus.broadcast).toHaveBeenCalledTimes(1);
expect(mockGenericMessageBus.broadcast.calls.argsFor(0)[0].includes(
'"requestId":0,"type":"MEDIA_STATUS"')).toBe(true);
});
Expand Down Expand Up @@ -857,7 +857,7 @@ filterDescribe('CastReceiver', castReceiverSupport, () => {

mockGenericMessageBus.broadcast.calls.reset();
fakeIncomingMessage(message, mockGenericMessageBus);
expect(mockGenericMessageBus.broadcast.calls.count()).toEqual(1);
expect(mockGenericMessageBus.broadcast).toHaveBeenCalledTimes(1);
expect(mockGenericMessageBus.broadcast.calls.argsFor(0)[0].includes(
'"requestId":0,' +
'"type":"INVALID_REQUEST",' +
Expand Down
63 changes: 23 additions & 40 deletions test/cast/cast_sender_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,68 +822,51 @@ describe('CastSender', () => {
* @param {boolean} yes If true, simulate receivers being available.
*/
function fakeReceiverAvailability(yes) {
const calls = mockCastApi.ApiConfig.calls;
expect(calls.count()).toEqual(1);
if (calls.count()) {
const onReceiverStatusChanged = calls.argsFor(0)[2];
onReceiverStatusChanged(yes ? 'available' : 'unavailable');
}
expect(mockCastApi.ApiConfig).toHaveBeenCalledTimes(1);
const onReceiverStatusChanged = mockCastApi.ApiConfig.calls.argsFor(0)[2];
onReceiverStatusChanged(yes ? 'available' : 'unavailable');
}

function fakeSessionConnection() {
const calls = mockCastApi.requestSession.calls;
expect(calls.count()).toEqual(1);
if (calls.count()) {
const onSessionInitiated = calls.argsFor(0)[0];
mockSession = createMockCastSession();
onSessionInitiated(mockSession);
}
expect(mockCastApi.requestSession).toHaveBeenCalledTimes(1);
const onSessionInitiated = mockCastApi.requestSession.calls.argsFor(0)[0];
mockSession = createMockCastSession();
onSessionInitiated(mockSession);
}

/**
* @param {string} code
*/
function fakeSessionConnectionFailure(code) {
const calls = mockCastApi.requestSession.calls;
expect(calls.count()).toEqual(1);
if (calls.count()) {
const onSessionError = calls.argsFor(0)[1];
onSessionError({code: code});
}
expect(mockCastApi.requestSession).toHaveBeenCalledTimes(1);
const onSessionError = mockCastApi.requestSession.calls.argsFor(0)[1];
onSessionError({code: code});
}

/**
* @param {?} message
*/
function fakeSessionMessage(message) {
const calls = mockSession.addMessageListener.calls;
expect(calls.count()).toEqual(1);
if (calls.count()) {
const namespace = calls.argsFor(0)[0];
const listener = calls.argsFor(0)[1];
const serialized = CastUtils.serialize(message);
listener(namespace, serialized);
}
expect(mockSession.addMessageListener).toHaveBeenCalledTimes(1);
const namespace = mockSession.addMessageListener.calls.argsFor(0)[0];
const listener = mockSession.addMessageListener.calls.argsFor(0)[1];
const serialized = CastUtils.serialize(message);
listener(namespace, serialized);
}

function fakeRemoteDisconnect() {
mockSession.status = 'disconnected';
const calls = mockSession.addUpdateListener.calls;
expect(calls.count()).toEqual(1);
if (calls.count()) {
const onConnectionStatus = calls.argsFor(0)[0];
onConnectionStatus();
}
expect(mockSession.addUpdateListener).toHaveBeenCalledTimes(1);
const onConnectionStatus =
mockSession.addUpdateListener.calls.argsFor(0)[0];
onConnectionStatus();
}

function fakeJoinExistingSession() {
const calls = mockCastApi.ApiConfig.calls;
expect(calls.count()).toEqual(1);
if (calls.count()) {
const onJoinExistingSession = calls.argsFor(0)[1];
mockSession = createMockCastSession();
onJoinExistingSession(mockSession);
}
expect(mockCastApi.ApiConfig).toHaveBeenCalledTimes(1);
const onJoinExistingSession = mockCastApi.ApiConfig.calls.argsFor(0)[1];
mockSession = createMockCastSession();
onJoinExistingSession(mockSession);
}

/**
Expand Down
26 changes: 13 additions & 13 deletions test/dash/dash_parser_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,17 @@ describe('DashParser Live', () => {

expect(manifest.periods.length).toBe(1);
// Should call filterAllPeriods for parsing the first manifest
expect(filterNewPeriod.calls.count()).toBe(0);
expect(filterAllPeriods.calls.count()).toBe(1);
expect(filterNewPeriod).toHaveBeenCalledTimes(0);
expect(filterAllPeriods).toHaveBeenCalledTimes(1);

fakeNetEngine.setResponseText('dummy://foo', secondManifest);
await updateManifest();

// Should update the same manifest object.
expect(manifest.periods.length).toBe(2);
// Should call filterNewPeriod for parsing the new manifest
expect(filterAllPeriods.calls.count()).toBe(1);
expect(filterNewPeriod.calls.count()).toBe(1);
expect(filterAllPeriods).toHaveBeenCalledTimes(1);
expect(filterNewPeriod).toHaveBeenCalledTimes(1);
});

it('uses redirect URL for manifest BaseURL and updates', async () => {
Expand Down Expand Up @@ -416,7 +416,7 @@ describe('DashParser Live', () => {

// The manifest request was made to the original URL.
// But includes a redirect
expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
const netRequest = fakeNetEngine.request.calls.argsFor(0)[1];
expect(netRequest.uris).toEqual([redirectedUri, originalUri]);

Expand All @@ -439,7 +439,7 @@ describe('DashParser Live', () => {
fakeNetEngine.setResponseText('dummy://foo', manifestText);
await parser.start('dummy://foo', playerInterface);

expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);

const error = new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand All @@ -449,7 +449,7 @@ describe('DashParser Live', () => {
fakeNetEngine.request.and.returnValue(operation);

await updateManifest();
expect(onError.calls.count()).toBe(1);
expect(onError).toHaveBeenCalledTimes(1);
});

it('uses @minimumUpdatePeriod', async () => {
Expand Down Expand Up @@ -556,7 +556,7 @@ describe('DashParser Live', () => {
const manifestRequest = shaka.net.NetworkingEngine.RequestType.MANIFEST;
await parser.start('dummy://foo', playerInterface);

expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
fakeNetEngine.expectRequest('dummy://foo', manifestRequest);
fakeNetEngine.request.calls.reset();

Expand All @@ -573,7 +573,7 @@ describe('DashParser Live', () => {
});

await updateManifest();
expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
});

it('uses @suggestedPresentationDelay', async () => {
Expand Down Expand Up @@ -803,7 +803,7 @@ describe('DashParser Live', () => {
.toBeRejected();
// start will only begin the network request, calling stop here will be
// after the request has started but before any parsing has been done.
expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
parser.stop();
await expectation;

Expand All @@ -825,7 +825,7 @@ describe('DashParser Live', () => {

await updateManifest();
// The request was made but should not be resolved yet.
expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
fakeNetEngine.expectRequest(manifestUri, manifestRequestType);
fakeNetEngine.request.calls.reset();
parser.stop();
Expand All @@ -846,7 +846,7 @@ describe('DashParser Live', () => {

await Util.shortDelay();
// This is the initial manifest request.
expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
fakeNetEngine.expectRequest(manifestUri, manifestRequestType);
fakeNetEngine.request.calls.reset();
// Resolve the manifest request and wait on the UTCTiming request.
Expand All @@ -855,7 +855,7 @@ describe('DashParser Live', () => {
await Util.shortDelay();

// This is the first UTCTiming request.
expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
fakeNetEngine.expectRequest(dateUri, dateRequestType);
fakeNetEngine.request.calls.reset();
// Interrupt the parser, then fail the request.
Expand Down
12 changes: 6 additions & 6 deletions test/dash/dash_parser_segment_base_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('DashParser SegmentBase', () => {
expect(manifest).toEqual(Dash.makeManifestFromInit('init.webm', 201, 300));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(3);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(3);
fakeNetEngine.expectRangeRequest('http://example.com', 100, 200);
fakeNetEngine.expectRangeRequest('http://example.com/init.webm', 201, 300);
});
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('DashParser SegmentBase', () => {
expect(manifest).toEqual(Dash.makeManifestFromInit('init.mp4', 201, 300));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest('http://example.com', 100, 200);
});

Expand All @@ -127,7 +127,7 @@ describe('DashParser SegmentBase', () => {
expect(manifest).toEqual(Dash.makeManifestFromInit('init.mp4', 201, 300));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest('http://example.com', 100, 200);
});

Expand Down Expand Up @@ -155,7 +155,7 @@ describe('DashParser SegmentBase', () => {
expect(manifest).toEqual(Dash.makeManifestFromInit('stream.mp4', 201, 300));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest('http://example.com/stream.mp4', 100, 200);
});

Expand Down Expand Up @@ -190,7 +190,7 @@ describe('DashParser SegmentBase', () => {
Dash.makeManifestFromInit('init.mp4', 201, 300, 10));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest('http://example.com/index.mp4', 5, 2000);
});

Expand Down Expand Up @@ -224,7 +224,7 @@ describe('DashParser SegmentBase', () => {
Dash.makeManifestFromInit('special.mp4', 0, null, 20));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest('http://example.com', 30, 900);
});

Expand Down
10 changes: 5 additions & 5 deletions test/dash/dash_parser_segment_template_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('DashParser SegmentTemplate', () => {
Dash.makeManifestFromInit('init-500.mp4', 0, null));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest(
'http://example.com/index-500.mp4', 0, null);
});
Expand All @@ -159,7 +159,7 @@ describe('DashParser SegmentTemplate', () => {
Dash.makeManifestFromInit('init-500.mp4', 0, null));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest(
'http://example.com/index-500.mp4', 0, null);
});
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('DashParser SegmentTemplate', () => {
Dash.makeManifestFromInit('init-500.webm', 0, null));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(3);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(3);
fakeNetEngine.expectRangeRequest(
'http://example.com/init-500.webm', 0, null);
fakeNetEngine.expectRangeRequest(
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('DashParser SegmentTemplate', () => {
Dash.makeManifestFromInit('init-500.mp4', 0, null));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest(
'http://example.com/index-500.mp4', 0, null);
});
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('DashParser SegmentTemplate', () => {
Dash.makeManifestFromInit('init-500.mp4', 0, null));
await Dash.callCreateSegmentIndex(manifest);

expect(fakeNetEngine.request.calls.count()).toBe(2);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(2);
fakeNetEngine.expectRangeRequest(
'http://example.com/index-500.mp4', 0, null);
});
Expand Down
2 changes: 1 addition & 1 deletion test/hls/hls_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ describe('HlsParser live', () => {

// Only one request was made, and it was for the playlist.
// No segment requests were needed to get the start time.
expect(fakeNetEngine.request.calls.count()).toBe(1);
expect(fakeNetEngine.request).toHaveBeenCalledTimes(1);
fakeNetEngine.expectRequest(
'test:/video',
shaka.net.NetworkingEngine.RequestType.MANIFEST);
Expand Down
2 changes: 1 addition & 1 deletion test/hls/hls_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ describe('HlsParser', () => {
playerInterface.filterAllPeriods = Util.spyFunc(filterAllPeriods);

await parser.start('test:/master', playerInterface);
expect(filterAllPeriods.calls.count()).toBe(1);
expect(filterAllPeriods).toHaveBeenCalledTimes(1);
});

it('gets mime type from header request', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/media/drm_engine_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('DrmEngine', () => {
if (requestSpy.calls.count()) {
// We made a license request.
// Only one request should have been made.
expect(requestSpy.calls.count()).toBe(1);
expect(requestSpy).toHaveBeenCalledTimes(1);
// So it's reasonable to assume that this requestComplete Promise
// is waiting on the correct request.
await requestComplete;
Expand Down
Loading

0 comments on commit e2ba179

Please sign in to comment.