Skip to content

Instantly share code, notes, and snippets.

@nshimiye
Last active March 6, 2019 20:15
Show Gist options
  • Save nshimiye/2ac756133f8b1ec193bb90425b042536 to your computer and use it in GitHub Desktop.
Save nshimiye/2ac756133f8b1ec193bb90425b042536 to your computer and use it in GitHub Desktop.
// @WARNING this is incomplete
class YourComponent extends Component {
static contextType = GoogleMapContext;
componentDidMount() {
if (this.context.map) {
this.context.map.addListener('click', (e) => { // <= accessing map
this.context.map.setCenter(e.latLng); // <= accessing map
});
}
}
render() {
return <div>Access to map instance using GoogleMapContext</div>;
}
}
describe('GoogleMapWithLoader', () => {
it('provides map instance to children components', () => {
const props = {
googleMapURL: '',
center: { lat: 0, lng: 0 },
zoom: 1,
height: 100,
width: 100,
};
const wrapper = mount(<GoogleMapWithLoader {...props}><YourComponent /></GoogleMapWithLoader>);
const instance = wrapper.find(YourComponent).instance();
expect(instance.context.map).toBeInstanceOf(google.maps.Map);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment