hekevintran
hekevintran

Reputation: 23722

Mocking Google Maps in RequireJS for running tests offline

I am using the async plugin from https://github.com/millermedeiros/requirejs-plugins to load the Google Maps API:

define(['async!//maps.google.com/maps/api/js?libraries=places&sensor=false'], function () {
    // ...
});

This has the consequence of requiring an Internet connexion when running unit tests. Is there a way to mock or otherwise allow code that loads this to run offline without raising a "Failed to load resource" error?

Upvotes: 1

Views: 865

Answers (1)

hekevintran
hekevintran

Reputation: 23722

My solution is to use an empty module in place of my google_maps module when running QUnit tests.

google_maps.js:

define(['async!//maps.google.com/maps/api/js?libraries=places&sensor=false'], function () {
    return window.google;
});

google_maps_stub.js:

define(function () {
    window.google = {};
    return window.google;
});

requirejs_config_qunit.js:

define(['./requirejs_config_development.js'], function () {
    requirejs.config({
        paths: {
            'google_maps': 'js/lib/google_maps_stub'
        }
    });
});

Upvotes: 1

Related Questions