Как смоделировать состояние маршрутизации в угловых для модульных тестов - PullRequest
0 голосов
/ 06 ноября 2019

Я пишу модульные тесты для моего компонента, но у меня возникли проблемы при создании экземпляра компонента и отображается следующая ошибка:

TypeError: Cannot read property 'patientId' of null 

Я попытался смутить всех провайдеров, включая маршрутизатор и активный маршрутизатор, вот какЯ звоню компоненту:

  public onSelectedOrganizationInsurance(organizationInsurance: OrganizationInsurance) {
    this.router.navigate(['../search-insurance-plan'], {
      relativeTo: this.activatedRoute,
      state: {
        selectedOrganizationInsurance: organizationInsurance,
        patientId: this.patientId
      }
    });

Мой component.ts - это

export class PatientInsurancePlanSearchComponent implements OnInit {

  private patientId: number = -1;
  public selectedOrganizationInsurance: OrganizationInsurance;
  public organizationInsurancePlans$: Observable<OrganizationInsurancePlan[]>;

  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private biilingHttpService: BillingHttpService
  ) {
    this.selectedOrganizationInsurance = new OrganizationInsurance();
  }

  ngOnInit() {
    this.patientId = history.state.patientId as number;
    this.selectedOrganizationInsurance = history.state.selectedOrganizationInsurance as OrganizationInsurance;
    this.organizationInsurancePlans$ = this.biilingHttpService.getOrganizationInsurancePlans(this.selectedOrganizationInsurance.id);
  }

spec.ts

class FakeInsurancePlanSearchComponent {
  @Input() public organizationInsurancePlans: OrganizationInsurancePlan[] = [];
  @Input() public selectedOrganizationInsurance: OrganizationInsurance;
  }
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ PatientInsurancePlanSearchComponent
      , FakeInsurancePlanSearchComponent ],
      imports: [
        StoreModule.forRoot({}),
        HttpClientModule,
        RouterTestingModule,
      ],
      providers: [
        Store,
        {
          provide: ActivatedRoute, useValue: {
            state: of({ selectedorganizationInsurancePlan: 'AETNA'})
        }
      },
      BillingHttpService
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(PatientInsurancePlanSearchComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

Пожалуйста, сообщите мне, что мне не хватает.

...