Skip to content

Commit

Permalink
chore: add labelSource test
Browse files Browse the repository at this point in the history
  • Loading branch information
BRaimbault committed Nov 28, 2024
1 parent 91f9b51 commit bb81874
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 29 deletions.
125 changes: 124 additions & 1 deletion src/utils/__tests__/labels.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,133 @@
import { labelLayer } from '../labels'
import { labelSource, labelLayer } from '../labels'
import defaults from '../style'

const id = 'abc'
const opacity = 0.5
const fontSize = 10
const labelNoData = 'No Data'
const isBoundary = false
const geometryPoint = {
type: 'Point',
coordinates: [0, 0],
}
const features = [
{
// Test a polygon
geometry: {
type: 'Polygon',
coordinates: [
[
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0],
],
],
},
properties: {
name: 'Feature1',
radius: 1,
value: 1,
},
},
{
// Test a point with a valid value which is not 0
geometry: geometryPoint,
properties: {
name: 'Feature2',
radius: 1,
value: 1,
},
},
{
// Test a point with a valid value which is 0
geometry: geometryPoint,
properties: {
name: 'Feature3',
radius: 1,
value: 0,
},
},
{
// Test a point with no value
geometry: geometryPoint,
properties: {
name: 'Feature4',
radius: 1,
},
},
]
const generateLabelSourceItem = (coordinates, name, anchor, offset, value) => {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates,
},
properties: {
name,
anchor,
offset,
color: '#333',
value,
},
}
}

describe('labels', () => {
it('Handles different label sources scenario', () => {
const expected = {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [
// Label for a polygon
generateLabelSourceItem(
[0.5, 0.5],
'Feature1',
'center',
[0, 0],
1
),
// Label for a point with a valid value which is not 0
generateLabelSourceItem(
[0, 0],
'Feature2',
'top',
[0, 0.5],
1
),
// Label for a point with a valid value which is 0
generateLabelSourceItem(
[0, 0],
'Feature3',
'top',
[0, 0.5],
0
),
// Label for a point with no value
generateLabelSourceItem(
[0, 0],
'Feature4',
'top',
[0, 0.5],
'No Data'
),
],
},
}
const received = labelSource(
features,
{ fontSize, labelNoData },
isBoundary
)

expect(JSON.stringify(received, null, 2)).toEqual(
JSON.stringify(expected, null, 2)
)
})

it('Should set opacity for for label layer', () => {
expect(labelLayer({ id, opacity }).paint['text-opacity']).toBe(opacity)
})
Expand Down
50 changes: 22 additions & 28 deletions src/utils/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,28 @@ export const labelSource = (
features,
{ fontSize, labelNoData = '' },
isBoundary
) => {
return {
type: 'geojson',
data: featureCollection(
features.map(({ geometry, properties }) => ({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: getLabelPosition(geometry),
},
properties: {
name: properties.name,
anchor: geometry.type === 'Point' ? 'top' : 'center',
offset: [
0,
getOffsetEms(
geometry.type,
properties.radius,
fontSize
),
],
color: isBoundary ? properties.color : '#333',
value: properties.value ?? labelNoData,
},
}))
),
}
}
) => ({
type: 'geojson',
data: featureCollection(
features.map(({ geometry, properties }) => ({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: getLabelPosition(geometry),
},
properties: {
name: properties.name,
anchor: geometry.type === 'Point' ? 'top' : 'center',
offset: [
0,
getOffsetEms(geometry.type, properties.radius, fontSize),
],
color: isBoundary ? properties.color : '#333',
value: properties.value ?? labelNoData,
},
}))
),
})

export const labelLayer = ({
id,
Expand Down

0 comments on commit bb81874

Please sign in to comment.