diff --git a/web/src/features/psuInsights/components/map/psuFeatureStylers.test.ts b/web/src/features/psuInsights/components/map/psuFeatureStylers.test.ts index b8c782833..f5d13d3ad 100644 --- a/web/src/features/psuInsights/components/map/psuFeatureStylers.test.ts +++ b/web/src/features/psuInsights/components/map/psuFeatureStylers.test.ts @@ -1,4 +1,4 @@ -import { setTransparency } from '@/features/psuInsights/components/map/psuFeatureStylers' +import { getColorForRasterValue, setTransparency } from '@/features/psuInsights/components/map/psuFeatureStylers' describe('setTransparency', () => { it('should return an rgba value from rgb with correct alpha value', () => { @@ -17,4 +17,17 @@ describe('setTransparency', () => { const incompleteColor = 'rgb(1, 2)' expect(() => setTransparency(incompleteColor, 0.5)).toThrow(Error) }) + + it('should return a completely transparent colour if no colour is provided as input', () => { + const rgba = setTransparency(undefined, 0.5) + expect(rgba).toBe('rgba(0, 0, 0, 0)') + }) +}) + +describe('getColorForRasterValue', () => { + it('should get the correct colour for the specified raster value', () => { + const rasterValue = 1 + const colour = getColorForRasterValue(rasterValue) + expect(colour).toBe('rgb(209, 255, 115)') + }) }) diff --git a/web/src/features/psuInsights/components/map/psuFeatureStylers.ts b/web/src/features/psuInsights/components/map/psuFeatureStylers.ts index b38fa2e43..26571e0ff 100644 --- a/web/src/features/psuInsights/components/map/psuFeatureStylers.ts +++ b/web/src/features/psuInsights/components/map/psuFeatureStylers.ts @@ -22,9 +22,9 @@ const rasterValueToFuelTypeCode = new Map([ [14, 'M-1/M-2'] ]) -const getColorForRasterValue = (rasterValue: number): string => { +export const getColorForRasterValue = (rasterValue: number): string | undefined => { const fuelTypeCode = rasterValueToFuelTypeCode.get(rasterValue) - return fuelTypeCode ? colorByFuelTypeCode.get(fuelTypeCode) : null + return fuelTypeCode ? colorByFuelTypeCode.get(fuelTypeCode) : undefined } /** @@ -33,7 +33,7 @@ const getColorForRasterValue = (rasterValue: number): string => { * @param alpha number value between 0 and 1 * @returns rgba value with alpha set ex. rgba(1, 2, 3, 0.5) */ -export const setTransparency = (color: string, alpha: number): string => { +export const setTransparency = (color: string | undefined, alpha: number): string => { if (!color) return 'rgba(0, 0, 0, 0)' const rgbMatch = color.match(/\d+/g) if (!rgbMatch || rgbMatch.length < 3) {