Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brettedw committed Dec 3, 2024
1 parent 5143165 commit 4208e62
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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)')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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) {
Expand Down

0 comments on commit 4208e62

Please sign in to comment.