1

I'm building a loginform component with the following interface:

<login-form onlogin="submit()"></login-form>

And that's the testing code:

  it("Should send credentials out of the component", async(() => {
    TestBed.compileComponents().then(() => {
      const fixture = TestBed.createComponent(LoginFormComponent);
      let component = fixture.componentInstance;
      spyOn(component.onlogin, "emit");

      let domRef = fixture.nativeElement;
      domRef.querySelector("input[name='username']").value = "test@test.com";
      domRef.querySelector("input[name='password']").value = '1234';
      domRef.querySelector("form").dispatchEvent(new Event('submit'));

      fixture.detectChanges();

      let expectedSubmitData:UserCredentials = {
        username: "test@test.com",
        password: "1234"
      };
      expect(component.onlogin.emit).toHaveBeenCalledWith(expectedSubmitData);
    });
  }));

I'm getting this message:

Expected spy emit to have been called with [ Object({ username: 'test@test.com', password: '1234' }) ] but actual calls were [ Object({ username: '', password: '' }) ]

So, what's the way to fill input values and the model detects those changes?

Serginho
  • 7,291
  • 2
  • 27
  • 52

1 Answers1

1

Please try to do the following things,It worked for me!!.

let domRef = fixture.nativeElement;
let userInput = domRef.querySelector("input[name='username']")
userInput.value = "test@test.com";
userInput.dispatchEvent(new Event('input'));
let passwordInput = domRef.querySelector("input[name='password']")
passwordInput.value = '1234';
passwordInput.dispatchEvent(new Event('input'));
fixture.detectChanges();
domRef.querySelector("form").dispatchEvent(new Event('submit'));
let expectedSubmitData:UserCredentials = {
   username: "test@test.com",
   password: "1234"
};
expect(component.onlogin.emit).toHaveBeenCalledWith(expectedSubmitData);
Vipin
  • 847
  • 1
  • 10
  • 21