JavaScript Mocking $state change in AngularJS controller

Main module being injected with everything;

require('./dashboard');
module.exports = angular.module('college', ['college.dashboard'])

    .config(function ($stateProvider) {
        $stateProvider
            .state('college.list', {
                url: '/college',
                templateUrl: '/dashboard/dashboard.html',
                controller: 'DashboardCtrl',
                authenticate: true
            });
    })
    .factory('ProjectFactory', require('./services/college.service'));        

College Index, which makes the dashboard controller available;

module.exports = angular.module('college.dashboard', 
  [])
    .controller('DashboardCtrl', require('./dashboard.controller.js'));

The college controller exposes the following method;

    module.exports = function($scope, $rootScope, $state) {

         $scope.openCollege = function(id) {       
            $rootScope.currentCollege = id;
            $state.go('college.main', {currentCollege: id});
        };
   };

The following error is thrown when the unit test calls

scope.openCollege (2);

Error:

 Error: Could not resolve 'college.main' from state ''

Creating state;

beforeEach(inject(function ($rootScope, $state, $location, $controller) {
        scope = $rootScope.$new();
        location = $location;
        rootScope = $rootScope;
        $rootScope.currentCollege = {};// Empty by default
        state = $state;            

        $controller('DashboardCtrl', {
            $scope: scope,
            $state: state,
            $location: location
        });

    }));

Some of the spec test code;

 expect(state.current.name).to.equal('');
 scope.openCollege(2);

I need to figure out how to handle/mock $state.go during the Karma unit testing so that state knows about college.main.

Any help is appreciated.

J

Answer:1

Here is how I got it working;

I added the following to the spec test;

// Globally defined
var stateSpy;

// within the beforeEach
stateSpy = sinon.stub($state, 'go');

// In the unit test
scope.openCollege (2);
assert(stateSpy.withArgs('college.main', '{currentCollege: 2}').calledOnce);

Note: the $state was not passed to the the controller.

I now have green tests!

Thanks for your help, gave me the idea of how to make this work.

J

Answer:2

I have the same config blocks for all the angular apps on my site, all in different files. app_1.config([ "$httpProvider", function($httpProvider) { $httpProvider.defaults.headers.common['X-...

I have the same config blocks for all the angular apps on my site, all in different files. app_1.config([ "$httpProvider", function($httpProvider) { $httpProvider.defaults.headers.common['X-...

  1. share same config file

I want to generate a new unique 20 digits string from a string that represent a number For example : var uid = key.pseudoHash("00000000000000000000"), // "45021-78054-45021-16875" uid = key....

I want to generate a new unique 20 digits string from a string that represent a number For example : var uid = key.pseudoHash("00000000000000000000"), // "45021-78054-45021-16875" uid = key....

I'm developing an extension for Google Chrome and the problem I'm having is I need to be able to call a JavaScript function that belongs to the webpage that's opened in the tab. For details, the ...

I'm developing an extension for Google Chrome and the problem I'm having is I need to be able to call a JavaScript function that belongs to the webpage that's opened in the tab. For details, the ...

  1. chrome.tabs.create
  2. chrome.tabs.create example
  3. chrome.tabs.create callback
  4. chrome.tabs.create not working
  5. chrome tabs create content script
  6. chrome.tabs.create undefined
  7. chrome.tabs.create callback not working
  8. chrome.tabs.create incognito
  9. chrome.tabs.create new window
  10. chrome.tabs.create background
  11. chrome.tabs.create executescript
  12. chrome.tabs.create send message
  13. chrome.tabs.create popup
  14. chrome.tabs.create post
  15. chrome.tabs.create parameters
  16. chrome tabs create callback example
  17. chrome.tabs.create( url
  18. chrome.tabs.create( url new url )
  19. chrome.tabs.create firefox
  20. chrome tabs create function

There was an error in my code and there was also a js file included inside my page which prevented anything from executing inside $(document).ready(function () { ... i'm trying to sumbit this login ...

There was an error in my code and there was also a js file included inside my page which prevented anything from executing inside $(document).ready(function () { ... i'm trying to sumbit this login ...

  1. ajax post form data
  2. ajax post form example
  3. ajax post form data to controller
  4. ajax post form urlencoded
  5. ajax post form serialize
  6. ajax post form with file
  7. ajax post form php
  8. ajax post form javascript
  9. ajax post form laravel
  10. ajax post form as json
  11. ajax post form data to php
  12. ajax post form data javascript
  13. ajax post form data jquery
  14. ajax post form serialize json