0
$componentController
서비스로 컨트롤러를 만든 후 구성 요소의 $onChanges
을 테스트하고 싶습니다. 그러나 나는 트리거/테스트 $onChanges
하는 방법을 몰랐다. $componentController
에 전달 된 바인딩 개체를 변경하려고했지만 변경 사항이 나타납니다.
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
을 구축해야 할
$compile
&
$rootScope
의 사용과 동일한 기능을 수행 할 수 있었다 ?
_ "수동으로 $ onChanges를 호출하고 changesObj를 수동으로 빌드해야합니까?"_ 실제로 그렇게 할 수있는 좋은 일입니다. 그래서 나는 당신의 질문에 혼란스러워합니다. – zeroflagL