Home >php教程 >PHP开发 >AngularJS auxiliary library browserTrigger usage example

AngularJS auxiliary library browserTrigger usage example

高洛峰
高洛峰Original
2016-12-08 09:23:451238browse

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(&#39;<select ng-model="robot">&#39; +
       &#39;<option value="">--select--</option>&#39; +
       &#39;<option value="x">robot x</option>&#39; +
       &#39;<option value="y">robot y</option>&#39; +
      &#39;</select>&#39;);
  expect(element).toEqualSelect(&#39;&#39;, [&#39;x&#39;], &#39;y&#39;);
  browserTrigger(element.find(&#39;option&#39;).eq(0));
  expect(element).toEqualSelect([&#39;&#39;], &#39;x&#39;, &#39;y&#39;);
  expect(scope.robot).toBe(&#39;&#39;);
});

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.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn