angular - how to do unit testing of component with multiple parent routes of ActivatedRoute -
lets have component access parent route this:
ngoninit() { //todo - ffbzap-25: far ideal solution this.sub = this.route.parent.parent.parent.params.subscribe(params => { this.siteid = params["siteid"]; this.loadcontents(); this.loadtypes(this.siteid); }); }
my issue when run test get
failed: cannot read property 'undefined' of undefined
i not testing route right now, think error should because of line:
- usevalue: { parent: { parent: { parent: { 'params': observable.from([{ 'siteid': '156' }]) } } } },
here 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 defined component', () => { expect(component).tobedefined(); }); it('sholud 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 take reference of following links. satisfactory result how mock activatedroute parent route
Comments
Post a Comment