From bdd0aaaba222051a92b47a2588747d4d8213cd51 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Tue, 17 Dec 2024 12:17:33 +0100 Subject: [PATCH] testing colors --- js/color-utils.js | 27 ++++++++++++++++++------- js/main.js | 6 +++--- tests/main.test.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/js/color-utils.js b/js/color-utils.js index 24891f5..3a48d08 100644 --- a/js/color-utils.js +++ b/js/color-utils.js @@ -1,5 +1,4 @@ -import utils from "./utils.js" - +import utils from './utils.js' let unknown_color = '#c0b1b1' // color for unknown power output of the station const colors = [ @@ -16,20 +15,34 @@ const error_color = '#ff1414' const max_out_legit_power = 400 const colorUtils = { + colors, /** * trouver une couleur correspondant */ - getColor : (feature) =>{ + getColor: (feature) => { let outputPower = utils.guessOutputPowerFromFeature(feature) feature.properties.tags.has_output_of_irve_specified = outputPower if (outputPower) { - if(outputPower> max_out_legit_power){ - return error_color; + if (outputPower > max_out_legit_power) { + return error_color + } + let index = 0 + // index = Math.min(Math.floor(outputPower / 10), colors.length - 1) + if (outputPower >= 300) { + index = 5 + } else if (outputPower >= 200) { + index = 4 + } else if (outputPower >= 100) { + index = 3 + } else if (outputPower >= 50) { + index = 2 + } else if (outputPower >= 20) { + index = 1 + } else if (outputPower >= 0) { + index = 0 } - let index = Math.min(Math.floor(outputPower / 10), colors.length - 1) - return colors[index] } // autrement, sans puissance max trouvée, on met la couleur des indéfinis diff --git a/js/main.js b/js/main.js index 6dd2e47..cc74beb 100644 --- a/js/main.js +++ b/js/main.js @@ -37,16 +37,16 @@ function setRandomView(){ } function setCoordinatesOfLeafletMapFromQueryParameters() { // Récupère les paramètres de l'URL - console.log('window.location', window.location.href, window) + // console.log('window.location', window.location.href, window) const urlParams = new URLSearchParams(window.location.href); - console.log('urlParams', urlParams) + // console.log('urlParams', urlParams) // Récupère les coordonnées et le zoom à partir des paramètres de l'URL const lat = urlParams.get('lat'); const lng = urlParams.get('lng'); const zoom = urlParams.get('zoom'); - console.log('lat,lng,zoom', lat, lng, zoom) // Vérifie si les paramètres sont présents et valides + // console.log('lat,lng,zoom', lat, lng, zoom) // Vérifie si les paramètres sont présents et valides if (lat && lng && zoom) { // Initialise la carte avec les coordonnées et le zoom récupérés map = map.setView([lat, lng], zoom); diff --git a/tests/main.test.js b/tests/main.test.js index 32d65eb..267f0b8 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -40,6 +40,56 @@ describe('testing on features', () => { let color = colorUtils.getColor(featureWithBadOutput) expect(color).toEqual('#ff1414') }) + + it('finds first color for 3 kW', () => { + let localFeature = Object.create(featureWithOutput) + localFeature.properties.tags['socket:type2:output'] = "3 kW" + let outputFound = utils.guessOutputPowerFromFeature(featureWithOutput) + expect(outputFound).toEqual(3) + let color = colorUtils.getColor(localFeature) + expect(color).toEqual(colorUtils.colors[0]) + }) + it('finds 2nd color for 20 kW', () => { + let localFeature = Object.create(featureWithOutput) + localFeature.properties.tags['socket:type2:output'] = "20 kW" + let outputFound = utils.guessOutputPowerFromFeature(featureWithOutput) + expect(outputFound).toEqual(20) + let color = colorUtils.getColor(localFeature) + expect(color).toEqual(colorUtils.colors[1]) + }) + it('finds 3nd color for 50 kW', () => { + let localFeature = Object.create(featureWithOutput) + localFeature.properties.tags['socket:type2:output'] = "50 kW" + let outputFound = utils.guessOutputPowerFromFeature(featureWithOutput) + expect(outputFound).toEqual(50) + let color = colorUtils.getColor(localFeature) + expect(color).toEqual(colorUtils.colors[2]) + }) + it('finds 3nd color for 100 kW', () => { + let localFeature = Object.create(featureWithOutput) + localFeature.properties.tags['socket:type2:output'] = "100 kW" + let outputFound = utils.guessOutputPowerFromFeature(featureWithOutput) + expect(outputFound).toEqual(100) + let color = colorUtils.getColor(localFeature) + expect(color).toEqual(colorUtils.colors[3]) + }) + it('finds 4nd color for 200 kW', () => { + let localFeature = Object.create(featureWithOutput) + localFeature.properties.tags['socket:type2:output'] = "200 kW" + let outputFound = utils.guessOutputPowerFromFeature(featureWithOutput) + expect(outputFound).toEqual(200) + let color = colorUtils.getColor(localFeature) + expect(color).toEqual(colorUtils.colors[4]) + }) + it('finds 4nd color for 300 kW', () => { + let localFeature = Object.create(featureWithOutput) + localFeature.properties.tags['socket:type2:output'] = "300 kW" + let outputFound = utils.guessOutputPowerFromFeature(featureWithOutput) + expect(outputFound).toEqual(300) + let color = colorUtils.getColor(localFeature) + expect(color).toEqual(colorUtils.colors[5]) + }) + }) })