Search code examples
angulartypescriptjasmine

Unit testing click event in Angular


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 is called which is defined in the .ts class file. That function prints a message in the console.log window saying that the button has been pressed. My current testing code tests for printing of the console.log message:

describe('Component: ComponentToBeTested', () => {
    var component: ComponentToBeTested;

    beforeEach(() => {
        component = new ComponentToBeTested();
        spyOn(console, 'log');
    });

    it('should call onEditButtonClick() and print console.log', () => {
        component.onEditButtonClick();
        expect(console.log).toHaveBeenCalledWith('Edit button has been clicked!);
    });
});

However, this only tests the controller class, not the HTML. I don't just want to test that the logging happens when onEditButtonClick is called; I also want to test that onEditButtonClick is called when the user clicks the edit button defined in the component's HTML file. How can I do that?


Solution

  • 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<TestComponent>;
      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()

    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.

    See also: