Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Mock class object for condition (instance of) to be true in unit testing angular

I need help in order to write unit tests for router.events.subscribe. I used ActivationEnd to use snapshot object which has properties which I need for my application. Below is my code snippet.

In constructor,

this.router.events.subscribe(e => this.onRouterEvent(e))
onRouterEvent(e: Event): void {
      if (e instanceof ActivationEnd) {
        this.section = e.snapshot.data.title;
        this.dynamicSubNavLabel = e.snapshot.routeConfig?.data?.label;
      }
  }

Unit test File

class ActivationEnd {
  snapshot: any = {
    data: {
      title: 'a title'
    },
    routeConfig: {
      data: {
        label: 'a label'
      }
    },
    params: {
      orderID: "3525",
      projectID: "24",
      usecaseID: "3"
    }
  };
}

class MockRouter {
  events: Observable<Event> = new Observable(observer => {
    observer.next(new ActivationEnd()); // This does NOT make the condition true (e instance of ActivationEnd)
    observer.complete();
  });
  get url(): string { return 'asd'; }
  navigateByUrl(url: string) { return url; }
  navigate(path: string) { return path; }
}

and using MockRouter class in providers

    TestBed.configureTestingModule({
      declarations: [AppComponent],
      imports: [
        RouterTestingModule
      ],
      providers: [ 
        { provide: Router, useClass: MockRouter },
      ],
      schemas: [NO_ERRORS_SCHEMA]
    })

Can you please help me how can I mock ActivationEnd event and pass it to onRouterEvent() so that if conditions evaluates to be true ?

Regards, Alpesh

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!