Angular Unit Test - click on a directive that does not run

Problem

I am trying to click input radiofrom value="agency"from a directive template with the ultimate goal of changing the scope variable newDatasourceType.

Appropriate source

Directive Template

  <div ng-if="!datasources" class="new-datasource">
        <input name="agencyOrWidget" type="radio" ng-model="newDatasourceType" value="agency" />
        <label>Agency</label>
        <input name="agencyOrWidget" type="radio" ng-model="newDatasourceType" value="widget" />
        <label>Widget</label>
        <select ng-if="newDatasourceType=='widget'" name="{{inputPrefix}}[0][widget_id]">
              <option>Choose a widget...</option>
              <option ng-repeat="(id, options) in widgetList" value="{{id}}">{{id}}</option>
        </select>
        <select ng-if="newDatasourceType=='agency'" name="{{inputPrefix}}[0][agency_id]">
              <option>Choose an agency...</option>            
              <option ng-repeat="(id, name) in agenciesList" value="{{id}}">{{name}}</option>
        </select>
  </div>

Corresponding Unit Test

Parts that have tried and notes are in the comments.

it('Select Agencies', function() {
    // Running this to see if it does anything
    scope.newDatasourceType = 'agency';

    //create the element with the directive template
    elem = $compile(directive)(scope);
    scope.$digest();

    // Returns `1`
    console.log(elem.find('input[value="agency"]').length);

    // elem.find('input[value="agency"]').triggerHandler('click');
    elem.find('input[value="agency"]').click(function() {
        // Doesn't get triggered
        console.log('click');
    });

    // Didn't do anything...
    // scope.$digest();
    // scope.$apply();


    // console.log(elem.html());

    // Test items
    expect(elem.find('select option:nth-child(2)').val()).toBe(Object.keys(scope.agenciesJson)[0]);
});

What do I see

Regardless if I manually installed newDatasourceTypeor try to click on it, only the selection box c will be shown Choose a widget....

+4
source share
1 answer
+2

Source: https://habr.com/ru/post/1612467/


All Articles