Angular Unit Testing
Welcome {{customerName}}
tag, Canada Uni d States 821A Bloor Street West, Toronto, Ontario, MEG 1M1 744 Yorkway Place, Jenkintown, PA. 19046 11866 206 4644 [email protected] 23 1877 517 6540 [email protected]Rg Pt Pl a aN 1.27 The DebugElement Class = This class provides utilities to help write test scripts. a Useful properties: ¢ componentinstance - Same as ComponentFixture.componentinstance © nativeElement - Same as ComponentFixture.nativeElement © parent : DebugElement - The parent element. = Useful methods: © query()/queryAll() - Obtain child DebugElement by CSS selector query. ¢ triggerEventHandler() - Used to simulate user interactions like button clicks. Canada United Statos {821A Bloor Streot West, Toronto, Ontario, MEG M1 744 Yarkway Pace, Jenkintown, PA. 18045 24 11866 206 4644 [email protected] 1877 517 6540 [email protected]Rg Py lee e re Ne 1.28 Simulating User Interaction = Here we trigger a button click event and verify the change in the component's state. import { By } from '@angular/platform-browser'; it('Should handle click", () let button : DebugElement = fixture .debugElement. query (By.css ("button") ) >{ button. triggerEventHandler("click", null) fixture.detectChanges (); expect (component.customerName) .toBe ("Bugs Bunny") let paraText = fixture nativeElement.querySelector('p') .textContent. expect (paraText) .toBe('Welcome Bugs Bunny!) ; We Canada 821A Bloor Street West, Toronto, Ontario, MEG M1744 Yorkway Place, Jenkintown, PA. 19046 25 11866 206 4644 [email protected] 1877 517 6540 [email protected]Rg Pt Pl a aN 1.29 Summary = Jasmine and Karma are the two main technologies used in Angular component unit testing ¢ Test code is written using Jasmine ¢ Karma provides automation for configuring and running the tests = Jasmine tests are defined within ‘describe’ functions that have ‘beforeEach' functions for test setup and ‘it' functions for the actual tests = The Angular TestBed class is the most important class for test configuration and execution «= Aseparate Angular module is created for each test suite. Tests are run using that module and not using the application module. = Angular provides us ways to inject service instances and test synchronous and asynchronous methods. = Components are tested in isolation and not as a part of a larger application. We can use standard DOM AP! to test the validity of the DOM generated by a component. Canada United States 821A Bloor Street West, Toronto, Ontario, MEG M1744 Yorkway Pl 11866 206 4644 [email protected] 1877 517 6540 9 Jenkintown, PA. 19046 26 [email protected]