diff --git a/package-lock.json b/package-lock.json index 38948eec..3da2b434 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@bayer/ol-kit", - "version": "0.0.0-2", + "version": "0.0.0-3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index bc6a1e9d..7107a0cd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bayer/ol-kit", - "version": "0.0.0-2", + "version": "0.0.0-3", "license": "BSD", "description": "Mapping components & utils built with openlayers + react", "contributors": [ diff --git a/src/Basemaps/__snapshots__/BasemapManager.test.js.snap b/src/Basemaps/__snapshots__/BasemapManager.test.js.snap index 36a4cd84..be97fff9 100644 --- a/src/Basemaps/__snapshots__/BasemapManager.test.js.snap +++ b/src/Basemaps/__snapshots__/BasemapManager.test.js.snap @@ -3,68 +3,85 @@ exports[` should render a basic basemap manager component 1`] = ` "
  + > +  +  +  +  + 
 +      OpenStreetMap      Stamen Terrain      Stamen Toner Dark      Stamen Toner Lite      Blank White  @@ -76,68 +93,85 @@ exports[` should render a basic basemap manager component 1`] exports[` should render a single child 1`] = ` "
  + > +  +  +  +  + 
 +      OpenStreetMap      Stamen Terrain      Stamen Toner Dark      Stamen Toner Lite      Blank White  @@ -154,68 +188,85 @@ exports[` should render a single child 1`] = ` exports[` should render an array of children 1`] = ` "
  + > +  +  +  +  + 
 +      OpenStreetMap      Stamen Terrain      Stamen Toner Dark      Stamen Toner Lite      Blank White  diff --git a/src/Basemaps/__snapshots__/BingMaps.test.js.snap b/src/Basemaps/__snapshots__/BingMaps.test.js.snap index 33786f78..ded72cd4 100644 --- a/src/Basemaps/__snapshots__/BingMaps.test.js.snap +++ b/src/Basemaps/__snapshots__/BingMaps.test.js.snap @@ -3,17 +3,34 @@ exports[` should render a basic basemap option component 1`] = ` "
  + > +  +  +  +  + 
 +     Bing Maps  @@ -24,17 +41,34 @@ exports[` should render a basic basemap option component 1`] = ` exports[` should require a key 1`] = ` "
  + > +  +  +  +  + 
 +     Bing Maps  diff --git a/src/Basemaps/__snapshots__/BlankWhite.test.js.snap b/src/Basemaps/__snapshots__/BlankWhite.test.js.snap index 1e33ac91..bcb639d7 100644 --- a/src/Basemaps/__snapshots__/BlankWhite.test.js.snap +++ b/src/Basemaps/__snapshots__/BlankWhite.test.js.snap @@ -3,17 +3,34 @@ exports[` should render a basic basemap option component 1`] = ` "
  + > +  +  +  +  + 
 +     Blank White  diff --git a/src/Basemaps/__snapshots__/OpenStreetMap.test.js.snap b/src/Basemaps/__snapshots__/OpenStreetMap.test.js.snap index 19d60c61..954a50b3 100644 --- a/src/Basemaps/__snapshots__/OpenStreetMap.test.js.snap +++ b/src/Basemaps/__snapshots__/OpenStreetMap.test.js.snap @@ -3,17 +3,34 @@ exports[` should render a basic basemap option component 1`] = ` "
  + > +  +  +  +  + 
 +     OpenStreetMap  diff --git a/src/Basemaps/__snapshots__/StamenTerrain.test.js.snap b/src/Basemaps/__snapshots__/StamenTerrain.test.js.snap index 9dd3f48c..d3539c8a 100644 --- a/src/Basemaps/__snapshots__/StamenTerrain.test.js.snap +++ b/src/Basemaps/__snapshots__/StamenTerrain.test.js.snap @@ -3,17 +3,34 @@ exports[` should render a basic basemap option component 1`] = ` "
  + > +  +  +  +  + 
 +     Stamen Terrain  diff --git a/src/Basemaps/__snapshots__/StamenTonerDark.test.js.snap b/src/Basemaps/__snapshots__/StamenTonerDark.test.js.snap index 61b332fc..86003226 100644 --- a/src/Basemaps/__snapshots__/StamenTonerDark.test.js.snap +++ b/src/Basemaps/__snapshots__/StamenTonerDark.test.js.snap @@ -3,17 +3,34 @@ exports[` should render a basic basemap option component 1`] = ` "
  + > +  +  +  +  + 
 +     Stamen Toner Dark  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[` should render a basic basemap option component 1`] = ` "
  + > +  +  +  +  + 
 +     Stamen Toner Lite  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 element is within the rendered html + expect(prettyDOM(testMap.getTargetElement())).toEqual(expect.stringContaining(' {