Skip to content

Commit

Permalink
Merge pull request wegue-oss#182 from justb4/180-fix-unit-tests
Browse files Browse the repository at this point in the history
wegue-oss#180 Fixed failing Unit Tests
  • Loading branch information
chrismayer authored Mar 18, 2021
2 parents 604a8c2 + 70e5d7f commit 43b825c
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 85 deletions.
11 changes: 6 additions & 5 deletions src/components/helpwin/HelpWin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
icon: {type: String, required: false, default: 'help'}
},
data () {
let config = this.$appConfig.modules['wgu-helpwin'] || {};
return {
show: false,
windowTitle: this.$appConfig.modules['wgu-helpwin'].windowTitle,
textTitle: this.$appConfig.modules['wgu-helpwin'].textTitle,
htmlContent: this.$appConfig.modules['wgu-helpwin'].htmlContent,
infoLinkUrl: this.$appConfig.modules['wgu-helpwin'].infoLinkUrl,
infoLinkText: this.$appConfig.modules['wgu-helpwin'].infoLinkText
windowTitle: config.windowTitle || 'About',
textTitle: config.textTitle || 'About Wegue',
htmlContent: config.htmlContent || '<h3>WebGIS with OpenLayers and Vue.js</h3>',
infoLinkUrl: config.infoLinkUrl || 'https://github.com/meggsimum/wegue',
infoLinkText: config.infoLinkText || 'More info'
}
},
methods: {
Expand Down
12 changes: 6 additions & 6 deletions src/components/helpwin/ToggleButton.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

<template>

<v-dialog v-model="show" max-width="300" :hide-overlay="false">
<template v-slot:activator="{ on }">
<v-btn icon dark="dark" v-on="on" >
<v-icon medium>{{icon}}</v-icon></v-btn>
{{text}}
<v-dialog v-model="show" max-width="300" :hide-overlay="false">
<template v-slot:activator="{ on }">
<v-btn icon dark="dark" v-on="on" >
<v-icon medium>{{icon}}</v-icon>
{{text}}
</v-btn>
</template>

<wgu-helpwin
Expand Down
47 changes: 36 additions & 11 deletions src/components/measuretool/MeasureWin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,30 @@
top: this.initPos ? this.initPos.top + 'px' : '0'
}
},
destroy () {
if (this.olMapCtrl) {
this.olMapCtrl.destroy();
this.olMapCtrl = undefined;
}
},
watch: {
show () {
var me = this;
if (me.show === true) {
me.olMapCtrl.addInteraction(me.measureType, me.onMeasureVertexSet);
if (!this.olMapCtrl) {
return;
}
if (this.show === true) {
this.olMapCtrl.addInteraction(this.measureType, this.onMeasureVertexSet);
} else {
me.olMapCtrl.removeInteraction();
this.olMapCtrl.removeInteraction();
}
},
measureType () {
var me = this;
if (!this.olMapCtrl) {
return;
}
// reset old geom
me.measureGeom = {};
me.olMapCtrl.addInteraction(me.measureType, me.onMeasureVertexSet);
this.measureGeom = {};
this.olMapCtrl.addInteraction(this.measureType, this.onMeasureVertexSet);
}
},
methods: {
Expand All @@ -93,11 +103,26 @@
* This function is executed, after the map is bound (see mixins/Mapable)
*/
onMapBound () {
const me = this;
const measureConf = me.$appConfig.modules[me.moduleName] || {};
this.olMapCtrl = new OlMeasureController(me.map, measureConf);
if (this.unbound) {
return;
}
// Only create if specified in config
if (!this.$appConfig.modules || !this.$appConfig.modules[this.moduleName]) {
return;
}
const measureConf = this.$appConfig.modules[this.moduleName] || {};
this.olMapCtrl = new OlMeasureController(this.map, measureConf);
me.olMapCtrl.createMeasureLayer();
this.olMapCtrl.createMeasureLayer();
},
/**
* This function is executed, after the map is bound (see mixins/Mapable)
*/
onMapUnbound () {
if (this.olMapCtrl) {
this.olMapCtrl.destroy();
this.olMapCtrl = undefined;
}
},
/**
* Callback function executed when user sets a measure point on the map.
Expand Down
27 changes: 20 additions & 7 deletions src/components/measuretool/OlMeasureController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ export default class OlMeasureController {
constructor (olMap, measureConf) {
this.map = olMap;
this.measureConf = measureConf || {};
this.measureLayer = undefined;
}

/**
* Tears down this controller.
*/
destroy () {
if (!this.measureLayer || !this.map) {
return;
}
this.removeInteraction();
this.map.removeLayer(this.measureLayer);
this.measureLayer = undefined;
}

/**
Expand All @@ -29,7 +42,7 @@ export default class OlMeasureController {
const measureConf = me.measureConf;
// create a vector layer to
var source = new VectorSource();
var vector = new VectorLayer({
this.measureLayer = new VectorLayer({
name: 'Measure Layer',
displayInLayerList: false,
source: source,
Expand All @@ -44,7 +57,7 @@ export default class OlMeasureController {
})
});

me.map.addLayer(vector);
me.map.addLayer(this.measureLayer);

// make vector source available as member
me.source = source;
Expand Down Expand Up @@ -116,12 +129,12 @@ export default class OlMeasureController {
* Removes the current interaction and clears the values.
*/
removeInteraction () {
var me = this;
if (me.draw) {
me.map.removeInteraction(me.draw);
if (this.draw) {
this.map.removeInteraction(this.draw);
this.draw = undefined;
}
if (me.source) {
me.source.clear();
if (this.source) {
this.source.clear();
}
}
}
8 changes: 8 additions & 0 deletions src/components/ol/Map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ export default {
me.setupMapHover();
}, 200);
},
destroyed () {
if (this.permalinkController) {
this.permalinkController.tearDown();
this.permalinkController = undefined;
}
// Send the event 'ol-map-unmounted' with the OL map as payload
WguEventBus.$emit('ol-map-unmounted', this.map);
},
created () {
// make map rotateable according to property
const interactions = defaultInteractions({
Expand Down
16 changes: 15 additions & 1 deletion src/components/ol/PermalinkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class PermalinkController {
// restore the view state when navigating through the history (browser back/forward buttons), see
// https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
window.addEventListener('popstate', (event) => {
if (event.state === null) {
if (event.state === null || this.map === null) {
return;
}

Expand Down Expand Up @@ -85,10 +85,21 @@ export default class PermalinkController {
});
}

/**
* Stop this instance.
*/
tearDown () {
this.unsubscribeLayers();
this.map = null;
}

/**
* Subscribe to Layer visibility changes.
*/
subscribeLayers () {
if (!this.map) {
return;
}
// First unsubscribe from all
this.unsubscribeLayers();

Expand All @@ -105,6 +116,9 @@ export default class PermalinkController {
* Unsubscribe to Layer visibility changes.
*/
unsubscribeLayers () {
if (!this.map) {
return;
}
// Listen to each Layer's visibility changes.
this.layerListeners.forEach((item) => {
item.layer.un(item.key.type, item.key.listener)
Expand Down
9 changes: 9 additions & 0 deletions src/mixins/Mapable.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ export const Mapable = {

if (this.onMapBound) {
this.onMapBound();
this.unbound = false;
}
});
} else {
// OL map is already mounted --> directly apply as member
this.map = this.$map;
if (this.onMapBound) {
this.onMapBound();
this.unbound = false;
}
}
WguEventBus.$on('ol-map-unmounted', () => {
// Make the OL map unaccessible in this component
if (this.onMapUnbound) {
this.onMapUnbound();
this.unbound = true;
}
});
}
};
2 changes: 0 additions & 2 deletions src/util/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const LayerUtil = {
*/
getLayersBy (key, value, olMap) {
if (!olMap) {
console.warn('No OL map passed to LayerUtil.getLayersBy - ' +
'no layer detection possible!');
return [];
}

Expand Down
1 change: 0 additions & 1 deletion test/unit/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Vue from 'vue'

Vue.config.productionTip = false

// require all test files (files that ends with .spec.js)
Expand Down
40 changes: 35 additions & 5 deletions test/unit/specs/components/helpwin/HelpWin.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
import { shallowMount } from '@vue/test-utils';
import HelpWin from '@/components/helpwin/HelpWin'
import Vue from 'vue';

describe('helpwin/HelpWin.vue', () => {
// Inspect the raw component options
it('is defined', () => {
expect(typeof HelpWin).to.not.equal('undefined');
});

describe('props', () => {
describe('unconfigured', () => {
let comp;
beforeEach(() => {
Vue.prototype.$appConfig = {modules: {}};
comp = shallowMount(HelpWin);
});

it('has correct default props', () => {
expect(comp.vm.color).to.equal('red darken-3');
expect(comp.vm.icon).to.equal('help');
expect(comp.vm.title).to.equal('About');
expect(comp.vm.headline).to.equal('About Wegue');
expect(comp.vm.content).to.equal('<h3>WebGIS with OpenLayers and Vue.js</h3> Template and re-usable components for webmapping applications with OpenLayers and Vue.js');
expect(comp.vm.infoLink).to.equal('https://github.com/meggsimum/wegue');
expect(comp.vm.windowTitle).to.equal('About');
expect(comp.vm.textTitle).to.equal('About Wegue');
expect(comp.vm.htmlContent).to.equal('<h3>WebGIS with OpenLayers and Vue.js</h3>');
expect(comp.vm.infoLinkUrl).to.equal('https://github.com/meggsimum/wegue');
expect(comp.vm.infoLinkText).to.equal('More info');
});
});

describe('configured', () => {
let comp;
beforeEach(() => {
// Config is fetched on 'mount' so need to defined before.
Vue.prototype.$appConfig = {
modules: {
'wgu-helpwin': {
'windowTitle': 'My Window Title',
'htmlContent': '<h1>MY CONTENT</h1>',
'infoLinkUrl': 'https://wegue.org',
'infoLinkText': 'Some Info Link Text'
}
}
};
comp = shallowMount(HelpWin);
});

it('has correct configured and default props', () => {
expect(comp.vm.color).to.equal('red darken-3');
expect(comp.vm.icon).to.equal('help');
expect(comp.vm.windowTitle).to.equal('My Window Title');
expect(comp.vm.textTitle).to.equal('About Wegue');
expect(comp.vm.htmlContent).to.equal('<h1>MY CONTENT</h1>');
expect(comp.vm.infoLinkUrl).to.equal('https://wegue.org');
expect(comp.vm.infoLinkText).to.equal('Some Info Link Text');
});
});

describe('data', () => {
let comp;
beforeEach(() => {
Expand Down
14 changes: 10 additions & 4 deletions test/unit/specs/components/helpwin/ToggleButton.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { shallowMount } from '@vue/test-utils';
import { mount } from '@vue/test-utils';
import HelpWinToggleBtn from '@/components/helpwin/ToggleButton';
import Vue from 'vue';

// Note: shallowMount does not work for vue test with scoped slots
// https://github.com/vuejs/vue-test-utils/issues/1261

describe('helpwin/ToggleButton.vue', () => {
// Inspect the raw component options
it('is defined', () => {
expect(typeof HelpWinToggleBtn).to.not.equal('undefined');
});

describe('props', () => {
describe('default props', () => {
let comp;
beforeEach(() => {
comp = shallowMount(HelpWinToggleBtn);
Vue.prototype.$appConfig = {modules: {}};
comp = mount(HelpWinToggleBtn);
});

it('has correct default props', () => {
Expand All @@ -24,7 +29,8 @@ describe('helpwin/ToggleButton.vue', () => {
describe('data', () => {
let comp;
beforeEach(() => {
comp = shallowMount(HelpWinToggleBtn);
Vue.prototype.$appConfig = {modules: {}};
comp = mount(HelpWinToggleBtn);
});

it('has correct default data', () => {
Expand Down
Loading

0 comments on commit 43b825c

Please sign in to comment.