簡體   English   中英

角度單位測試 - 單擊指令不觸發

[英]Angular Unit Test - Click in directive not triggering

問題

我試圖從指令模板點擊帶有value="agency"radio輸入,最終目標是更改范圍變量newDatasourceType

相關來源

指令模板

  <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>

相關單元測試

嘗試的部分和注釋在注釋中。

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]);
});

我在看什么

無論我手動設置newDatasourceType還是嘗試單擊它,只有Choose a widget...的選擇塊Choose a widget...可見。

除了觸發click事件之外,您還必須更改radiobutton上的checked屬性。 這是一個例子http://jsfiddle.net/k1axxs21/1/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM