[39m
diff --git a/src/Basemaps/__snapshots__/StamenTonerLite.test.js.snap b/src/Basemaps/__snapshots__/StamenTonerLite.test.js.snap
index 05497de9..bcbd60b1 100644
--- a/src/Basemaps/__snapshots__/StamenTonerLite.test.js.snap
+++ b/src/Basemaps/__snapshots__/StamenTonerLite.test.js.snap
@@ -3,17 +3,34 @@
exports[`
[39m
[36m
[39m
[36m
[39m
diff --git a/src/Map/Map.js b/src/Map/Map.js
index d0be3baf..8c178bc0 100644
--- a/src/Map/Map.js
+++ b/src/Map/Map.js
@@ -27,15 +27,12 @@ class Map extends React.Component {
// this is used to create a unique identifier for the map div
this.target = `_ol_kit_map_${nanoid(6)}`
- // if no map was passed, create the map
- this.map = !props.map ? createMap({ target: this.target }) : props.map
-
// create a map context
MapContext = React.createContext()
}
componentDidMount () {
- const { onMapInit, updateUrlDebounce, updateUrlFromView, updateViewFromUrl, urlViewParam } = this.props
+ const { map: passedMap, onMapInit, updateUrlDebounce, updateUrlFromView, updateViewFromUrl, urlViewParam } = this.props
const onMapReady = map => {
// pass map back via callback prop
const initCallback = onMapInit(map)
@@ -48,6 +45,9 @@ class Map extends React.Component {
: this.setState({ mapInitialized: true })
}
+ // if no map was passed, create the map
+ this.map = !passedMap ? createMap({ target: this.target }) : passedMap
+
// optionally attach map listener
if (updateUrlFromView) {
const setUrl = () => updateUrlFromMap(this.map, urlViewParam)
diff --git a/src/Map/Map.test.js b/src/Map/Map.test.js
index 5eaec838..e552f651 100644
--- a/src/Map/Map.test.js
+++ b/src/Map/Map.test.js
@@ -1,5 +1,7 @@
import React from 'react'
import { mount } from 'enzyme'
+import { render, waitFor } from '@testing-library/react'
+import { prettyDOM } from '@testing-library/dom'
import olMap from 'ol/map'
import olView from 'ol/view'
import Map from './Map'
@@ -25,10 +27,13 @@ describe('
', () => {
mount(
)
})
- it('should render with onMapInit callback', (done) => {
+ it('should render with onMapInit callback', async () => {
// set the url with a view param
window.history.replaceState(null, '', `${window.location.pathname}?view=49.618551,-97.280674,8.00,0.91`)
+ let testMap = null
const onMapInit = map => {
+ // hoist map to closure for later expect
+ testMap = map
// returned map should be an openlayers map
expect(map).toBeInstanceOf(olMap)
// do not respect the view location from the url since updateViewFromUrl={false}
@@ -36,11 +41,16 @@ describe('
', () => {
expect(map.getView().getZoom()).not.toBe(8)
// round off crazy long decimal
expect(Number(map.getView().getRotation().toFixed(2))).not.toEqual(0.91)
- done()
}
// using updateViewFromUrl={false} checks a separate if block inside Map's componentDidMount
- mount(
)
+ const { container } = render(
)
+
+ // wait for async child render
+ await waitFor(() => {}, { container })
+
+ // make sure a