Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine mock window.location.reload(true);

I have a constructor of a js object that looks like this:

function SomeThing(window, document) {
   var self = this;
   self.window = window;
   self.document = document;

   if (!self.window.sessionStorage.getItem('page-reloaded')) {
      self.window.sessionStorage.setItem('page-reloaded', true);
      self.window.location.reload(true); // PROBLEM ON THIS LINE
      return;
   }
}

my mock test looks like this:

beforeEach(function() {
  mockWindowObj = {
    'location': {
        'href': '',
        'search': '?hello=world',
        'pathname': '/some/path',
        'replace': function () {},
        'reload': jasmine.createSpy()
      }
   };

  spyOn(mockWindowObj.location, 'reload').and.callFake(function(){}); ;

  some = new SomeThing(mockWindowObj, mockDocumentObj);
});

When I run a test I get this error:

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
 {
   "message": "Some of your tests did a full page reload!",
   "str": "Some of your tests did a full page reload!"
 }

If I comment out the line window.location.reload(true) all of my test run fine and pass. I'm sort of new to unit tests and I'm not sure how to get around this. Any help would be very much appreciated. Thanks in advance.

like image 477
Johnny Kay Avatar asked Jan 20 '26 23:01

Johnny Kay


1 Answers

Your posted code cannot be what you actually ran. The line containing self.window.sessionStorage.getItem would have to fail since you did not define sessionStorage on your mock.

I guess the SomeThing function is being called with window pointing to the real window object. That explains what you observe.

like image 74
johnjbarton Avatar answered Jan 23 '26 17:01

johnjbarton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!