Search code examples
javascriptgeolocation

Disable geolocation browser using javascript


I'm writing an integration test using QUnit, and in my webapp if geolocation is not available, (I use if (navigator.geolocation) to detect if is present or not, but I want to disable it in order to implement the test when geolocation is not available.

I tried navigator.geolocation = undefined but unfortunately it didn't work.


Solution

  • You can't really "disable" navigator.geolocation since it's a read-only property.

    Instead, you could create a wrapper that checks for certain functionality and then mock it up for your tests.

    var supports = {
      geolocation: !!navigator.geolocation // will be `true` if geolocation is defined
    };
    
    // In your tests...
    supports.geolocation = false;
    executeTest();