Lets say I have a component that access parent route like this:
ngOnInit() {
//TODO - FFBZAP-25: far from an ideal solution
this.sub = this.route.parent.parent.parent.params.subscribe(params => {
this.siteId = params["siteId"];
this.loadContents();
this.loadTypes(this.siteId);
});
}
My issue is that when I run the test I get
Failed: Cannot read property 'undefined' of undefined
I am not testing the route right now, but I think error should be Because of this line:
- useValue: { parent: { parent: { parent: { 'params': Observable.from([{ 'siteId': '156' }]) } } } },
Here is my Unit-test code:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpModule,
RouterTestingModule,
SharedModule,
BootstrapModalModule
],
declarations: [
ContentListComponent,
],
providers: [
SiteService,
UserService,
Modal,
Overlay,
{
provide: ActivatedRoute,
useValue: { parent: { parent: { parent: { 'params': Observable.from([{ 'siteId': '156' }]) } } } },
},
OverlayRenderer,
AuthHttp,
MessageService,
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents()
.then(() => {
fixture = TestBed.createComponent(ContentListComponent);
component = fixture.componentInstance;
siteService = TestBed.get(SiteService);
userService = TestBed.get(UserService);
activatedRoute = TestBed.get(ActivatedRoute);
});
}));
console.log("HEUUUYFYYHSIDDHARTHs", { parent: { parent: { parent: { 'params': Observable.from([{ 'siteId': '11' }]) } } } })
it('should have a defined component', () => {
expect(component).toBeDefined();
});
it('Sholud get content-list', async(() => {
spyOn(userService, 'getUser')
.and.returnValue(Observable.of(UpdatedBy));
spyOn(siteService, 'getSiteContents')
.and.returnValue(Observable.of(contentList));
fixture.detectChanges();
component = new ContentListComponent(activatedRoute, mockModal, mockRouter, siteService, userService);
component.ngOnInit();
fixture.whenStable()
.then(() => {
fixture.detectChanges();
return fixture.whenStable();
})
.then(() => {
const totalRows = de.queryAll(By.css('tr'));
expect(totalRows.length).toEqual(3);
});
}));
});
I have tried to take reference of following links. But I could now get satisfactory result How to mock an activatedRoute parent Route