Unit testing click event in Angular

前端 未结 5 1909
闹比i
闹比i 2020-12-04 09:00

I\'m trying to add unit tests to my Angular 2 app. In one of my components, there is a button with a (click) handler. When the user clicks the button a function

5条回答
  •  旧巷少年郎
    2020-12-04 09:51

    My objective is to check if the 'onEditButtonClick' is getting invoked when the user clicks the edit button and not checking just the console.log being printed.

    You will need to first set up the test using the Angular TestBed. This way you can actually grab the button and click it. What you will do is configure a module, just like you would an @NgModule, just for the testing environment

    import { TestBed, async, ComponentFixture } from '@angular/core/testing';
    
    describe('', () => {
      let fixture: ComponentFixture;
      let component: TestComponent;
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          imports: [ ],
          declarations: [ TestComponent ],
          providers: [  ]
        }).compileComponents().then(() => {
          fixture = TestBed.createComponent(TestComponent);
          component = fixture.componentInstance;
        });
      }));
    });
    

    Then you need to spy on the onEditButtonClick method, click the button, and check that the method was called

    it('should', async(() => {
      spyOn(component, 'onEditButtonClick');
    
      let button = fixture.debugElement.nativeElement.querySelector('button');
      button.click();
    
      fixture.whenStable().then(() => {
        expect(component.onEditButtonClick).toHaveBeenCalled();
      });
    }));
    

    Here we need to run an async test as the button click contains asynchronous event handling, and need to wait for the event to process by calling fixture.whenStable()

    See Also:

    UPDATE

    It is now preferred to use fakeAsync/tick combo as opposed to the async/whenStable combo. The latter should be used if there is an XHR call made, as fakeAsync does not support it. So instead of the above code, refactored, it would look like

    it('should', fakeAsync(() => {
      spyOn(component, 'onEditButtonClick');
    
      let button = fixture.debugElement.nativeElement.querySelector('button');
      button.click();
      tick();
      expect(component.onEditButtonClick).toHaveBeenCalled();
    
    }));
    

    Don't forget to import fakeAsync and tick

    • Testing documentation

提交回复
热议问题