jeudi 12 mars 2020

Angular test a router event like NavigationEnd

I'm searching from last 2 days to simulate a router event in order to test

    window.scrollTo(0, 0);

In this block :

      ngOnInit() {
        this.router.events.subscribe((evt) => {
          if (!(evt instanceof NavigationEnd)) {
            return;
          }
          window.scrollTo(0, 0);
          if (this.object.attribut1) {
            this.loadSomething();
          }
          if (this.object.attribut2) {
            this.loadSomething2();
          }
        });
      }

So i need to simulate this : this.router.events.subscribe((evt) in test

I already have a test for this window.scrollTo (that works but only if window.scrollTo is out of the router event) :

      it('calls window.scrollTo', () => {
    expect(component).toBeDefined();

    const req = httpTestingController.expectOne(endpoint);
    expect(req).toBeDefined();
    expect(req.request.method).toEqual('GET');
    req.flush(MOCKED);
    fixture.detectChanges();

    expect(spyScrollTo).toHaveBeenCalled();
  });

I found many examples but nothing help me and i never understand theses examples.

I will be really thanksfull if someone got explanations or examples Thanks :)

Aucun commentaire:

Enregistrer un commentaire