Ad

Mock AddEventListener For Google Maps

- 1 answer

I've this code:

const eventNames = ['ready', 'click']
const node = ReactDOM.findDOMNode(mapRef)
const mapConfig = { streetViewControl: false }
this.map = new maps.Map(node, mapConfig)

eventNames.forEach(e => {
  this.listeners[e] = this.map.addListener(e, this.handleEvent(e))
})

And I want to test it but I'm receiving this error: this.map.addListener is not a function. I'm mocking like this:

window.addEventListener = jest.fn()
const google = {
  maps: {
    LatLng: jest.fn(),
    Map: jest.fn(),
    event: jest.fn()
  }
}

const wrapper = shallow(
  <Map google={google}>
    <Component />
  </Map>
)

Can any one help to mock this part?

Thank you in advance

Ad

Answer

I found out the how to mock this:

const google = {
  maps: {
    LatLng: jest.fn(),
    Map: function() { return { addListener: jest.fn() } },
    event: { trigger: jest.fn() }
  }
}
Ad
source: stackoverflow.com
Ad