I have an application which relies on Google Maps, and I am writing unit tests using Jasmine and Karma. I have a fake Google.maps.Map instance which I use to test my code:
myFunction
var myFunction(map) {
var foo = false;
var projection = map.getProjection();
// ... do dome stuff
return foo;
}
tests
describe('myFunction() {
var fakeMap = function() {
var map;
// create fake DOM element for map
mapCanvas = document.createElement("div");
mapCanvas.setAttribute("id", 'map-canvas');
// map instance
map = new google.maps.Map(mapCanvas);
return map
}
it("should test my function", function () {
var map = fakeMap();
foo = myfunction(map);
expect(foo).toBe(true);
})
});
However, this fails as in myFuntion
, projection
is undefined
.
I read that the google maps instance needs to be idle before getProjection
will return correctly, so I tried this:
it("should test my function", function () {
var map = fakeMap();
google.maps.event.addListener(map, 'idle', function() {
foo = myfunction(map);
expect(foo).toBe(true);
});
});
EDIT and this:
it("should test my function", function () {
var map = fakeMap();
google.maps.event.addListener(map, 'projection_changed', function() {
foo = myfunction(map);
expect(foo).toBe(true);
});
});
But in this case the test is never executed, presumably because the map never reaches idle
or projection_changed
state.
EDIT
I also tried to trigger the event manually:
it("should test my function", function () {
var map = fakeMap();
google.maps.event.addListener(map, 'projection_changed', function() {
foo = myfunction(map);
expect(foo).toBe(true);
});
google.maps.event.trigger(map, 'projection_changed');
});
How can I test with a Map
instance which will return all it's methods as expected in the functioning application?