Edit: I figured it out, the problem is spyOn
will make the function it spy on to return undefined
, need to call and.callThough
explicitly.
I'm trying to test my component, which uses ngrx store. I'm stubbing my store in that unit test like this:
meetup-view.component.spec.ts
class ActivatedRouteStub {
private subject = new Subject();
push(value) {
this.subject.next(value);
}
get params() {
return this.subject.asObservable();
}
}
class StoreStub {
storeState = {
auth: Observable.of({}),
meetups: Observable.of([{key: '1'}])
};
select(value) {
return this.storeState[value];
}
dispatch() {
}
}
describe('MeetupCreateComponent', () => {
let component: MeetupCreateComponent;
let fixture: ComponentFixture<MeetupCreateComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [FormsModule],
declarations: [ MeetupCreateComponent ],
providers: [
{ provide: ActivatedRoute, useClass: ActivatedRouteStub },
{ provide: Store, useClass: StoreStub }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MeetupCreateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should init edit/view mode correctly', () => {
const route: ActivatedRouteStub = TestBed.get(ActivatedRoute);
const store: StoreStub = TestBed.get(Store);
const spy = spyOn(store, 'select').and.callThrough();
route.push({id: 1});
expect(spy).toHaveBeenCalled();
expect(component.editMode).toBe(true);
});
});
the problem is, in my component, I use concatMap (and import it using import 'rxjs/add/operator/concatMap';
)
meetup-view.component.ts
this.meetup$ = this.store.select('meetups')
.concatMap(meetups => {
return meetups.filter(m => m.key === key);
});
and when I run my unit test, I always get this error: Cannot read property 'concatMap' of undefined
.
I already tried to import concatMap
to my unit tests but no luck. Been searching for hours now :(.