Skip to content

Commit

Permalink
Improve consistency of time-based testing
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmalmgren committed Aug 14, 2017
1 parent 89bef01 commit c0c26d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/app/components/pages/sermons/sermon.spec.ts
@@ -1,6 +1,6 @@
import { expect, sinon, async, MockBuilder } from 'testing';
import { ActivatedRoute } from '@angular/router';
import { SermonComponent } from './sermon';
import { expect, sinon, async, MockBuilder, callCount } from 'testing';
import { ActivatedRoute } from '@angular/router';
import { SermonComponent } from './sermon';
import {
Analytics,
FeatureToggles,
Expand Down Expand Up @@ -49,15 +49,16 @@ describe('SermonComponent', () => {
.build();

const sermon = new SermonComponent(activatedRoute, analytics, null, null, null, null, youtubeService);
sermon.analyticsInterval = 10;
sermon.analyticsInterval = 5;
sermon.videoState = VideoState.PLAYING;
sermon.sermon = { youtube: 'Jesus4Life' } as Sermon;

sermon.ngOnInit();

window.setTimeout(() => {
expect(analytics.event).to.have.been.calledTwice
.and.to.have.been.calledWith('Sermon', 'Playing', 'Jesus4Life');
/* Giving a reasonable range to account for annoying time based testing */
expect(callCount(analytics.event)).to.be.at.least(2).and.at.most(6);
expect(analytics.event).to.have.been.calledWith('Sermon', 'Playing', 'Jesus4Life');
sermon.ngOnDestroy();
}, 25);
}));
Expand All @@ -76,7 +77,7 @@ describe('SermonComponent', () => {
.build();

const sermon = new SermonComponent(activatedRoute, analytics, null, null, null, null, youtubeService);
sermon.analyticsInterval = 10;
sermon.analyticsInterval = 5;
sermon.videoState = VideoState.PLAYING;
sermon.live = true;
sermon.sermon = { youtube: 'Jesus4Life' } as Sermon;
Expand Down Expand Up @@ -110,7 +111,7 @@ describe('SermonComponent', () => {
.build();

const sermon = new SermonComponent(activatedRoute, analytics, null, null, null, null, youtubeService);
sermon.analyticsInterval = 10;
sermon.analyticsInterval = 5;
sermon.videoState = state;
sermon.sermon = { youtube: 'Jesus4Life' } as Sermon;

Expand Down
4 changes: 4 additions & 0 deletions src/testing.ts
Expand Up @@ -42,6 +42,10 @@ export class MockBuilder<T> {
}
}

export function callCount(spy: any) {
return spy.callCount;
}

export class Loop {
static times(n: number) {
return new Loop(n);
Expand Down

0 comments on commit c0c26d2

Please sign in to comment.