-
Notifications
You must be signed in to change notification settings - Fork 58
Increase test coverage #4394
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
Open
marcjansen
wants to merge
8
commits into
terrestris:main
Choose a base branch
from
marcjansen:increase-test-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+557
−6
Open
Increase test coverage #4394
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5d1b521
chore: update jest's transformIgnorePatterns & file exclusions
marcjansen c2b5824
fix: better implementation for initiallySelectedLayer / backgroundLay…
marcjansen c7a63a4
chore: add tests for BackgroundLayerChooser
marcjansen 74c5b46
chore: add tests for BackgroundLayerPreview
marcjansen ec872b4
chore: add tests for DrawCutButton
marcjansen 96eade6
chore: update jest's ignore files
marcjansen d07e30e
chore: add tests for FeatureLabelModal
marcjansen 10815d6
chore: add tests for SearchField
marcjansen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
src/BackgroundLayerChooser/BackgroundLayerChooser.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import * as React from 'react'; | ||
|
||
import Map from 'ol/Map'; | ||
import View from 'ol/View'; | ||
import TileLayer from 'ol/layer/Tile'; | ||
import OSM from 'ol/source/OSM' | ||
// import TileWMS from 'ol/source/TileWMS'; | ||
|
||
import { | ||
renderInMapContext | ||
} from '@terrestris/react-util/dist/Util/rtlTestUtils'; | ||
|
||
import BackgroundLayerChooser from './BackgroundLayerChooser'; | ||
import { act, fireEvent, waitFor } from '@testing-library/react'; | ||
|
||
describe('<BackGroundLayerChooser />', () => { | ||
|
||
let map: Map; | ||
let layers: TileLayer[]; | ||
|
||
function createLayers() { | ||
return [ | ||
new TileLayer({ | ||
source: new OSM(), | ||
properties: { | ||
name: 'OSM', | ||
isBackgroundLayer: true | ||
} | ||
}), | ||
new TileLayer({ | ||
source: new OSM(), | ||
properties: { | ||
name: 'OSM2', | ||
isBackgroundLayer: true | ||
} | ||
}) | ||
]; | ||
} | ||
|
||
beforeEach(() => { | ||
layers = createLayers(); | ||
map = new Map({ | ||
view: new View({ | ||
center: [0, 0], | ||
zoom: 1 | ||
}), | ||
layers | ||
}); | ||
}); | ||
|
||
|
||
describe('#Basics', () => { | ||
it('is defined', () => { | ||
expect(BackgroundLayerChooser).not.toBeUndefined(); | ||
}); | ||
|
||
it('can be rendered', () => { | ||
const { container } = renderInMapContext( | ||
map, | ||
<BackgroundLayerChooser | ||
layers={layers} | ||
allowEmptyBackground={true} | ||
/> | ||
); | ||
expect(container.querySelector('.bg-layer-chooser')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('shows layer cards when button is clicked', async () => { | ||
const { container } = renderInMapContext( | ||
map, | ||
<BackgroundLayerChooser | ||
layers={layers} | ||
allowEmptyBackground={true} | ||
/> | ||
); | ||
const btn = container.querySelector('.change-bg-btn'); | ||
await act(async () => { | ||
btn && fireEvent.click(btn); | ||
}); | ||
await waitFor(() => { | ||
expect(container.querySelector('.layer-cards')).toBeInTheDocument(); | ||
expect(container.querySelectorAll('.layer-preview').length).toBeGreaterThan(0); | ||
}); | ||
}); | ||
|
||
it('calls titleRenderer for each layer', async () => { | ||
const titleRenderer = jest.fn(l => <span>Layer: {l.get('name')}</span>); | ||
const { container } = renderInMapContext( | ||
map, | ||
<BackgroundLayerChooser | ||
layers={layers} | ||
allowEmptyBackground={true} | ||
titleRenderer={titleRenderer} | ||
/> | ||
); | ||
const btn = container.querySelector('.change-bg-btn'); | ||
await act(async () => { | ||
btn && fireEvent.click(btn); | ||
}); | ||
await waitFor(() => { | ||
expect(titleRenderer).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('shows no background option and selects it', async () => { | ||
const { container } = renderInMapContext( | ||
map, | ||
<BackgroundLayerChooser | ||
layers={layers} | ||
allowEmptyBackground={true} | ||
noBackgroundTitle="None" | ||
/> | ||
); | ||
const btn = container.querySelector('.change-bg-btn'); | ||
await act(async () => { | ||
btn && fireEvent.click(btn); | ||
}); | ||
const noBg = await waitFor(() => container.querySelector('.no-background')); | ||
expect(noBg).toBeInTheDocument(); | ||
await act(async () => { | ||
noBg && fireEvent.click(noBg); | ||
}); | ||
await waitFor(() => { | ||
expect(container.querySelector('.bg-preview .layer-title')?.textContent).toBe('None'); | ||
}); | ||
}); | ||
|
||
it('selects a background layer when preview is clicked', async () => { | ||
const { container } = renderInMapContext( | ||
map, | ||
<BackgroundLayerChooser | ||
layers={layers} | ||
allowEmptyBackground={true} | ||
/> | ||
); | ||
const btn = container.querySelector('.change-bg-btn'); | ||
await act(async () => { | ||
btn && fireEvent.click(btn); | ||
}); | ||
const previews = await waitFor(() => container.querySelectorAll('.layer-preview')); | ||
expect(previews.length).toBeGreaterThan(0); | ||
await act(async () => { | ||
previews[0] && fireEvent.click(previews[0]); | ||
}); | ||
await waitFor(() => { | ||
expect(container.querySelector('.bg-preview .layer-title')?.textContent).toBe('OSM'); | ||
}); | ||
}); | ||
|
||
it('respects initiallySelectedLayer prop', async () => { | ||
// Do not set allowEmptyBackground, so 'No Background' is not selected | ||
const { container } = renderInMapContext( | ||
map, | ||
<BackgroundLayerChooser | ||
layers={layers} | ||
allowEmptyBackground={false} | ||
initiallySelectedLayer={layers[1]} | ||
/> | ||
); | ||
await waitFor(() => { | ||
expect(container.querySelector('.bg-preview .layer-title')?.textContent).toBe('OSM2'); | ||
}); | ||
}); | ||
|
||
it('filters layers using backgroundLayerFilter', async () => { | ||
const filter = (l: any) => l.get('name') === 'OSM2'; | ||
const { container } = renderInMapContext( | ||
map, | ||
<BackgroundLayerChooser | ||
layers={layers} | ||
allowEmptyBackground={false} | ||
backgroundLayerFilter={filter} | ||
/> | ||
); | ||
const btn = container.querySelector('.change-bg-btn'); | ||
await act(async () => { | ||
btn && fireEvent.click(btn); | ||
}); | ||
await waitFor(() => { | ||
// Only OSM2 should be present | ||
const previews = container.querySelectorAll('.layer-preview'); | ||
expect(previews.length).toBe(1); | ||
const title = (previews[0] as Element).querySelector('.layer-title')?.textContent; | ||
expect(title).toBe('OSM2'); | ||
}); | ||
}); | ||
|
||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,9 +115,10 @@ export const BackgroundLayerChooser: React.FC<BackgroundLayerChooserProps> = ({ | |
}, [map, layerOptionsVisible]); | ||
|
||
useEffect(() => { | ||
const activeLayerCand = layers.find(l => l.getVisible()); | ||
|
||
if (!initiallySelectedLayer) { | ||
if (initiallySelectedLayer) { | ||
setSelectedLayer(initiallySelectedLayer); | ||
} else { | ||
const activeLayerCand = layers.find(l => l.getVisible()); | ||
setSelectedLayer(activeLayerCand as OlLayer); | ||
} | ||
}, [initiallySelectedLayer, layers]); | ||
|
@@ -227,7 +228,7 @@ export const BackgroundLayerChooser: React.FC<BackgroundLayerChooserProps> = ({ | |
layerOptionsVisible && ( | ||
<div className="layer-cards"> | ||
{ | ||
layers.map(layer => ( | ||
layers.filter(backgroundLayerFilter).map(layer => ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catches! |
||
<BackgroundLayerPreview | ||
key={getUid(layer)} | ||
activeLayer={selectedLayer} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this is only because of the querySelector possibly returning null? To satisfy the typechecking?
If the
btn
does not exist in the document, the event will not be fired and the error might be hard to debug.maybe something like this works as well:
?