Skip to content

Commit 5401e83

Browse files
committed
test: add test for editor
1 parent eaeb343 commit 5401e83

File tree

6 files changed

+89
-15
lines changed

6 files changed

+89
-15
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"eslint-plugin-import": "2.29.1",
2222
"eslint-plugin-prettier": "5.1.3",
2323
"fixpack": "4.0.0",
24+
"happy-dom": "^14.10.1",
2425
"husky": "9.0.11",
2526
"is-ci": "3.0.1",
2627
"jsdoc": "4.0.3",

src/control/cad.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CadControl extends Control {
6262
* @param {ol.style.Style.StyleLike} [options.linesStyle] Style used for the lines layer.
6363
*
6464
*/
65-
constructor(options) {
65+
constructor(options = {}) {
6666
super({
6767
title: 'CAD control',
6868
className: 'ole-control-cad',

src/editor.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class Editor {
5959
if (this.options.showToolbar) {
6060
this.toolbar = new Toolbar(this.map, this.controls, this.options.target);
6161
}
62+
63+
this.activeStateChange = this.activeStateChange.bind(this);
6264
}
6365

6466
/**
@@ -68,9 +70,7 @@ class Editor {
6870
addControl(control) {
6971
control.setMap(this.map);
7072
control.setEditor(this);
71-
7273
control.addEventListener('change:active', this.activeStateChange);
73-
7474
this.controls.push(control);
7575
}
7676

@@ -79,13 +79,11 @@ class Editor {
7979
* @param {ole.Control} control The control.
8080
*/
8181
removeControl(control) {
82-
control.deactivate(true);
83-
control.setMap(null);
84-
control.setEditor(null);
85-
control.removeEventListener('change:active', this.activeStateChange);
86-
82+
control.deactivate();
8783
this.controls.remove(control);
88-
this.activeControls.remove(control);
84+
control.removeEventListener('change:active', this.activeStateChange);
85+
control.setEditor();
86+
control.setMap();
8987
}
9088

9189
/**
@@ -115,8 +113,9 @@ class Editor {
115113
* Removes the editor from the map.
116114
*/
117115
remove() {
118-
this.controls.forEach((c) => {
119-
c.deactivate(true);
116+
const controls = [...this.controls.getArray()];
117+
controls.forEach((control) => {
118+
control.removeControl(control);
120119
});
121120
if (this.toolbar) {
122121
this.toolbar.destroy();
@@ -182,8 +181,8 @@ class Editor {
182181
* @param {ol.control.Control} control Control.
183182
* @private
184183
*/
185-
activeStateChange = (e) => {
186-
const ctrl = e.detail.control;
184+
activeStateChange(evt) {
185+
const ctrl = evt.detail.control;
187186
// Deactivate other controls that are not standalone
188187
if (ctrl.getActive() && ctrl.standalone) {
189188
for (let i = 0; i < this.controls.getLength(); i += 1) {
@@ -204,7 +203,7 @@ class Editor {
204203
} else {
205204
this.activeControls.remove(ctrl);
206205
}
207-
};
206+
}
208207
}
209208

210209
export default Editor;

src/editor.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
import { expect, test, describe, beforeEach } from 'vitest';
3+
import Map from 'ol/Map';
4+
import Editor from './editor';
5+
import CAD from './control/cad';
6+
7+
describe('editor', () => {
8+
let map;
9+
let editor;
10+
let cad;
11+
12+
beforeEach(() => {
13+
// In the test we use pixel as coordinates.
14+
map = new Map({
15+
target: document.createElement('div'),
16+
});
17+
editor = new Editor(map);
18+
cad = new CAD();
19+
});
20+
21+
test('adds a control', () => {
22+
editor.addControl(cad);
23+
expect(editor.controls.getArray()[0]).toBe(cad);
24+
expect(editor.activeControls.getLength()).toBe(0);
25+
expect(cad.map).toBe(map);
26+
expect(cad.editor).toBe(editor);
27+
expect(cad.getActive()).toBe();
28+
29+
cad.activate();
30+
expect(cad.getActive()).toBe(true);
31+
expect(editor.activeControls.getArray()[0]).toBe(cad);
32+
});
33+
34+
test('removes a control', () => {
35+
editor.addControl(cad);
36+
cad.activate();
37+
expect(cad.getActive()).toBe(true);
38+
expect(editor.controls.getArray()[0]).toBe(cad);
39+
expect(editor.activeControls.getArray()[0]).toBe(cad);
40+
editor.removeControl(cad);
41+
expect(editor.controls.getLength()).toBe(0);
42+
expect(editor.activeControls.getLength()).toBe(0);
43+
expect(cad.map).toBe();
44+
expect(cad.editor).toBe();
45+
expect(cad.getActive()).toBe(false);
46+
});
47+
});

vitest.config.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
import { defineConfig } from 'vitest/config';
3+
4+
export default defineConfig({
5+
test: {
6+
environment: 'happy-dom',
7+
},
8+
});

yarn.lock

+20-1
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ enquirer@^2.3.6:
19711971
dependencies:
19721972
ansi-colors "^4.1.1"
19731973

1974-
entities@^4.4.0:
1974+
entities@^4.4.0, entities@^4.5.0:
19751975
version "4.5.0"
19761976
resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48"
19771977
integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==
@@ -3032,6 +3032,15 @@ handlebars@^4.7.7:
30323032
optionalDependencies:
30333033
uglify-js "^3.1.4"
30343034

3035+
happy-dom@^14.10.1:
3036+
version "14.10.1"
3037+
resolved "https://registry.yarnpkg.com/happy-dom/-/happy-dom-14.10.1.tgz#fd2445489722c3363223109bb9e465409a8e1cb7"
3038+
integrity sha512-GRbrZYIezi8+tTtffF4v2QcF8bk1h2loUTO5VYQz3GZdrL08Vk0fI+bwf/vFEBf4C/qVf/easLJ/MY1wwdhytA==
3039+
dependencies:
3040+
entities "^4.5.0"
3041+
webidl-conversions "^7.0.0"
3042+
whatwg-mimetype "^3.0.0"
3043+
30353044
hard-rejection@^2.1.0:
30363045
version "2.1.0"
30373046
resolved "https://registry.yarnpkg.com/hard-rejection/-/hard-rejection-2.1.0.tgz#1c6eda5c1685c63942766d79bb40ae773cecd883"
@@ -5905,6 +5914,16 @@ web-worker@^1.2.0:
59055914
resolved "https://registry.yarnpkg.com/web-worker/-/web-worker-1.2.0.tgz#5d85a04a7fbc1e7db58f66595d7a3ac7c9c180da"
59065915
integrity sha512-PgF341avzqyx60neE9DD+XS26MMNMoUQRz9NOZwW32nPQrF6p77f1htcnjBSEV8BGMKZ16choqUG4hyI0Hx7mA==
59075916

5917+
webidl-conversions@^7.0.0:
5918+
version "7.0.0"
5919+
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a"
5920+
integrity sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==
5921+
5922+
whatwg-mimetype@^3.0.0:
5923+
version "3.0.0"
5924+
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz#5fa1a7623867ff1af6ca3dc72ad6b8a4208beba7"
5925+
integrity sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==
5926+
59085927
which-boxed-primitive@^1.0.2:
59095928
version "1.0.2"
59105929
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"

0 commit comments

Comments
 (0)