I have service that uses $state. I am trying to unit test the service. I see that $state.current does not change after call of $state.transitionTo. I checked $stateProvider, it contains states.
Routes config:
angular.module('EmsWeb.Routes', ['ui.compat']).config(['$routeProvider', '$httpProvider', '$stateProvider', '$locationProvider',
function($routeProvider, $httpProvider, $stateProvider, $locationProvider) {
var defaultView = {
name: 'defaultView',
url: '',
template: '<div />'
};
var view = {
name: 'mainView',
url: '/view:nodeId:navParams:reqContext', //added reqContext in order to change state parameters that will trigger new navigation to view
templateUrl: 'MainView.html',
controller: 'MainAreaCtrl'
};
$stateProvider.state(defaultView);
$stateProvider.state(view);
$locationProvider.html5Mode(true);
}]);
Service code:
angular.module('EmsWeb.Services').factory('MetadataNavigation', ['$q', '$state', '$stateParams', 'DalService', 'ResponseUtil', function ($q, $state, $stateParams, dalService, responseUtil) {
return {
hasChangedBroadcastData: function (nodeId, navParams, appContext) {
if (!$state.is('mainView'))
return true;
if ($stateParams.nodeId !=nodeId)
return true;
return false;
}
}]);
My test:
describe("Unit: MetadataNavigation->", function () {
var service,stateProvider;
beforeEach(angular.mock.module('EmsWeb.Routes', function ($stateProvider) {
stateProvider = $stateProvider;
}));
beforeEach(angular.mock.module('EmsWeb.Services'));
beforeEach(inject(function ($state, MetadataNavigation) {
service = MetadataNavigation;
$state.transitionTo('defaultView', {}, false);
//STATE WAS NOT CHANGED
}));
describe("hasChangedBroadcastdata:", function () {
var navParams1, navParams2, nodeId1, nodeId2, appContext1, appContext2, state;
beforeEach(inject(function ( AppModel) {
appContext1 = AppModel.context;
}));
it('broadcastdata has not changed',inject(function ($state) {
nodeId1 = 'nodeId1';
nodeId2 = 'nodeId2';
navParams1 = [{
targetName: 'fundId',
value: 'LPL.2q.xxxx',
display: 'ACLASS'
}, {
targetName: 'fundId',
value: 'LPL.2q.yyyy',
display: 'BCLASS'
}];
navParams2 = [{
targetName: 'fundId',
value: 'LPL.2q.xxxx',
display: 'ACLASS'
}, {
targetName: 'fundId',
value: 'LPL.2q.yyyy',
display: 'BCLASS'
}];
//quick way to copy w/o copying properties
appContext2 = angular.fromJson(angular.toJson(appContext1));
$state.transitionTo('mainView', { 'nodeId': nodeId1, 'navParams': navParams1, 'reqContext': appContext1.toRequestContext() }, false);
//STATE WAS NOT CHANGED
expect(service.hasChangedBroadcastData(nodeId2, navParams2, appContext2)).to.be.false;
}));
});
});
I looked at debugger and $state.current remains the same i.e. $state.current.name is empty and $state.current.abstract is true I tried different ways to no avail.
What am I missing?
Thank you.