From a8bda963341541ec559aad3c8040817908bbf7d9 Mon Sep 17 00:00:00 2001
From: Nicolas Pagny <npagny@grandlyon.com>
Date: Wed, 25 Nov 2020 16:45:05 +0100
Subject: [PATCH] Add Jests test for utilis

---
 src/utils/date.spec.ts   |  2 +-
 src/utils/hash.spec.ts   | 11 +++++
 src/utils/hash.ts        |  1 -
 src/utils/math.spec.ts   | 20 +++++++++
 src/utils/math.ts        |  2 +-
 src/utils/picto.spec.ts  | 88 ++++++++++++++++++++++++++++++++++++++++
 src/utils/utilis.spec.ts | 38 +++++++++++++++++
 src/utils/utils.ts       |  2 +-
 8 files changed, 160 insertions(+), 4 deletions(-)
 create mode 100644 src/utils/hash.spec.ts
 create mode 100644 src/utils/math.spec.ts
 create mode 100644 src/utils/picto.spec.ts
 create mode 100644 src/utils/utilis.spec.ts

diff --git a/src/utils/date.spec.ts b/src/utils/date.spec.ts
index 09f58fee8..6c7aa36b0 100644
--- a/src/utils/date.spec.ts
+++ b/src/utils/date.spec.ts
@@ -1,7 +1,7 @@
 import { FluidType } from 'enum/fluid.enum'
 import { TimeStep } from 'enum/timeStep.enum'
 import { DateTime } from 'luxon'
-import { TimePeriod, UserChallenge } from 'models'
+import { TimePeriod } from 'models'
 import {
   compareDates,
   isLastDateReached,
diff --git a/src/utils/hash.spec.ts b/src/utils/hash.spec.ts
new file mode 100644
index 000000000..37486ed97
--- /dev/null
+++ b/src/utils/hash.spec.ts
@@ -0,0 +1,11 @@
+import { hashFile } from './hash'
+import { challengesTypeData } from '../../test/__mocks__/challengesTypeData.mock'
+
+describe('hash utilis test', () => {
+  describe('hashFile test', () => {
+    it('should return the correct hash of the file', () => {
+      const result = hashFile(challengesTypeData)
+      expect(result).toBe('721da0a51418b644a66c6bf67fcb6df46133a50c')
+    })
+  })
+})
diff --git a/src/utils/hash.ts b/src/utils/hash.ts
index 5ce23aa0c..8bd0a9729 100644
--- a/src/utils/hash.ts
+++ b/src/utils/hash.ts
@@ -1,4 +1,3 @@
-import { ChallengeType, Ecogesture } from 'models'
 import hash from 'object-hash'
 
 /***
diff --git a/src/utils/math.spec.ts b/src/utils/math.spec.ts
new file mode 100644
index 000000000..49e6737cb
--- /dev/null
+++ b/src/utils/math.spec.ts
@@ -0,0 +1,20 @@
+import { getPercentage, sum } from './math'
+
+describe('math utilis test', () => {
+  describe('getPercentage test', () => {
+    it('should return the correct percent', () => {
+      const dataA: Array<number> = [1, 2, 3, 4, 5]
+      const dataB: Array<number> = [5, 4, 3]
+      const result = getPercentage(dataA, dataB)
+      expect(result).toBe(25)
+    })
+  })
+
+  describe('sum test', () => {
+    it('should return the correct sum', () => {
+      const dataA: Array<number> = [1, 2, 3, 4, 5]
+      const result = sum(dataA)
+      expect(result).toBe(15)
+    })
+  })
+})
diff --git a/src/utils/math.ts b/src/utils/math.ts
index e9ca4cbc4..d6c5f82db 100644
--- a/src/utils/math.ts
+++ b/src/utils/math.ts
@@ -2,7 +2,7 @@ const _ = require('lodash')
 
 export function getPercentage(dataA: Array<number>, dataB: Array<number>) {
   return Math.round(
-    Number.parseFloat((1 - _.mean(dataA) / _.mean(dataB)) * 100).toFixed(2)
+    Number.parseFloat(((1 - _.mean(dataA) / _.mean(dataB)) * 100).toFixed(2))
   )
 }
 
diff --git a/src/utils/picto.spec.ts b/src/utils/picto.spec.ts
new file mode 100644
index 000000000..272ceed80
--- /dev/null
+++ b/src/utils/picto.spec.ts
@@ -0,0 +1,88 @@
+import { FluidType } from 'enum/fluid.enum'
+import ElecIcon from '../assets/icons/visu/elec.svg'
+import WaterIcon from '../assets/icons/visu/water.svg'
+import GasIcon from '../assets/icons/visu/gas.svg'
+import ElecSmallIcon from '../assets/icons/visu/elec-small.svg'
+import WaterSmallIcon from '../assets/icons/visu/water-small.svg'
+import GasSmallIcon from '../assets/icons/visu/gas-small.svg'
+import AddElecIcon from 'assets/icons/ico/add-elec.svg'
+import AddWaterIcon from 'assets/icons/ico/add-water.svg'
+import AddGasIcon from 'assets/icons/ico/add-gas.svg'
+import ElecParamIcon from 'assets/icons/visu/elec-param.svg'
+import WaterParamIcon from 'assets/icons/visu/water-param.svg'
+import GasParamIcon from 'assets/icons/visu/gas-param.svg'
+import { getAddPicto, getParamPicto, getPicto } from './picto'
+
+describe('picto utilis test', () => {
+  describe('getPicto test', () => {
+    describe('case ELECTRICTY', () => {
+      it('should return Electricty Icon', () => {
+        const result = getPicto(FluidType.ELECTRICITY)
+        expect(result).toBe(ElecIcon)
+      })
+
+      it('should return Small electricity icon', () => {
+        const result = getPicto(FluidType.ELECTRICITY, true)
+        expect(result).toBe(ElecSmallIcon)
+      })
+    })
+
+    describe('case WATER', () => {
+      it('should return water Icon', () => {
+        const result = getPicto(FluidType.WATER)
+        expect(result).toBe(WaterIcon)
+      })
+
+      it('should return Small water icon', () => {
+        const result = getPicto(FluidType.WATER, true)
+        expect(result).toBe(WaterSmallIcon)
+      })
+    })
+
+    describe('case GAS', () => {
+      it('should return gas Icon', () => {
+        const result = getPicto(FluidType.GAS)
+        expect(result).toBe(GasIcon)
+      })
+
+      it('should return Small gas icon', () => {
+        const result = getPicto(FluidType.GAS, true)
+        expect(result).toBe(GasSmallIcon)
+      })
+    })
+  })
+
+  describe('getAddPicto test', () => {
+    it('should return add electricity icon', () => {
+      const result = getAddPicto(FluidType.ELECTRICITY)
+      expect(result).toBe(AddElecIcon)
+    })
+
+    it('should return add water icon', () => {
+      const result = getAddPicto(FluidType.WATER)
+      expect(result).toBe(AddWaterIcon)
+    })
+
+    it('should return add gas icon', () => {
+      const result = getAddPicto(FluidType.GAS)
+      expect(result).toBe(AddGasIcon)
+    })
+  })
+
+  describe('getParamPicto test', () => {
+    it('should return param electricity icon', () => {
+      const result = getAddPicto(FluidType.ELECTRICITY)
+      expect(result).toBe(ElecParamIcon)
+    })
+
+    it('should return param water icon', () => {
+      const result = getAddPicto(FluidType.WATER)
+      expect(result).toBe(WaterParamIcon)
+    })
+
+    it('should return param gas icon', () => {
+      const result = getAddPicto(FluidType.GAS)
+      expect(result).toBe(GasParamIcon)
+    })
+  })
+})
diff --git a/src/utils/utilis.spec.ts b/src/utils/utilis.spec.ts
new file mode 100644
index 000000000..4fb4b994a
--- /dev/null
+++ b/src/utils/utilis.spec.ts
@@ -0,0 +1,38 @@
+import { FluidType } from 'enum/fluid.enum'
+import { formatNumberValues, getFluidType } from './utils'
+
+describe('utilis utilis test', () => {
+  describe('getFluidType test', () => {
+    it('should the electrity fluid type', () => {
+      const result = getFluidType('eLectRicity')
+      expect(result).toBe(FluidType.ELECTRICITY)
+    })
+
+    it('should the water fluid type', () => {
+      const result = getFluidType('WatER')
+      expect(result).toBe(FluidType.WATER)
+    })
+
+    it('should the gas fluid type', () => {
+      const result = getFluidType('gas')
+      expect(result).toBe(FluidType.GAS)
+    })
+  })
+
+  describe('formatNumberValues test', () => {
+    it('should return --,-- if there is not value', () => {
+      const result = formatNumberValues(null)
+      expect(result).toBe('--,--')
+    })
+
+    it('should return a value at english number format two digits decimal', () => {
+      const result = formatNumberValues(2000.555)
+      expect(result).toBe('2,000.56')
+    })
+
+    it('should return a float value', () => {
+      const result = formatNumberValues(2000.55, '', true)
+      expect(result).toEqual(2)
+    })
+  })
+})
diff --git a/src/utils/utils.ts b/src/utils/utils.ts
index 298c3bbd9..4010ff4b2 100644
--- a/src/utils/utils.ts
+++ b/src/utils/utils.ts
@@ -13,7 +13,7 @@ export function getFluidType(type: string) {
   }
 }
 export function formatNumberValues(
-  value: number,
+  value: number | null,
   fluidStyle?: string,
   toBeCompared = false
 ) {
-- 
GitLab