JavaScript How to fake being offline in Jasmine? how to fake being offline

I have a javascript function which is supposed to behave differently when offline than online as a safeguard. I would like to have a Jasmine unit test which tests the function in both offline and online modes - e.g.,

// offline
describe('When there is no connection to the internet', function() {
  beforeEach(function(){
    spyOn(navigator, 'onLine').and.returnValue(false);
  });

  it('offline behavior happens', function() {
    myFunction();

    expect(something).not.toHaveBeenCalled();
  });
});

// online
describe('When there is a connection to the internet', function() {
  beforeEach(function(){
    spyOn(navigator, 'onLine').and.returnValue(true);
  });

  it('online behavior happens', function() {
    myFunction();

    expect(something).toHaveBeenCalled();
  });
});

However, I am unable to fake the value of navigator.onLine. In my before, I also tried:

navigator = {
  'onLine': false
}

This didn't work either. To be thorough, I tried the same techniques above with window.navigator.onLine and that also didn't work.

Does anyone know how to mock being offline for a Jasmine test?

Answer:1

You have no chance to overwrite some built in properties. Though with Object.defineProperty() you can reconfigure builtin properties.

Jasmine 2.6 and newer

Since Jasmine 2.6 (release notes) there is spyOnProperty().

This is how you would spy navigator.onLine:

beforeEach(function(){
    spyOnProperty(Navigator.prototype, 'onLine').and.returnValue(false);
});

Note that the prototype object Navigator is referenced, not it's instance window.navigator.

Jasmine pre 2.6

Build a facade for browser objects

With pre Jasmine 2.6 you can't spy attributes (or properties) directly.

I'd suggest to create a facade for such browser builtins using getter methods. Then you can mock those methods in your tests to return what you like.

const Browser = (function() {
    return {
        isOnline: function() {
            return navigator.onLine;
        }
    };
})();

In your code:

if (Browser.isOnline()) {
    // ...
}

In your test:

beforeEach(function(){
    spyOn(Browser, 'isOnline').and.returnValue(false);
});

Do what spyOnProperty() does on your own

If you cannot upgrade from a pre 2.6 version for some reason, you could even spy manually by utilizing Object.defineProperty() and Object.getOwnPropertyDescriptor().

var onlineState,
    origOnLineProp;

beforeEach(function() {
    // Remember original config
    origOnLineProp = Object.getOwnPropertyDescriptor(Navigator.prototype, "onLine");

    onlineState = true;

    // New behavior
    Object.defineProperty(Navigator.prototype, "onLine", {
        enumerable: origOnLineProp.enumerable,
        configurable: origOnLineProp.configurable,
        get: function() { return  onlineState }
    });
});

it("...", function() {
    onlineState = false;
    expect("code").toBe("correctly functioning in offline mode");
});

afterEach(function() {
    // Restore original behavior
    Object.defineProperty(Navigator.prototype, "onLine", {
        enumerable: origOnLineProp.enumerable,
        configurable: origOnLineProp.configurable,
        get: origOnLineProp.get
    });
});

You could even implement your own spyOnProperty() backport if you're desperate (that's beyond this answer though).

Answer:2

I am pretty noob at JavaScript RegExp. I just need to verify whether a string is 4 characters long and contains only caps letters (A-Z). Any help, highly appreciated.

I am pretty noob at JavaScript RegExp. I just need to verify whether a string is 4 characters long and contains only caps letters (A-Z). Any help, highly appreciated.

  1. verify string javascript
  2. checking string javascript
  3. verify string is number javascript
  4. javascript verify string is not empty
  5. javascript verify string is numeric
  6. javascript verify string is valid date
  7. javascript verify string is integer
  8. javascript verify string is date
  9. javascript verify string format
  10. verify empty string javascript
  11. verify if is string javascript
  12. javascript verify string contains
  13. verify if string contains javascript
  14. javascript verify string length

I am trying to get a pop-up window to display when a user clicks on an element on my page. I have all the functionality for the pop-up working correctly; however, I am having trouble getting the ...

I am trying to get a pop-up window to display when a user clicks on an element on my page. I have all the functionality for the pop-up working correctly; however, I am having trouble getting the ...

  1. call javascript function from html
  2. call javascript function from php
  3. call javascript function from html button
  4. call javascript function from c#
  5. call javascript function on page load
  6. call javascript function on button click
  7. call javascript function from another file
  8. call javascript function from cshtml
  9. call javascript function from typescript
  10. call javascript function in html body
  11. call javascript function from jquery
  12. call javascript function from python
  13. call javascript function on form submit
  14. call javascript function from console
  15. call javascript function after page load
  16. call javascript function from a href
  17. call javascript function from vb.net
  18. call javascript function from java
  19. call javascript function on image click
  20. call javascript function from jsp

I intend to focus on usage of Directive Controller only. So, pl answer around the same error as mentioned as I suppose for each instance of directive usage, angular would go once in controller section ...

I intend to focus on usage of Directive Controller only. So, pl answer around the same error as mentioned as I suppose for each instance of directive usage, angular would go once in controller section ...

I am trying to develop a form abandonment heat mapper function newMarkup(id, uniqueHits){ var min = Math.min.apply(null,uniqueHits); var max = Math.max.apply(null,uniqueHits); var med = max / 2; for(...

I am trying to develop a form abandonment heat mapper function newMarkup(id, uniqueHits){ var min = Math.min.apply(null,uniqueHits); var max = Math.max.apply(null,uniqueHits); var med = max / 2; for(...

  1. data from google forms
  2. data from google maps
  3. data from google analytics
  4. data from google trends
  5. data from google sheets to bigquery
  6. data from google forms to spss
  7. data from google
  8. data from google searches
  9. data from google account
  10. restore data from google drive
  11. pull data from google spreadsheet to website
  12. restore data from google
  13. restore data from google account
  14. pull data from google spreadsheet to html
  15. import data from google sheets to excel
  16. get data from google sheets
  17. display data from google spreadsheet on website
  18. download data from google
  19. export data from google maps to excel
  20. export data from google analytics