Ad
Mock AddEventListener For Google Maps
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
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad