jeudi 30 août 2018

Angular - Unit testing focusing on button

I am attempting to unit test whether my button has been focused on, but I cannot seem to get the spy to work correctly?

I saw this post, but it hasn't helped fully.

Am I missing something obvious?

component.ts

ngOnInit() {
    // autofocus on the cancel button to guard against mistakes
    document.getElementById('cancel').focus();
  }

component.spec.ts

beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ ConfirmationComponent ],
      providers: [ MessageService]
    });

    fixture = TestBed.createComponent(ConfirmationComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

    component.ngOnInit();
  });

  it('should autofocus on cancel button on init', () => {
    const cancelButton = document.getElementById('cancel');
    spyOn(cancelButton, 'focus');
    expect(cancelButton.focus).toHaveBeenCalled();
  });

Aucun commentaire:

Enregistrer un commentaire