Skip to content

Commit c27cc7f

Browse files
committed
Coverage, after review and Sonar
1 parent a614a9d commit c27cc7f

4 files changed

Lines changed: 100 additions & 13 deletions

File tree

src/client/javascripts/geospatial-map.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getCentroidGridRef,
88
getCoordinateGridRef
99
} from '~/src/client/javascripts/map.js'
10+
import { formatDelimtedList } from '~/src/client/javascripts/utils.js'
1011

1112
const helpPanelConfig = {
1213
showLabel: true,
@@ -56,29 +57,29 @@ function getLineOrShapeText(allowLine, allowShape) {
5657
function getAllowedTypesPhrase(allowPoint, allowLine, allowShape) {
5758
const items = []
5859

59-
if (allowPoint) items.push('points')
60-
if (allowLine) items.push('lines')
61-
if (allowShape) items.push('shapes')
62-
63-
if (items.length === 0) return ''
64-
65-
if (items.length === 1) {
66-
return items[0]
60+
if (allowPoint) {
61+
items.push('points')
62+
}
63+
if (allowLine) {
64+
items.push('lines')
65+
}
66+
if (allowShape) {
67+
items.push('shapes')
6768
}
6869

69-
if (items.length === 2) {
70-
return `${items[0]} or ${items[1]}`
70+
if (items.length === 0) {
71+
return ''
7172
}
7273

73-
return `${items[0]}, ${items[1]} or ${items[2]}`
74+
return formatDelimtedList(items, ',', 'or')
7475
}
7576

7677
/**
7778
* @param {boolean} allowPoint
7879
* @param {boolean} allowLine
7980
* @param {boolean} allowShape
8081
*/
81-
function getHelpPanelHtml(allowPoint, allowLine, allowShape) {
82+
export function getHelpPanelHtml(allowPoint, allowLine, allowShape) {
8283
const lineOrShapeText = getLineOrShapeText(allowLine, allowShape)
8384
const doneExtra = lineOrShapeText
8485
? `<li>Double‑click, or select 'Done', when you have finished drawing ${lineOrShapeText}</li>`

src/client/javascripts/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Builds a text representation of a list in the form 'a, b, c, d or e'
3+
* @param {string[]} items
4+
* @param {string} separator
5+
* @param {string} lastSpearator
6+
*/
7+
export function formatDelimtedList(items, separator, lastSpearator) {
8+
if (items.length === 0) {
9+
return ''
10+
}
11+
12+
if (items.length === 1) {
13+
return items[0]
14+
}
15+
16+
if (items.length === 2) {
17+
return `${items[0]} ${lastSpearator} ${items[1]}`
18+
}
19+
20+
const last = items.pop()
21+
return `${items.join(`${separator} `)} ${lastSpearator} ${last}`
22+
}

test/client/javascripts/map.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
createFeatureHTML,
3-
createFeaturesHTML
3+
createFeaturesHTML,
4+
getHelpPanelHtml
45
} from '~/src/client/javascripts/geospatial-map.js'
56
import {
67
formSubmitFactory,
@@ -289,6 +290,44 @@ describe('Maps Client JS', () => {
289290
})
290291
})
291292

293+
describe('getHelpPanelHtml', () => {
294+
it('should handle only point', () => {
295+
expect(getHelpPanelHtml(true, false, false)).toBe(
296+
'<p class="govuk-body-s govuk-!-margin-bottom-2">You can add points to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Give the location a name</li></ul>'
297+
)
298+
})
299+
it('should handle only line', () => {
300+
expect(getHelpPanelHtml(false, true, false)).toBe(
301+
'<p class="govuk-body-s govuk-!-margin-bottom-2">You can add lines to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Double‑click, or select \'Done\', when you have finished drawing a line</li><li>Give the location a name</li></ul>'
302+
)
303+
})
304+
it('should handle only shape', () => {
305+
expect(getHelpPanelHtml(false, false, true)).toBe(
306+
'<p class="govuk-body-s govuk-!-margin-bottom-2">You can add shapes to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Double‑click, or select \'Done\', when you have finished drawing a shape</li><li>Give the location a name</li></ul>'
307+
)
308+
})
309+
it('should handle point and line', () => {
310+
expect(getHelpPanelHtml(true, true, false)).toBe(
311+
'<p class="govuk-body-s govuk-!-margin-bottom-2">You can add points or lines to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Double‑click, or select \'Done\', when you have finished drawing a line</li><li>Give the location a name</li></ul>'
312+
)
313+
})
314+
it('should handle point and shape', () => {
315+
expect(getHelpPanelHtml(true, false, true)).toBe(
316+
'<p class="govuk-body-s govuk-!-margin-bottom-2">You can add points or shapes to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Double‑click, or select \'Done\', when you have finished drawing a shape</li><li>Give the location a name</li></ul>'
317+
)
318+
})
319+
it('should handle line and shape', () => {
320+
expect(getHelpPanelHtml(false, true, true)).toBe(
321+
'<p class="govuk-body-s govuk-!-margin-bottom-2">You can add lines or shapes to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Double‑click, or select \'Done\', when you have finished drawing a line or shape</li><li>Give the location a name</li></ul>'
322+
)
323+
})
324+
it('should handle point, line and shape', () => {
325+
expect(getHelpPanelHtml(true, true, true)).toBe(
326+
'<p class="govuk-body-s govuk-!-margin-bottom-2">You can add points, lines or shapes to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Double‑click, or select \'Done\', when you have finished drawing a line or shape</li><li>Give the location a name</li></ul>'
327+
)
328+
})
329+
})
330+
292331
describe('Easting northing component', () => {
293332
beforeEach(() => {
294333
document.body.innerHTML = `
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { formatDelimtedList } from '~/src/client/javascripts/utils.js'
2+
3+
describe('utils', () => {
4+
describe('formatDelimitedList', () => {
5+
it('should handle empty list', () => {
6+
expect(formatDelimtedList([], ',', 'or')).toBe('')
7+
})
8+
9+
it('should handle one item', () => {
10+
expect(formatDelimtedList(['item1'], ',', 'or')).toBe('item1')
11+
})
12+
13+
it('should handle two items', () => {
14+
expect(formatDelimtedList(['item1', 'item2'], ',', 'or')).toBe(
15+
'item1 or item2'
16+
)
17+
})
18+
19+
it('should handle three items', () => {
20+
expect(formatDelimtedList(['item1', 'item2', 'item3'], ',', 'or')).toBe(
21+
'item1, item2 or item3'
22+
)
23+
})
24+
})
25+
})

0 commit comments

Comments
 (0)