1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Darwin Tech
  • 18,449
  • 38
  • 112
  • 187
  • You wrote: `I read that the google maps instance needs to be idle before getProjection will return correctly`. The getProjection method will be available after the `projection_changed` event. – geocodezip Jan 11 '15 at 17:48
  • OK. So should I use `google.maps.event.addListener(map, 'projection_changed', function() {})` instead of `idle`? Do I need to trigger that event manually? – Darwin Tech Jan 11 '15 at 19:18
  • I tried with `google.maps.event.addListener(map, 'projection_changed', function() {})`, and used `google.maps.event.trigger(map, 'projection_changed');` but no joy :( – Darwin Tech Jan 11 '15 at 21:55
  • What is the trigger 'projection_changed' for? – geocodezip Jan 11 '15 at 23:11
  • Because using just `google.maps.event.addListener(map, 'projection_changed', function() { # ..execute test})`, the test is never executed, so I assume that `projection_changed` never happens. So I try to trigger it manually, but still nothing. – Darwin Tech Jan 12 '15 at 17:15
  • Should I perhaps be treating this as an async call, and use Jasmine's `done()` function? – Darwin Tech Jan 13 '15 at 17:56

0 Answers0