我也有同樣的挫敗感。我在ui-select spec中發現執行on-select
函數需要在點擊其中一個選項後調用$timeout.flush()
。
像這樣的東西(不要忘記注入$超時):
compiledUiSelectEl.find('.ui-select-choices-row-inner')[0].click();
scope.$digest();
$timeout.flush();
而且包括ui-select spec
function compileTemplate(template) {
var el = $compile(angular.element(template))(scope);
scope.$digest();
return el;
}
function clickItem(el, text) {
if (!isDropdownOpened(el)) {
openDropdown(el);
}
$(el).find('.ui-select-choices-row div:contains("' + text + '")').click();
scope.$digest();
}
it('should invoke select callback on select', function() {
scope.onSelectFn = function ($item, $model, $label) {
scope.$item = $item;
scope.$model = $model;
};
var el = compileTemplate(
'<ui-select on-select="onSelectFn($item, $model)" ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person.name as person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);
expect(scope.$item).toBeFalsy();
expect(scope.$model).toBeFalsy();
clickItem(el, 'Samantha');
$timeout.flush();
expect(scope.selection.selected).toBe('Samantha');
expect(scope.$item).toEqual(scope.people[5]);
expect(scope.$model).toEqual('Samantha');
});
測試的相關部分,我已經做到這一點。但是我想知道on-select接線是否正確。 – rogergl