The example in this article describes the usage of AngularJS auxiliary library browserTrigger. Share it with everyone for your reference, the details are as follows:
Today I recommend a unit test auxiliary library browserTrigger from the angularjs source code. This is a piece of code from ngScenario. The main user triggers browser-type behavior to update the value of the scope view model in ng.
This is an example of using browserTrigger for unit testing in angularjs source code:
it('should set the model to empty string when empty option is selected', function() { scope.robot = 'x'; compile('<select ng-model="robot">' + '<option value="">--select--</option>' + '<option value="x">robot x</option>' + '<option value="y">robot y</option>' + '</select>'); expect(element).toEqualSelect('', ['x'], 'y'); browserTrigger(element.find('option').eq(0)); expect(element).toEqualSelect([''], 'x', 'y'); expect(scope.robot).toBe(''); });
In this code, pass in the select option you want to select to browserTrigger, and it will help you tigger change and select the current option. It also triggers the update of the viewmodel of ng select.
In browserTrigger, we also made many other trigger interfaces for input boxes or HTML controls, and also added browser compatibility. This makes our testing more convenient without having to consider browser compatibility or different HTML controls triggering different events to update the scope value.
For more information, please refer to ng’s official test and browserTrigger source code.