Home > OS >  Mocked Angular navigateByUrl still running page reload
Mocked Angular navigateByUrl still running page reload

Time:12-22

I am trying to write a test for a button click which triggers a navigateByUrl function call with some params.

I have mocked Router and provided to the testing module as such

let mockRouter = {
navigateByUrl: jasmine.createSpy("navigateByUrl").and.callFake(() => {
  console.log("fake is called");
}),
};

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientModule],
      declarations: [RecoverPasswordComponent],
      providers: [{ provide: Router, useValue: mockRouter }],
    }).compileComponents();
  }));

when i click the button and navigateByUrl is triggered, i am getting the console as written but i am also getting a error as Some of your tests did a full page reload! which means that actual navigateByUrl is triggered.

I am not able to get an idea of, if the function is spied than why the actual function call is triggered?

CodePudding user response:

I Finally figured out what was causing this! So basically i had a form in which my button was stated, as the form was not binded with ng-submit callback so on clicking the button the default event for a form button was triggered which reloaded the test suite.

  • Related