angularjs - Jasmine (via TypeScript) test to verify scope $on is called -
i have search around few places ideas on this.
how unit test $scope.broadcast, $scope.$on using jasmine
$scope.$on not working in jasmine specrunner
it has helped think seems still missing something. trying test controller has $rootscope.$on in code. in unit test trying trigger $broadcast $on gets , code runs. current code not working.
here controller:
constructor($state: ng.ui.istateservice, store: angular.a0.storage.istoreservice, jwthelper: angular.jwt.ijwthelper, $rootscope: any) { this.currentdate = new date(); this.state = $state; this.store = store; this.jwthelper = jwthelper; this.userfullname = ''; $rootscope.$on('$statechangestart', (event: any, tostate: any, toparams: any, fromstate: any, fromparams: any, error: any) => { var jwttoken = this.store.get('token'); if (jwttoken != null) { var decodedtoken: = this.jwthelper.decodetoken(jwttoken); } }); }
here test:
beforeeach(angular.mock.inject(($compile: ng.icompileservice, $rootscope: any, $controller: any, $state: ng.ui.istateservice, jwthelper: angular.jwt.ijwthelper) => { controllerscope = $rootscope.$new(); navbarcontroller = $controller('navbarcontroller', { $scope: controllerscope }); currentdate = new date(); rootscope = $rootscope; state = $state; jwt = jwthelper; } it('should broadcast user token', () => { spyon(controllerscope, '$on'); spyon(jwt, 'decodetoken'); //state.go('home'); trying different way trigger event rootscope.$broadcast('$statechangestart', [{ tostate: 'home' }]); expect(controllerscope.$on).tohavebeencalled(); expect(jwt.decodetoken).tohavebeencalled(); });
both spies never called. have missed aligned?
instead of testing whether or not event listener fired, you're verifying whether code registers listener fired.
it fired, controllerscope.$on
got invoked in beforeeach
function, not in it
. spyon
can't reach past detect prior function calls; if it, that's not test you're trying do.
basically you've done this:
window.addeventlistener('mousemove', somefunctionexpr); spyon(window, 'addeventlistener'); triggermousemoveevent(); expect(window.addeventlistener).tohavebeencalled();
i can't speak decodetoken
/ jwt
-- code doesn't reference anywhere don't know expect called.
Comments
Post a Comment