Skip to content

Instantly share code, notes, and snippets.

@LayZeeDK
Last active August 14, 2018 22:10
Show Gist options
  • Save LayZeeDK/5cacdbf7c722d10b7e90057fb6c3891a to your computer and use it in GitHub Desktop.
Save LayZeeDK/5cacdbf7c722d10b7e90057fb6c3891a to your computer and use it in GitHub Desktop.
Heroes: Container component test suite setup
import { fakeAsync, tick } from '@angular/core/testing';
import { asapScheduler, of as observableOf, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { femaleMarvelHeroes } from '../../test/female-marvel-heroes';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
import { HeroesContainerComponent } from './heroes.container';
describe(HeroesContainerComponent.name, () => {
function createHeroServiceStub(): jasmine.SpyObj<HeroService> {
const stub: jasmine.SpyObj<HeroService> = jasmine.createSpyObj(
HeroService.name,
[
'addHero',
'deleteHero',
'getHeroes',
]);
resetHeroServiceStub(stub);
return stub;
}
function resetHeroServiceStub(stub: jasmine.SpyObj<HeroService>): void {
stub.addHero
.and.callFake(({ name }: Partial<Hero>) => observableOf({
id: 42,
name,
}, asapScheduler))
.calls.reset();
stub.deleteHero
.and.callFake((hero: Hero) => observableOf(hero, asapScheduler))
.calls.reset();
stub.getHeroes
.and.returnValue(observableOf(femaleMarvelHeroes, asapScheduler))
.calls.reset();
}
let container: HeroesContainerComponent;
const destroy: Subject<void> = new Subject();
const heroServiceStub: jasmine.SpyObj<HeroService> = createHeroServiceStub();
const observer: jasmine.Spy = jasmine.createSpy('heroes observer');
beforeEach(fakeAsync(() => {
container = new HeroesContainerComponent(heroServiceStub);
container.heroes$.pipe(takeUntil(destroy)).subscribe(observer);
tick();
}));
afterEach(() => {
destroy.next();
observer.calls.reset();
resetHeroServiceStub(heroServiceStub);
});
afterAll(() => {
destroy.complete();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment