Skip to content

Commit 87eabdd

Browse files
committed
fix: tests
1 parent 4fcdf1a commit 87eabdd

7 files changed

+67
-41
lines changed

.eslintrc.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
extends: ['jwalker', 'jwalker/ts', 'prettier'],
1212
rules: {
1313
'no-shadow': 'off',
14+
'no-new': 'off',
1415
'no-magic-numbers': 'off',
1516
'no-unused-vars': 'off',
1617
'no-this-before-super': 'off',

.husky/pre-commit

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npm run lint
5+
npm test

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
"svg.preview.background": "editor",
1010
"[ignore]": {
1111
"editor.defaultFormatter": "foxundermoon.shell-format"
12+
},
13+
"[shellscript]": {
14+
"editor.defaultFormatter": "foxundermoon.shell-format"
1215
}
1316
}

index.html

+23-15
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,23 @@
3838
<body>
3939
<div id="map"></div>
4040
<script type="module">
41+
import Map from 'ol/Map.js';
42+
import View from 'ol/View.js';
43+
import Feature from 'ol/Feature.js';
44+
import Point from 'ol/geom/Point.js';
45+
import { transform } from 'ol/proj';
46+
import { format } from 'ol/coordinate';
47+
import { Icon, Text, Fill, Stroke, Style } from 'ol/style.js';
48+
import { OSM, Vector as VectorSource } from 'ol/source.js';
49+
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer.js';
4150
import ContextMenu from './src/main.ts';
4251

43-
const view = new ol.View({ center: [0, 0], zoom: 4 });
44-
const vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() });
45-
const baseLayer = new ol.layer.Tile({ source: new ol.source.OSM() });
46-
const map = new ol.Map({
47-
target: 'map',
52+
const view = new View({ center: [0, 0], zoom: 2 });
53+
const vectorLayer = new VectorLayer({ source: new VectorSource() });
54+
const map = new Map({
4855
view,
49-
layers: [baseLayer, vectorLayer],
56+
target: 'map',
57+
layers: [new TileLayer({ source: new OSM() }), vectorLayer],
5058
});
5159

5260
const pinIcon =
@@ -200,21 +208,21 @@
200208
}
201209

202210
function marker(obj) {
203-
const coord4326 = ol.proj.transform(obj.coordinate, 'EPSG:3857', 'EPSG:4326'),
211+
const coord4326 = transform(obj.coordinate, 'EPSG:3857', 'EPSG:4326'),
204212
template = 'Coordinate is ({x} | {y})',
205-
iconStyle = new ol.style.Style({
206-
image: new ol.style.Icon({ scale: 0.6, src: pinIcon }),
207-
text: new ol.style.Text({
213+
iconStyle = new Style({
214+
image: new Icon({ scale: 0.6, src: pinIcon }),
215+
text: new Text({
208216
offsetY: 25,
209-
text: ol.coordinate.format(coord4326, template, 2),
217+
text: format(coord4326, template, 2),
210218
font: '15px Open Sans,sans-serif',
211-
fill: new ol.style.Fill({ color: '#111' }),
212-
stroke: new ol.style.Stroke({ color: '#eee', width: 2 }),
219+
fill: new Fill({ color: '#111' }),
220+
stroke: new Stroke({ color: '#eee', width: 2 }),
213221
}),
214222
}),
215-
feature = new ol.Feature({
223+
feature = new Feature({
216224
type: 'removable',
217-
geometry: new ol.geom.Point(obj.coordinate),
225+
geometry: new Point(obj.coordinate),
218226
});
219227

220228
feature.setStyle(iconStyle);

package-lock.json

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"lint": "eslint --cache --ext .js .",
4343
"lint:fix": "npm run lint -- --fix",
4444
"test": "jest",
45-
"prepublishOnly": "npm run lint && npm test && npm run build"
45+
"prepublishOnly": "npm run lint && npm test && npm run build",
46+
"prepare": "husky install"
4647
},
4748
"devDependencies": {
4849
"@swc/core": "^1.3.24",
@@ -53,6 +54,7 @@
5354
"eslint-config-prettier": "^8.5.0",
5455
"eslint-plugin-jest": "^27.1.7",
5556
"eslint-plugin-prettier": "^4.2.1",
57+
"husky": "^8.0.2",
5658
"identity-obj-proxy": "^3.0.0",
5759
"jest": "^29.3.1",
5860
"jest-environment-jsdom": "^29.3.1",

test/instance.spec.ts

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import OlMap from 'ol/Map';
2-
import BaseEvent from 'ol/events/Event';
32

43
import { DEFAULT_ITEMS, DEFAULT_OPTIONS } from '../src/constants';
54
import ContextMenu from '../src/main';
6-
import { ContextMenuEvent, Item } from '../src/types';
5+
import { Item } from '../src/types';
6+
7+
window.ResizeObserver =
8+
window.ResizeObserver ||
9+
jest.fn().mockImplementation(() => ({
10+
disconnect: jest.fn(),
11+
observe: jest.fn(),
12+
unobserve: jest.fn(),
13+
}));
714

815
const items: Item[] = [
916
'-',
@@ -49,10 +56,7 @@ describe('Instance options', () => {
4956
describe('Instance methods', () => {
5057
const menu = new ContextMenu();
5158

52-
// Add map to initialize the emitter
53-
new OlMap({
54-
controls: [menu],
55-
});
59+
new OlMap({ controls: [menu] });
5660

5761
test('getDefaultItems()', () => {
5862
expect(toJSON(menu.getDefaultItems())).toEqual(toJSON(DEFAULT_ITEMS));
@@ -106,26 +110,7 @@ describe('Throw errors', () => {
106110
expect(() => {
107111
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
108112
// @ts-expect-error
109-
// eslint-disable-next-line no-new
110113
new ContextMenu('foo');
111114
}).toThrow();
112115
});
113116
});
114-
115-
// EVENTS
116-
// The lines below are not a test per se, but as an ESLint indicator if the events are not correctly declared
117-
const context = new ContextMenu();
118-
119-
context.on('beforeopen', (evt: ContextMenuEvent) => {
120-
evt.pixel;
121-
evt.coordinate;
122-
});
123-
124-
context.on('open', (evt: ContextMenuEvent) => {
125-
evt.pixel;
126-
evt.coordinate;
127-
});
128-
129-
context.on('close', (evt: BaseEvent) => {
130-
evt.target;
131-
});

0 commit comments

Comments
 (0)