angularjs - How to trigger $onInit or $onChanges implictly in unit testing Angular component controller? -
i'm using angular 1.5.5 , jasmine test framework. have test passes:
function createcontroller(bindings) { return $componentcontroller('mycontroller', null, bindings); } beforeeach(inject(function (_$componentcontroller_) { $componentcontroller = _$componentcontroller_; })); describe('on pages updated', function () { beforeeach(function () { controller = createcontroller({prop1: 0, prop2: 0}); controller.$oninit(); // see have explitcitly call $oninit function }); it('should update isselected , currentpage', function () { expect(controller.prop1).tobe(0); expect(controller.prop2).tobe(0); controller.prop1= 1; controller.prop2= 2; controller.$onchanges(controller); // , $onchanges here expect(controller.prop1).tobe(1); expect(controller.prop2).tobe(2); }); });
there issue in github regarding this: https://github.com/angular/angular.js/issues/14129
basically working intended, not calling $oninit
or $onchanges
automatically.
it makes no sense (or low sense) execute $oninit, explain it: $componentcontroller instance controllers kind of replacement $controller, instead of creating instances of controllers registered controllerprovider creates instances of controllers registered through directives (the ones satisfies component definition). so, once have instance of controller, can call manually $oninit, , lifecycle of controller, idea testing controller, not directive (and relationships).
Comments
Post a Comment