Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Singleton neo4j #79

Merged
merged 13 commits into from
Dec 5, 2020
242 changes: 121 additions & 121 deletions __mocks__/leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,162 +9,162 @@ import { L } from 'leaflet';
const LeafletMock = jest.createMockFromModule('leaflet')

class ControlMock extends LeafletMock.Control {
constructor(options) {
super()
this.options = { ...L.Control.prototype.options, ...options }
}

getPosition() {
return this.options.position
}

setPosition(position) {
this.options.position = position
return this
}
constructor(options) {
super()
this.options = { ...L.Control.prototype.options, ...options }
}

const controlMock = (options) => new ControlMock(options)
getPosition() {
return this.options.position
}

class LayersControlMock extends ControlMock {
constructor(baseLayers = [], overlays = [], options) {
super(options)
this._layers = []
setPosition(position) {
this.options.position = position
return this
}
}

baseLayers.forEach((layer, i) => {
this._addLayer(layer, i)
})
overlays.forEach((layer, i) => {
this._addLayer(layer, i, true)
})
}
const controlMock = (options) => new ControlMock(options)

_addLayer(layer, name, overlay) {
this._layers.push({ layer, name, overlay })
}
class LayersControlMock extends ControlMock {
constructor(baseLayers = [], overlays = [], options) {
super(options)
this._layers = []

addBaseLayer(layer, name) {
this._addLayer(layer, name)
return this
}
baseLayers.forEach((layer, i) => {
this._addLayer(layer, i)
})
overlays.forEach((layer, i) => {
this._addLayer(layer, i, true)
})
}

addOverlay(layer, name) {
this._addLayer(layer, name, true)
return this
}
_addLayer(layer, name, overlay) {
this._layers.push({ layer, name, overlay })
}

removeLayer(obj) {
this._layers.splice(this._layers.indexOf(obj), 1)
}
addBaseLayer(layer, name) {
this._addLayer(layer, name)
return this
}

ControlMock.Layers = LayersControlMock
controlMock.layers = (baseLayers, overlays, options) => {
return new LayersControlMock(baseLayers, overlays, options)
addOverlay(layer, name) {
this._addLayer(layer, name, true)
return this
}

class MapMock extends LeafletMock.Map {
constructor(id, options = {}) {
super()
Object.assign(this, L.Evented.prototype)
removeLayer(obj) {
this._layers.splice(this._layers.indexOf(obj), 1)
}
}

this.options = { ...L.Map.prototype.options, ...options }
this._container = id
ControlMock.Layers = LayersControlMock
controlMock.layers = (baseLayers, overlays, options) => {
return new LayersControlMock(baseLayers, overlays, options)
}

if (options.bounds) {
this.fitBounds(options.bounds, options.boundsOptions)
}
class MapMock extends LeafletMock.Map {
constructor(id, options = {}) {
super()
Object.assign(this, L.Evented.prototype)

if (options.maxBounds) {
this.setMaxBounds(options.maxBounds)
}
this.options = { ...L.Map.prototype.options, ...options }
this._container = id

if (options.center && options.zoom !== undefined) {
this.setView(L.latLng(options.center), options.zoom)
}
if (options.bounds) {
this.fitBounds(options.bounds, options.boundsOptions)
}

_limitZoom(zoom) {
const min = this.getMinZoom()
const max = this.getMaxZoom()
return Math.max(min, Math.min(max, zoom))
if (options.maxBounds) {
this.setMaxBounds(options.maxBounds)
}

_resetView(center, zoom) {
this._initialCenter = center
this._zoom = zoom
if (options.center && options.zoom !== undefined) {
this.setView(L.latLng(options.center), options.zoom)
}
}

fitBounds(bounds, options) {
this._bounds = bounds
this._boundsOptions = options
}
_limitZoom(zoom) {
const min = this.getMinZoom()
const max = this.getMaxZoom()
return Math.max(min, Math.min(max, zoom))
}

getBounds() {
return this._bounds
}
_resetView(center, zoom) {
this._initialCenter = center
this._zoom = zoom
}

getCenter() {
return this._initialCenter
}
fitBounds(bounds, options) {
this._bounds = bounds
this._boundsOptions = options
}

getMaxZoom() {
return this.options.maxZoom === undefined ? Infinity : this.options.maxZoom
}
getBounds() {
return this._bounds
}

getMinZoom() {
return this.options.minZoom === undefined ? 0 : this.options.minZoom
}
getCenter() {
return this._initialCenter
}

getZoom() {
return this._zoom
}
getMaxZoom() {
return this.options.maxZoom === undefined ? Infinity : this.options.maxZoom
}

setMaxBounds(bounds) {
bounds = L.latLngBounds(bounds)
this.options.maxBounds = bounds
return this
}
getMinZoom() {
return this.options.minZoom === undefined ? 0 : this.options.minZoom
}

setView(center, zoom) {
zoom = zoom === undefined ? this.getZoom() : zoom
this._resetView(L.latLng(center), this._limitZoom(zoom))
return this
}
getZoom() {
return this._zoom
}

setZoom(zoom) {
return this.setView(this.getCenter(), zoom)
}
setMaxBounds(bounds) {
bounds = L.latLngBounds(bounds)
this.options.maxBounds = bounds
return this
}

class PopupMock extends LeafletMock.Popup {
constructor(options, source) {
super()
Object.assign(this, L.Evented.prototype)
setView(center, zoom) {
zoom = zoom === undefined ? this.getZoom() : zoom
this._resetView(L.latLng(center), this._limitZoom(zoom))
return this
}

this.options = { ...L.Popup.prototype.options, ...options }
this._source = source
}
setZoom(zoom) {
return this.setView(this.getCenter(), zoom)
}
}

getContent() {
return this._content
}
class PopupMock extends LeafletMock.Popup {
constructor(options, source) {
super()
Object.assign(this, L.Evented.prototype)

setContent(content) {
this._content = content
}
this.options = { ...L.Popup.prototype.options, ...options }
this._source = source
}

getContent() {
return this._content
}

module.exports = {
...LeafletMock,
Control: ControlMock,
control: controlMock,
LatLng: L.LatLng,
latLng: L.latLng,
LatLngBounds: L.LatLngBounds,
latLngBounds: L.latLngBounds,
Map: MapMock,
map: (id, options) => new MapMock(id, options),
Popup: PopupMock,
popup: (options, source) => new PopupMock(options, source),
setContent(content) {
this._content = content
}
}

module.exports = {
...LeafletMock,
Control: ControlMock,
control: controlMock,
LatLng: L.LatLng,
latLng: L.latLng,
LatLngBounds: L.LatLngBounds,
latLngBounds: L.latLngBounds,
Map: MapMock,
map: (id, options) => new MapMock(id, options),
Popup: PopupMock,
popup: (options, source) => new PopupMock(options, source),
}
33 changes: 33 additions & 0 deletions __mocks__/neo4jDesktopApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Object.defineProperty(window, 'neo4jDesktopApi', {
writable: true,
value: {
getContext: jest.fn().mockImplementation(() => {
const mockGraph = {
name: 'mock graph',
description: 'mock graph for testing',
status: 'ACTIVE',
connection: {
configuration: {
protocols: {
bolt: {
url: '',
username: '',
password: ''
}
}
}
}
}

const mockProject = {
graphs: [mockGraph]
}

const mockContext = {
projects: [mockProject]
}

return Promise.resolve(mockContext);
})
}
});
55 changes: 18 additions & 37 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
import neo4jService from './services/neo4jService'
import download from "downloadjs";
import { Map } from "./components/Map";
import { Menu } from "./components/Menu";
Expand All @@ -10,21 +9,6 @@ import "./App.css";
export const App = React.memo(() => {

const [layers, setLayers] = React.useState([]);
const [ready, setReady] = React.useState(false);
const driverRef = React.useRef(undefined);

// This blocks render indefinitely if the driver is never resolved.
// A better pattern would be to render anyway without driver,
// only calling getDriver() when a module wants to use the driver.
// Error handle incase driver is not resolved at that point.

// TODO: Remove boot blocker
React.useEffect(() => {
neo4jService.getNeo4jDriver().then(result => {
driverRef.current = result;
setReady(true);
});
});

const addLayer = (layer) => {
setLayers([...layers, layer]);
Expand Down Expand Up @@ -76,26 +60,23 @@ export const App = React.memo(() => {
e.preventDefault();
};

return <React.Suspense fallback={<span>Loading...</span>}>
{ready && (
<div id="wrapper" className="row">
<div id="sidebar" className="col-md-4">
<Menu saveConfigToFile={saveConfigToFile} loadConfigFromFile={loadConfigFromFile} />
<SideBar
driver = {driverRef.current}
layers={layers}
addLayer={addLayer}
updateLayer={updateLayer}
removeLayer={removeLayer}
/>
</div>
<div id="app-maparea" className="col-md-8">
<Map
key="map"
layers={layers}
/>
</div>
return (
<div id="wrapper" className="row">
<div id="sidebar" className="col-md-4">
<Menu saveConfigToFile={saveConfigToFile} loadConfigFromFile={loadConfigFromFile} />
<SideBar
layers={layers}
addLayer={addLayer}
updateLayer={updateLayer}
removeLayer={removeLayer}
/>
</div>
<div id="app-maparea" className="col-md-8">
<Map
key="map"
layers={layers}
/>
</div>
)}
</React.Suspense>
</div>
);
});
Loading