lundi 19 octobre 2020

How to write TEST CASES to know which component is loaded in router-outlet in Angular 6 and above (Jasmine and Karma)

This is my app.component

<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>

In the Header component I want to test which component is loaded

I have a function which return bool value if either is loaded. I want to test that function in my test suite

someComponentLoaded(): boolean {
    return (
      this.loadedComponent === HomeComponent ||
      this.loadedComponent === DirectoryComponent
    );
  }

App.routing looks like this -

const routes: Routes = [
  { path: '' , component: HomeComponent},
  { path: 'directory' , component: DirectoryComponent }
];

This is my suite -

it('should test someComponentLoaded', async( () =>{
      fixture = TestBed.createComponent(HeaderComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
      fixture.componentInstance.someComponentLoaded();
      console.log("test loaded component", fixture.componentInstance.someComponentLoaded());
    })
  )

console.log always gives false even if condition is true

Aucun commentaire:

Enregistrer un commentaire