|
| 1 | +/* global jest, module, require */ |
| 2 | + |
| 3 | +import assign from 'lodash/assign' |
| 4 | + |
| 5 | +const L = require.requireActual('leaflet') |
| 6 | +const LeafletMock = jest.genMockFromModule('leaflet') |
| 7 | + |
| 8 | +class ControlMock extends LeafletMock.Control { |
| 9 | + constructor (options) { |
| 10 | + super() |
| 11 | + this.options = {...L.Control.prototype.options, ...options} |
| 12 | + } |
| 13 | + |
| 14 | + getPosition () { |
| 15 | + return this.options.position |
| 16 | + } |
| 17 | + |
| 18 | + setPosition (position) { |
| 19 | + this.options.position = position |
| 20 | + return this |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +const controlMock = options => new ControlMock(options) |
| 25 | + |
| 26 | +class LayersControlMock extends ControlMock { |
| 27 | + constructor (baseLayers = [], overlays = [], options) { |
| 28 | + super(options) |
| 29 | + this._layers = [] |
| 30 | + |
| 31 | + baseLayers.forEach((layer, i) => { |
| 32 | + this._addLayer(layer, i) |
| 33 | + }) |
| 34 | + overlays.forEach((layer, i) => { |
| 35 | + this._addLayer(layer, i, true) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + _addLayer (layer, name, overlay) { |
| 40 | + this._layers.push({layer, name, overlay}) |
| 41 | + } |
| 42 | + |
| 43 | + addBaseLayer (layer, name) { |
| 44 | + this._addLayer(layer, name) |
| 45 | + return this |
| 46 | + } |
| 47 | + |
| 48 | + addOverlay (layer, name) { |
| 49 | + this._addLayer(layer, name, true) |
| 50 | + return this |
| 51 | + } |
| 52 | + |
| 53 | + removeLayer (obj) { |
| 54 | + this._layers.splice(this._layers.indexOf(obj), 1) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +ControlMock.Layers = LayersControlMock |
| 59 | +controlMock.layers = (baseLayers, overlays, options) => { |
| 60 | + return new LayersControlMock(baseLayers, overlays, options) |
| 61 | +} |
| 62 | + |
| 63 | +class MapMock extends LeafletMock.Map { |
| 64 | + constructor (id, options = {}) { |
| 65 | + super() |
| 66 | + assign(this, L.Mixin.Events) |
| 67 | + |
| 68 | + this.options = {...L.Map.prototype.options, ...options} |
| 69 | + this._container = id |
| 70 | + |
| 71 | + if (options.bounds) { |
| 72 | + this.fitBounds(options.bounds, options.boundsOptions) |
| 73 | + } |
| 74 | + |
| 75 | + if (options.maxBounds) { |
| 76 | + this.setMaxBounds(options.maxBounds) |
| 77 | + } |
| 78 | + |
| 79 | + if (options.center && options.zoom !== undefined) { |
| 80 | + this.setView(L.latLng(options.center), options.zoom) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + _limitZoom (zoom) { |
| 85 | + const min = this.getMinZoom() |
| 86 | + const max = this.getMaxZoom() |
| 87 | + return Math.max(min, Math.min(max, zoom)) |
| 88 | + } |
| 89 | + |
| 90 | + _resetView (center, zoom) { |
| 91 | + this._initialCenter = center |
| 92 | + this._zoom = zoom |
| 93 | + } |
| 94 | + |
| 95 | + fitBounds (bounds, options) { |
| 96 | + this._bounds = bounds |
| 97 | + this._boundsOptions = options |
| 98 | + } |
| 99 | + |
| 100 | + getBounds () { |
| 101 | + return this._bounds |
| 102 | + } |
| 103 | + |
| 104 | + getCenter () { |
| 105 | + return this._initialCenter |
| 106 | + } |
| 107 | + |
| 108 | + getMaxZoom () { |
| 109 | + return this.options.maxZoom === undefined ? Infinity : this.options.maxZoom |
| 110 | + } |
| 111 | + |
| 112 | + getMinZoom () { |
| 113 | + return this.options.minZoom === undefined ? 0 : this.options.minZoom |
| 114 | + } |
| 115 | + |
| 116 | + getZoom () { |
| 117 | + return this._zoom |
| 118 | + } |
| 119 | + |
| 120 | + setMaxBounds (bounds) { |
| 121 | + bounds = L.latLngBounds(bounds) |
| 122 | + this.options.maxBounds = bounds |
| 123 | + return this |
| 124 | + } |
| 125 | + |
| 126 | + setView (center, zoom) { |
| 127 | + zoom = zoom === undefined ? this.getZoom() : zoom |
| 128 | + this._resetView(L.latLng(center), this._limitZoom(zoom)) |
| 129 | + return this |
| 130 | + } |
| 131 | + |
| 132 | + setZoom (zoom) { |
| 133 | + return this.setView(this.getCenter(), zoom) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +class PopupMock extends LeafletMock.Popup { |
| 138 | + constructor (options, source) { |
| 139 | + super() |
| 140 | + assign(this, L.Mixin.Events) |
| 141 | + |
| 142 | + this.options = {...L.Popup.prototype.options, ...options} |
| 143 | + this._source = source |
| 144 | + } |
| 145 | + |
| 146 | + getContent () { |
| 147 | + return this._content |
| 148 | + } |
| 149 | + |
| 150 | + setContent (content) { |
| 151 | + this._content = content |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +module.exports = { |
| 156 | + ...LeafletMock, |
| 157 | + Control: ControlMock, |
| 158 | + control: controlMock, |
| 159 | + LatLng: L.LatLng, |
| 160 | + latLng: L.latLng, |
| 161 | + LatLngBounds: L.LatLngBounds, |
| 162 | + latLngBounds: L.latLngBounds, |
| 163 | + geoJson (data, props) { |
| 164 | + return { |
| 165 | + data, |
| 166 | + getLayers () { |
| 167 | + return [] |
| 168 | + }, |
| 169 | + props |
| 170 | + } |
| 171 | + }, |
| 172 | + Map: MapMock, |
| 173 | + map: (id, options) => new MapMock(id, options), |
| 174 | + Popup: PopupMock, |
| 175 | + popup: (options, source) => new PopupMock(options, source) |
| 176 | +} |
0 commit comments