Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aa63544

Browse files
committedJun 24, 2024
Add basic tests for some components
1 parent b256f91 commit aa63544

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed
 

‎components/StandardApp.jsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ class AppInitComponent extends React.Component {
130130
initialView = {
131131
center: coords,
132132
zoom: zoom,
133-
crs: params.crs || theme.mapCrs};
133+
crs: params.crs || theme.mapCrs
134+
};
134135
}
135136
} else if (params.e) {
136137
const bounds = params.e.split(/[;,]/g).map(x => parseFloat(x));
@@ -223,6 +224,10 @@ export default class StandardApp extends React.Component {
223224
);
224225
}
225226
setupTouchEvents = (el) => {
227+
if (el === null) {
228+
// Do nothing when unmounting
229+
return;
230+
}
226231
el.addEventListener('touchstart', ev => {
227232
this.touchY = ev.targetTouches[0].clientY;
228233
}, { passive: false });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import {Provider} from "react-redux";
3+
4+
import {render, screen} from '@testing-library/react';
5+
import configureStore from 'redux-mock-store';
6+
7+
import CoordinateDisplayer from '../../components/CoordinateDisplayer';
8+
9+
const mockStore = configureStore();
10+
11+
test('current coordinates are shown', () => {
12+
const store = mockStore({
13+
map: { projection: 'EPSG:4326' },
14+
mousePosition: { position: { coordinate: [123, 456] } }
15+
});
16+
17+
render(
18+
<Provider store={store}>
19+
<CoordinateDisplayer displaycrs={'EPSG:4326'} />
20+
</Provider>
21+
);
22+
23+
expect(screen.getByRole('textbox')).toHaveValue('123.0000 456.0000');
24+
});

‎tests/components/SearchBox.test.jsx

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React from 'react';
2+
import {Provider} from "react-redux";
3+
4+
import {act, fireEvent, render, screen} from '@testing-library/react';
5+
import configureStore from 'redux-mock-store';
6+
7+
import SearchBox from '../../components/SearchBox';
8+
9+
const mockStore = configureStore();
10+
11+
const searchProviders = {
12+
testing: {
13+
onSearch: (text, searchParams, callback) => callback({
14+
results: [
15+
{
16+
id: 'layer1',
17+
title: 'Layer 1',
18+
items: [
19+
{
20+
id: 'item1',
21+
text: 'Item 1'
22+
},
23+
{
24+
id: 'item2',
25+
text: 'Item 2'
26+
}
27+
]
28+
},
29+
{
30+
id: 'layer2',
31+
title: 'Layer 2',
32+
items: [
33+
{
34+
id: 'item3',
35+
text: 'Item 3'
36+
}
37+
]
38+
}
39+
]
40+
})
41+
}
42+
};
43+
44+
// eslint-disable-next-line new-cap
45+
const Search = SearchBox(searchProviders);
46+
47+
test('search results are visible', () => {
48+
const store = mockStore({
49+
map: { projection: 'EPSG:4326' },
50+
layers: { flat: [] },
51+
theme: { current: { searchProviders: ['testing'] } }
52+
});
53+
54+
const searchOptions = {
55+
allowSearchFilters: false
56+
};
57+
58+
render(
59+
<Provider store={store}>
60+
<Search searchOptions={searchOptions} />
61+
</Provider>
62+
);
63+
64+
const input = screen.getByRole('input');
65+
expect(input).toHaveValue('');
66+
67+
fireEvent.change(input, { target: { value: 'Test' } });
68+
act(() => input.focus());
69+
70+
expect(input).toHaveValue('Test');
71+
expect(screen.getByText('Layer 1')).toBeInTheDocument();
72+
expect(screen.getByText('Layer 2')).toBeInTheDocument();
73+
expect(screen.getByText('Item 1')).toBeInTheDocument();
74+
expect(screen.getByText('Item 2')).toBeInTheDocument();
75+
expect(screen.getByText('Item 3')).toBeInTheDocument();
76+
});

‎tests/components/StandardApp.test.jsx

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import {Provider} from "react-redux";
3+
4+
import {render} from '@testing-library/react';
5+
import configureStore from 'redux-mock-store';
6+
7+
import StandardApp from "../../components/StandardApp";
8+
9+
const mockStore = configureStore();
10+
11+
test('app is running w/o plugins', () => {
12+
const store = mockStore({});
13+
14+
const appConfig = {
15+
initialState: {
16+
defaultState: {}
17+
},
18+
pluginsDef: {
19+
plugins: {}
20+
}
21+
};
22+
23+
render(
24+
<Provider store={store}>
25+
<StandardApp appConfig={appConfig} />
26+
</Provider>
27+
);
28+
});

‎tests/jest.setup.js

+11
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1+
import LocaleUtils from "../utils/LocaleUtils";
2+
import MapUtils from "../utils/MapUtils";
3+
import MockMap from "./mocks/MockMap";
4+
15
import '@testing-library/jest-dom';
6+
7+
// Mock translation function, just return the message key
8+
LocaleUtils.tr = (key) => key;
9+
LocaleUtils.lang = () => 'en';
10+
11+
// Mock the Map object globally
12+
MapUtils.registerHook(MapUtils.GET_MAP, new MockMap());

‎tests/mocks/MockMap.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default class MockMap {
2+
addLayer = () => null;
3+
removeLayer = () => null;
4+
getViewport = () => document.createElement('div');
5+
}

0 commit comments

Comments
 (0)
Please sign in to comment.