Skip to content
Snippets Groups Projects
date.spec.ts 12.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { FluidType, TimeStep } from 'enums'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import { DateTime } from 'luxon'
    import { Dataload } from 'models'
    
    import { graphData } from 'tests/__mocks__/chartData.mock'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import {
      compareDates,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      convertDateToMonthYearString,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      convertDateToShortDateString,
      getActualAnalysisDate,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      getLagDays,
      isLastDateReached,
      isLastPeriodReached,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    } from './date'
    
    describe('date utils', () => {
      describe('compareDates test', () => {
        const datePrevious = DateTime.fromObject({ year: 2020, month: 11, day: 1 })
        const dateLatter = DateTime.fromObject({ year: 2020, month: 11, day: 2 })
    
        it('should return -1 if date is previous that the second one', () => {
          const result = compareDates(datePrevious, dateLatter)
          expect(result).toBe(-1)
        })
    
        it('should return 1 if the date is latter that the second one', () => {
          const result = compareDates(dateLatter, datePrevious)
          expect(result).toBe(1)
        })
      })
    
      describe('isLastDateReached test', () => {
        describe('case HALF_AN_Hour', () => {
          const timeStep = TimeStep.HALF_AN_HOUR
    
          it('should return true if date is latter that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
    
          it('should return false if date is previous that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
        })
    
        describe('case WEEK', () => {
          const timeStep = TimeStep.WEEK
    
          it('should return true if date is latter that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
    
          it('should return false if date is previous that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
        })
    
        describe('case DAY', () => {
          const timeStep = TimeStep.DAY
    
          it('should return true if date is latter that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
    
          it('should return false if date is previous that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
        })
    
        describe('case MONTH', () => {
          const timeStep = TimeStep.MONTH
    
          it('should return true if date is latter that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ months: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
    
          it('should return false if date is previous that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ months: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
        })
    
        describe('case YEAR', () => {
          const timeStep = TimeStep.YEAR
    
          it('should return true if date is latter that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ years: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
    
          it('should return false if date is previous that now', () => {
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ years: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
        })
    
        describe('default', () => {
    
          it('should return false for default case', () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            const result = isLastDateReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ years: 1 }),
    
              0 as TimeStep
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            )
            expect(result).toBe(false)
          })
        })
      })
    
      describe('IsLastPeriodReached test', () => {
        describe('case HALF_AN_HOUR', () => {
          const timeStep = TimeStep.HALF_AN_HOUR
    
          it('should return false when date is previous to now + 1 day', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
    
          it('should return true when date is latter to date + 1 day', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ days: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
        })
    
        describe('case WEEK', () => {
          const timeStep = TimeStep.WEEK
    
          it('should return false when date is previous to week + 1 day', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ week: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
    
          it('should return true when date is latter to week + 1 day', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ week: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
        })
    
        describe('case DAY', () => {
          const timeStep = TimeStep.DAY
    
          it('should return false when date is previous to now + 1 month', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ months: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
    
          it('should return true when date is latter to date + 1 month', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ months: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
        })
    
        describe('case MONTH', () => {
          const timeStep = TimeStep.MONTH
    
          it('should return false when date is previous to now + 1 year', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ years: 1 }),
              timeStep
            )
            expect(result).toBe(false)
          })
    
          it('should return true when date is latter to date + 1 year', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ years: 1 }),
              timeStep
            )
            expect(result).toBe(true)
          })
        })
    
        describe('case YEAR', () => {
          const timeStep = TimeStep.YEAR
    
          it('should return false when date is previous to now + 5 year', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ years: 5 }),
              timeStep
            )
            expect(result).toBe(false)
          })
    
          it('should return true when date is latter to date + 5 year', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .plus({ years: 5 }),
              timeStep
            )
            expect(result).toBe(true)
          })
        })
    
        describe('default', () => {
          it('should return false', () => {
            const result = isLastPeriodReached(
              DateTime.local()
                .setZone('utc', {
                  keepLocalTime: true,
                })
                .minus({ years: 1 }),
    
              0 as TimeStep
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            )
            expect(result).toBe(false)
          })
        })
      })
    
      describe('getLagDays test', () => {
        it('should return 3 when there is only WATER fluid type', () => {
          const result = getLagDays([FluidType.WATER])
          expect(result).toBe(3)
        })
    
    
        it('should return 3 when there are many fluid type including WATER', () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          const result = getLagDays([FluidType.ELECTRICITY, FluidType.WATER])
          expect(result).toBe(3)
        })
    
        it('should return 2 hen there is only GAS Fluid Type', () => {
          const result = getLagDays([FluidType.GAS])
          expect(result).toBe(2)
        })
    
    
        it('should return 2 when there are many fluid type including GAS and excluding WATER', () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          const result = getLagDays([FluidType.ELECTRICITY, FluidType.GAS])
          expect(result).toBe(2)
        })
    
    
        it('should return when there is only ELECTRICITY Fluid Type', () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          const result = getLagDays([FluidType.ELECTRICITY])
          expect(result).toBe(1)
        })
      })
    
      describe('convertDateToShortDateString test', () => {
        const actualData: Dataload[] = graphData.actualData
    
        it('should return an empty string when the TimePeriod is unknown', () => {
    
          const result = convertDateToShortDateString(null, 0 as TimeStep)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          expect(result).toBe('')
        })
    
        describe('should return the correct short date string', () => {
          it('case HALF_AN_HOUR', () => {
            const result = convertDateToShortDateString(
              actualData,
              TimeStep.HALF_AN_HOUR
            )
    
            expect(result).toBe('bilan du jeudi 01 octobre')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          })
    
          it('case WEEK', () => {
            const result = convertDateToShortDateString(actualData, TimeStep.WEEK)
    
            expect(result).toBe('bilan du 01/10 au 03/10')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          })
    
          it('case DAY', () => {
            const result = convertDateToShortDateString(actualData, TimeStep.DAY)
    
            expect(result).toBe('bilan d’octobre')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          })
    
          it('case MONTH', () => {
            const result = convertDateToShortDateString(actualData, TimeStep.MONTH)
    
            expect(result).toBe('bilan de l’année 2020')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          })
        })
      })
    
      describe('convertDateToMonthYearString test', () => {
        it('should return the name of the month and the year', () => {
          const date = DateTime.fromISO('2020-11-29T23:59:59.999Z', {
            zone: 'utc',
          })
          const result = convertDateToMonthYearString(date)
          expect(result).toBe('novembre 2020')
        })
      })
    
      describe('getActualAnalysisDate test', () => {
        it('should return the 3rd of this month if actual day >= 3', () => {
          const now = DateTime.local().setZone('utc', {
            keepLocalTime: true,
          })
          jest
            .spyOn(DateTime, 'local')
            .mockReturnValueOnce(now.set({ day: 3, month: 11, year: 2020 }))
          const mockDate = now.startOf('day').set({ day: 3, month: 11, year: 2020 })
          const result = getActualAnalysisDate()
          expect(result).toEqual(mockDate)
        })
    
        it('should return the 3rd of previous month if actual day < 3', () => {
          const now = DateTime.local().setZone('utc', {
            keepLocalTime: true,
          })
          jest
            .spyOn(DateTime, 'local')
            .mockReturnValueOnce(now.set({ day: 2, month: 11, year: 2020 }))
          const mockDate = now.startOf('day').set({ day: 3, month: 10, year: 2020 })
          const result = getActualAnalysisDate()
          expect(result).toEqual(mockDate)
        })
      })
    })