0
我想在使用 $componentController
服務創建控制器後測試我的組件的$onChanges
。但我不知道如何觸發/測試$onChanges
。我試圖更改傳遞到$componentController
的綁定對象,但更改並未糾正。
但是我能夠做同樣的使用好老$compile
& $rootScope
:
describe("test component", function() {
angular.module('test', []).component('component', {
template: '<span>{{$ctrl.text}}</span>',
bindings: {
'text': '@'
},
controller: function() {
this.$onChanges = function(changesObj) {
console.log("onChanges called", changesObj);
}
}
})
var $compile, $rootScope;
beforeEach(module('test'));
beforeEach(inject(function (_$rootScope_, _$compile_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
}));
it('should call onChanges', function() {
var scope = $rootScope.$new()
scope.text = 'original text';
var el = $compile('<component text="{{text}}" />')(scope);
$rootScope.$apply();
console.log(el.find('span').text())
scope.text = 'text has changed';
$rootScope.$apply();
console.log(el.find('span').text())
})
})
我的問題是:與$componentController
測試$onChanges
的時候,我必須手動調用$onChanges
和手動構建changesObj
?
_「我必須手動調用$ onChanges並手動構建changesObj嗎?」_這實際上是一件好事,你可以做到這一點。所以我很困惑你的問題。 – zeroflagL