gpt4 book ai didi

javascript - Angular 单元测试 - 单击指令未触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:10 24 4
gpt4 key购买 nike

问题

我正在尝试从指令模板中单击带有 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... 的选择 block 是可见的。

最佳答案

除了触发 click 事件外,您还必须更改单选按钮的 checked 属性。这是一个例子 http://jsfiddle.net/k1axxs21/1/

关于javascript - Angular 单元测试 - 单击指令未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33241494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com