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?