Wondering Coder
Wondering Coder

Reputation: 1702

Unable to set a localStorage/cookie in unit testing

Good afternoon to you all,

Just want to ask how to properly set the value of localStorage/cookie in unit testing. I have this code below where I set a cookie then tried to get the value of that cookie but it always disply null.

This snippet of code is the one that I'm trying to test:

scope.$on("$locationChangeSuccess", function(event, next, current) 
    {
        if(location.path() === '/home') {
            if(!Utils.isBlank(session.get('token'))) {
                    var usertype = session.get('usertype');
                    // console.log('landed home');
                    if(usertype === 'player') location.path('/user/dashboard');
                    if(usertype === 'broadcaster') location.path('/broadcaster/dashboard');
                    if(usertype === 'quizmaster') location.path('/quizmaster/dashboard');         
            }
        }   
    });

My controllerSpec.js

describe('MainCtrl', function() {

var scope, api, security, clearAll, location, redirect, session, utility;
beforeEach(inject(function($rootScope, $controller ,$location, Api, Security, Utils, localStorageService){
    scope = $rootScope.$new();
    location = $location;
    session = localStorageService
    utility = Utils;

    $controller("MainCtrl", {
        $scope : scope,
        localStorageService : session,
        Api : Api,
        $location : location,
        Utility : utility
    });
}));

it('should expect player to be redirected to /user/dashboard', function() {
  //set the location to home
  var home = spyOn(location, 'path');
  var addSession = spyOn(session, 'add');
  var token = 'testToken';

  location.path('/home');

  scope.$on('$locationChangeSuccess', {})
  expect(home).toHaveBeenCalledWith('/home');

  //mock a session
  session.add('token',token);
  expect(addSession).toHaveBeenCalled();
  expect(session.get('token')).toEqual('testToken');
});

Error:

Chrome 24.0 (Linux) controllers MainCtrl MainCtrl should expect player to be redirected to /user/dashboard FAILED
Expected null to equal 'testToken'.

Even though I already set the token "session.add('token', token)" it still showing token is null. I added a spyOn to check if the session.add method was called and it did. Please help.

Upvotes: 1

Views: 1110

Answers (1)

jjperezaguinaga
jjperezaguinaga

Reputation: 2482

You mocked the method add in the service. If you want to call it while spying it, you need to use andCallThrough()

var addSession = spyOn(session, 'add').andCallThrough();

This might not be obvious if you are new to Jasmine. There was an issue (couldn't find it, sorry) where people complained that this should be the default functionality of spyOn. IMHO, it's good the way it is, as you are supposed to only do Unit Tests, not expect your controller do a full integration test (i.e remove the session.get expect, you are not testing the session to work, that has to be in the library test).

Update Answering your comment, to test the url based on a token stored in local storage just do something like this:

spyOn(session, 'get').andReturn(token); //Remember, you are not testing the service, you assume it works.

Depending on what the value of token, you can do expect(location.path()).toBe('/registeredUserOnly')

Upvotes: 2

Related Questions