模拟谷歌地图的addEventListener

问题描述 投票:0回答:1

我有这个代码:

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))
})

我想测试它,但我收到了这个错误:this.map.addListener is not a function。我这样嘲笑:

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

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

任何人都可以帮助模仿这部分吗?

先感谢您

javascript reactjs jasmine jestjs
1个回答
1
投票

我发现了如何模仿这个:

const google = {
  maps: {
    LatLng: jest.fn(),
    Map: function() { return { addListener: jest.fn() } },
    event: { trigger: jest.fn() }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.