dimanche 5 mars 2017

Angular2 test with activated route and resolve (undefined unsubscribe)

inside router

{path: CONSTANTS.BOOKING_STEPS.CONFIRMATION, component: Confirmation.ConfirmationComponent, resolve: {bookingExtras: ConfirmationResolver}}

inside component

  reservation: ConfirmationInterface;
  passengersCount: number;
  subscription: Subscription;

  constructor(private activatedRoute: ActivatedRoute) {}

  ngOnInit() {
    this.subscription = this.activatedRoute.data.subscribe((response) => {
      this.reservation = get(response, 'bookingExtras');
      this.passengersCount = sum([
        this.reservation.passengerSummary.adultsCount,
        this.reservation.passengerSummary.childrensCount,
        this.reservation.passengerSummary.infantsCount
      ]);
    });
  }

  ngOnDestroy() {
   this.subscription.unsubscribe();
  }

and inside component.spec

describe('ConfirmationComponent', () => {
  let component: ConfirmationComponent;
  let fixture: ComponentFixture<ConfirmationComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule.withRoutes([])
      ],
      declarations: [ ConfirmationComponent, SplitDatePipe ],
      schemas: [NO_ERRORS_SCHEMA]
    })
    .compileComponents();
  }));

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

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

Get an error that cannot read property 'unsubscribe' of undefined. All works great with ng serve but there is a problem with testing. It looks like the subscription doesn't exist at all. I tried to do something like:

 providers: [
 {
  provide: ActivatedRoute,
    useValue: {
      data: {
        bookingExtras: ConfirmationResolver
      }
    }}
  ]

but still no effect. Could you please tell me how am I suppose to test components which are based on resolve value?

Aucun commentaire:

Enregistrer un commentaire