Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MultiLayerSlider to function component #3746

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 101 additions & 87 deletions src/Slider/MultiLayerSlider/MultiLayerSlider.example.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,106 +7,120 @@ layers showing the same area but different content or time.

```jsx
import MultiLayerSlider from '@terrestris/react-geo/dist/Slider/MultiLayerSlider/MultiLayerSlider';
import MapComponent from '@terrestris/react-util/dist/Components/MapComponent/MapComponent';
import OlLayerTile from 'ol/layer/Tile';
import OlMap from 'ol/Map';
import { fromLonLat } from 'ol/proj';
import OlSourceTileWMS from 'ol/source/TileWMS';
import OlView from 'ol/View';
import * as React from 'react';
import React from 'react';

class MultiLayerSliderExample extends React.Component {
const MultiLayerSliderExample = () => {

constructor(props) {
const layer1 = new OlLayerTile({
properties: {
name: 'Land/Water'
},
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {
LAYERS: '1_8A1104',
TILED: true
}
})
});
const layer2 = new OlLayerTile({
properties: {
name: 'True Color Composite'
},
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {
LAYERS: '1_040302',
TILED: true
}
})
});
const layer3 = new OlLayerTile({
properties: {
name: 'Color Infrared (vegetation)'
},
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {
LAYERS: '1_080403',
TILED: true
}
})
});
const layer4 = new OlLayerTile({
properties: {
name: 'Atmospheric Penetration'
},
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {
LAYERS: '1_12118A',
TILED: true
}
})
});
const layer5 = new OlLayerTile({
properties: {
name: 'Vegetation'
},
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {
LAYERS: '1_118A04',
TILED: true
}
})
});

super(props);
const map = new OlMap({
layers: [
layer1,
layer2,
layer3,
layer4,
layer5
],
view: new OlView({
center: fromLonLat([
36.8331537,
-4.0962687
]),
zoom: 13
})
});

this.mapDivId = `map-${Math.random()}`;
map.on('change:zoom')

this.layer1 = new OlLayerTile({
name: 'Land/Water',
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {LAYERS: '1_8A1104', TILED: true},
serverType: 'geoserver'
})
});
this.layer2 = new OlLayerTile({
name: 'True Color Composite',
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {LAYERS: '1_040302', TILED: true},
serverType: 'geoserver'
})
});
this.layer3 = new OlLayerTile({
name: 'Color Infrared (vegetation)',
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {LAYERS: '1_080403', TILED: true},
serverType: 'geoserver'
})
});
this.layer4 = new OlLayerTile({
name: 'Atmospheric Penetration',
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {LAYERS: '1_12118A', TILED: true},
serverType: 'geoserver'
})
});
this.layer5 = new OlLayerTile({
name: 'Vegetation',
source: new OlSourceTileWMS({
url: 'https://geoserver.mundialis.de/geoserver/wms',
params: {LAYERS: '1_118A04', TILED: true},
serverType: 'geoserver'
})
});
return (
<>
<MapComponent
map={map}
style={{
height: '400px'
}}
/>

this.map = new OlMap({
layers: [
this.layer1,
this.layer2,
this.layer3,
this.layer4,
this.layer5
],
view: new OlView({
center: [4100247.903296841, -456383.49866892234],
zoom: 13
})
});
}

componentDidMount() {
this.map.setTarget(this.mapDivId);
}

render() {
return (
<div>
<div
id={this.mapDivId}
style={{
height: '400px'
}}
<span>{'Move the slider to change the layer\'s opacity:'}</span>
<MultiLayerSlider
layers={[
layer1,
layer2,
layer3,
layer4,
layer5
]}
/>

<div>
<span>{'Move the slider to change the layer\'s opacity:'}</span>
<MultiLayerSlider
layers={[
this.layer1,
this.layer2,
this.layer3,
this.layer4,
this.layer5
]}
/>
</div>
</div>
);
}
}
</>
);
};

<MultiLayerSliderExample />
```
143 changes: 83 additions & 60 deletions src/Slider/MultiLayerSlider/MultiLayerSlider.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,105 +1,128 @@
import OlLayer from 'ol/layer/Layer';
import { ArrayTwoOrMore } from '@terrestris/base-util/dist/types';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import OlLayerBase from 'ol/layer/Base';
import React from 'react';

import TestUtil from '../../Util/TestUtil';
import MultiLayerSlider from './MultiLayerSlider';

describe('<MultiLayerSlider />', () => {
let layers: OlLayer[];
let layers: ArrayTwoOrMore<OlLayerBase>;

beforeEach(() => {
layers = [
TestUtil.createVectorLayer({}),
TestUtil.createVectorLayer({}),
TestUtil.createVectorLayer({})
];
layers.push(TestUtil.createVectorLayer({}));
});

it('is defined', () => {
expect(MultiLayerSlider).not.toBeUndefined();
});

it('can be rendered', () => {
const props = {
layers: layers
};
const wrapper = TestUtil.mountComponent(MultiLayerSlider, props);
expect(wrapper).not.toBeUndefined();
it('can rendered', () => {
const { container } = render(
<MultiLayerSlider
layers={layers}
/>
);

expect(container).toBeVisible();
});

it('sets the initial transparency of the layers', () => {
const props = {
layers: layers
};

TestUtil.mountComponent(MultiLayerSlider, props);
render(
<MultiLayerSlider
layers={layers}
/>
);
expect(layers[0].getOpacity()).toBe(1);
expect(layers[1].getOpacity()).toBe(0);
expect(layers[2].getOpacity()).toBe(0);
});

it('updates the opacity of the layer by setting a transparency value', () => {
const props = {
layers: layers
};
const wrapper = TestUtil.mountComponent(MultiLayerSlider, props);
it('updates the opacity of the layer by setting a transparency value', async () => {
render(
<MultiLayerSlider
layers={layers}
/>
);

const mark1 = screen.getByText('Layer 1');

(wrapper.instance() as MultiLayerSlider).valueUpdated(25);
expect(layers[0].getOpacity()).toBe(0.5);
expect(layers[1].getOpacity()).toBe(0.5);
await userEvent.click(mark1);

expect(layers[0].getOpacity()).toBe(1);
expect(layers[1].getOpacity()).toBe(0);
expect(layers[2].getOpacity()).toBe(0);

(wrapper.instance() as MultiLayerSlider).valueUpdated(50);
const mark2 = screen.getByText('Layer 2');

await userEvent.click(mark2);

expect(layers[0].getOpacity()).toBe(0);
expect(layers[1].getOpacity()).toBe(1);
expect(layers[2].getOpacity()).toBe(0);

(wrapper.instance() as MultiLayerSlider).valueUpdated(75);
expect(layers[0].getOpacity()).toBe(0);
expect(layers[1].getOpacity()).toBe(0.5);
expect(layers[2].getOpacity()).toBe(0.5);
const mark3 = screen.getByText('Layer 3');

await userEvent.click(mark3);

(wrapper.instance() as MultiLayerSlider).valueUpdated(100);
expect(layers[0].getOpacity()).toBe(0);
expect(layers[1].getOpacity()).toBe(0);
expect(layers[2].getOpacity()).toBe(1);

(wrapper.instance() as MultiLayerSlider).valueUpdated(0);
expect(layers[0].getOpacity()).toBe(1);
expect(layers[1].getOpacity()).toBe(0);
expect(layers[2].getOpacity()).toBe(0);
});

it.only('sets the display name for the layer based on the layer property defined by the user', () => {
it('sets the display name for the layer based on the layer property defined by the user', () => {
layers.forEach((layer, index) => {
layer.set('name', `Layer Name ${index + 1}`);
layer.set('title', `Layer Title ${index + 1}`);
});

const props = {
layers: layers,
};

// if nothing is defined, it should get the layer name
const wrapper = TestUtil.mountComponent(MultiLayerSlider, props);

const expectedMarksWithNameProperty = { 0: 'Layer Name 1', 50: 'Layer Name 2', 100: 'Layer Name 3' };
const expectedMarksWithTitleProperty = { 0: 'Layer Title 1', 50: 'Layer Title 2', 100: 'Layer Title 3' };
const expectedMarksWithoutProperty = { 0: 'Layer 1', 50: 'Layer 2', 100: 'Layer 3' };

expect((wrapper.instance() as MultiLayerSlider).getMarks()).toEqual(expectedMarksWithNameProperty);
expect((wrapper.instance() as MultiLayerSlider).formatTip(0)).toEqual('Layer Name 1 100%');

wrapper.setProps({ ...props, nameProperty: 'title' });
expect((wrapper.instance() as MultiLayerSlider).getMarks()).toEqual(expectedMarksWithTitleProperty);
expect((wrapper.instance() as MultiLayerSlider).formatTip(0)).toEqual('Layer Title 1 100%');

wrapper.setProps({ ...props, nameProperty: 'name' });
expect((wrapper.instance() as MultiLayerSlider).getMarks()).toEqual(expectedMarksWithNameProperty);
expect((wrapper.instance() as MultiLayerSlider).formatTip(0)).toEqual('Layer Name 1 100%');

wrapper.setProps({ ...props, nameProperty: 'randomProp' });
expect((wrapper.instance() as MultiLayerSlider).getMarks()).toEqual(expectedMarksWithoutProperty);
expect((wrapper.instance() as MultiLayerSlider).formatTip(0)).toEqual('Layer 1 100%');

const { rerender } = render(
<MultiLayerSlider
layers={layers}
/>
);

let mark1 = screen.getByText('Layer Name 1');
let mark2 = screen.getByText('Layer Name 2');
let mark3 = screen.getByText('Layer Name 3');

expect(mark1).toBeVisible();
expect(mark2).toBeVisible();
expect(mark3).toBeVisible();

rerender(
<MultiLayerSlider
layers={layers}
nameProperty="title"
/>
);

mark1 = screen.getByText('Layer Title 1');
mark2 = screen.getByText('Layer Title 2');
mark3 = screen.getByText('Layer Title 3');

expect(mark1).toBeVisible();
expect(mark2).toBeVisible();
expect(mark3).toBeVisible();

rerender(
<MultiLayerSlider
layers={layers}
nameProperty="randomProp"
/>
);

mark1 = screen.getByText('Layer 1');
mark2 = screen.getByText('Layer 2');
mark3 = screen.getByText('Layer 3');

expect(mark1).toBeVisible();
expect(mark2).toBeVisible();
expect(mark3).toBeVisible();
});
});
Loading
Loading