Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing stateProvider state if configured in config in angularJS returns null on $state

I have a config like this one:

angular.module('myModule', ['ui.router'])
.config(['$stateProvider', function($stateProvider) {
  $stateProvider
    .state('app.home', {
      abstract: true,
      url: '/home',
      template: '<div>Foo Bar</div>'
    });
}]);

and a unit test using jasmine like this:

'use strict';

describe('Module: myModule', function() {
  var $rootScope, $state;

  beforeEach(module('ui.router'));
  beforeEach(module('myModule'));

  beforeEach(inject(function(_$rootScope_, _$state_) {
    $state = _$state_;
    $rootScope = _$rootScope_;
  }));

  it('must have a route state for home', function(){
    console.log($state.get('app.home')); // this returns null
  });
});

However I could not get the state in the config to show up on the array returned by $state.get()

I also checked and the file containing the config is loaded and it is there. Can anyone say what I am doing wrong? Basically I just want to test if the states I am expecting are existing in the config of "myModule"

like image 629
reikyoushin Avatar asked May 19 '15 03:05

reikyoushin


People also ask

What is $stateprovider in AngularJS UI-router?

The AngularJS UI-Router provides both, a provider called $stateProvider as well as a service called $state. The usage would look like this: // using the $stateProvider!

What is state state in angular?

State corresponds to a "Place" in your Angular application. As this statement does not make sense at the moment, I will clear up this concept in upcoming articles.

What is stateconfig statename in UI-router?

This method has two parameters stateName and stateConfig. stateName is a unique state name. For example: it can be /home, /courses, /students, and so on. This is our current output after using UI-Router.

What is AngularJS UI-router?

In this article, you will learn about AngularJS UI-Router and Configuring States. In this article, we will learn about UI Router, where to get UI-Router module from, and how to include it in an Angular application. The UI-Router is a third-party routing framework for AngularJS.


1 Answers

First, you don't need this

beforeEach(module('ui.router'));

as your module lists it as a dependency already.

My guess is, you aren't bringing in the parent app state so your app.home state will not be created.

Say your app state is defined in myAppModule, simply change your module line to

beforeEach(module('myAppModule', 'myModule'));

If you don't want to include the module with the app state, include a config function before your module that creates a fake parent state. For this, you will need to include ui.router.

beforeEach(module('ui.router', function($stateProvider) {
    $stateProvider.state('app', { abstract: true });
}, 'myModule'));

Alternatively, you could spy on $stateProvider.state and use an expect to ensure it was called with app.home, eg

var $stateProvider;

beforeEach(module('ui.router', function(_$stateProvider_) {
    $stateProvider = _$stateProvider_;
    spyOn($stateProvider, 'state');
}, 'myModule'));

it('must have a route state for home', function() {
    expect($stateProvider.state).toHaveBeenCalledWith('app.home', jasmine.any(Object));
});
like image 181
Phil Avatar answered Sep 20 '22 12:09

Phil