1

I have one issue with angular2 testing. I have some child component with theirs own routing, route config generated dynamically from service, and when I try to test this component I got error about non exist getter (it`s normal). But how and where I should pass this getter into process of component creataion?

My component

import {Component, Input} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES, Router} from 'angular2/router';
/* ------- !Angular 2 native components  ---------*/
import {routeGeneratorInstance} from '../../config/routes/route_instructions_generator';


import {Config} from '../../config/config';
/* ------- !Config  ---------*/

const MODULE_PATH: string = `/${Config.getProdFolderName()}/modules/patients`;

@Component({
  selector: 'patients',
  template: `
    <router-outlet ></router-outlet>
  `,
  directives: [ROUTER_DIRECTIVES]
})

@RouteConfig(routeGeneratorInstance.getRoutesInstructions().patients)

export class PatientsComponent {}

and my test

import {setBaseTestProviders} from 'angular2/testing';
import {
TEST_BROWSER_PLATFORM_PROVIDERS,
TEST_BROWSER_APPLICATION_PROVIDERS
} from 'angular2/platform/testing/browser';

import { beforeEach,
beforeEachProviders,
describe,
expect,
provide,
it,
inject,
injectAsync,
TestComponentBuilder,
AsyncTestCompleter} from 'angular2/testing';

import {routeGeneratorInstance} from '../../config/routes/route_instructions_generator';
import {PatientsComponent} from './patients.component';

describe('Patients component test', () => {

  it('Should be able to test', injectAsync([TestComponentBuilder],
  (tcb: TestComponentBuilder) => {
    return tcb.createAsync(PatientsComponent).then((componentFixture) => {
      componentFixture.detectChanges();
      expect(true).toBe(true);
    });
  }));

});

Got error: Cannot read property 'patients' of undefined

I understand that I not passed this data into RouteConfig when I simulate component creatin, but how and where to do this? Thanks for any help!

Mr.Web
  • 6,992
  • 8
  • 51
  • 86
Velidan
  • 5,526
  • 10
  • 48
  • 86
  • Looks like `routeGeneratorInstance.getRoutesInstructions()` returns `undefined`. – Günter Zöchbauer Apr 14 '16 at 13:19
  • Hi Gunter. It is can be true, because I fetch data from server before app started. So how I can mock this and inject redy-data into my component in test? (I can`t use RouteConfig right in spec.file) Can you help me please? – Velidan Apr 14 '16 at 13:37
  • maybe some way exist how to do this? I tryed to run test in callback ater route dynamic data fetched but it's not work. Maybe exist some way how to inject ready-data on creating phase component in test? – Velidan Apr 14 '16 at 14:08
  • 1
    That's a bit cumbersome because this runs outside Angulars DI. I don't have a good idea how to fix this. – Günter Zöchbauer Apr 14 '16 at 14:10
  • 1
    Thanks Gunter for your answer :) that's enough for me =) – Velidan Apr 14 '16 at 14:20
  • If that helps already - any time again :D – Günter Zöchbauer Apr 14 '16 at 14:21

0 Answers0