4

I'm new to Jasmine unit testing. I have written a function something like below.

//retrieve hash value from the url
        var hash = window.location.hash;
        //if hash exists, create a url and redirect
        if(hash != undefined && hash.length > 0){
            var newHref = hash.replace(/^#hash=/, '');
            window.location = newHref;
    }

Basically, it reads the value from window hash and replaces the value and reloads with the new href. In Jasmine have written a test something like below.

describe("onHashChange()", function() {
                    var href = "http://example.com/contactUs?abc=name";
                    var windowHash = "#hash="+href;
                    var newHref = windowHash.replace(/^#hash=/, '');
                    it('passes if Urls are equal', function() {
                        expect(href).toEqual(newHref);
                    });
                });

Is this the right test and how do I mock window.location.href in jasmine.

Robin Whittleton
  • 6,159
  • 4
  • 40
  • 67
magi
  • 131
  • 1
  • 3
  • 18
  • Possible duplicate of [mocking window.location.href in Javascript](http://stackoverflow.com/questions/4792281/mocking-window-location-href-in-javascript) – Jamie Mason Mar 13 '17 at 13:10

0 Answers0