Skip to content
Snippets Groups Projects
ConsumptionView.spec.tsx 8.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { render, screen } from '@testing-library/react'
    
    import { FluidState, FluidType, TimeStep } from 'enums'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import React from 'react'
    import { Provider } from 'react-redux'
    
    import * as chartActions from 'store/chart/chart.slice'
    
    import { mockCustomPopup } from 'tests/__mocks__/customPopup.mock'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import {
    
    } from 'tests/__mocks__/fluidStatusData.mock'
    import { mockTestProfile1 } from 'tests/__mocks__/profileType.mock'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import {
      createMockEcolyoStore,
      mockChartState,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      mockInitialEcolyoState,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      mockModalState,
    
    } from 'tests/__mocks__/store'
    
    import ConsumptionView from './ConsumptionView'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    
    const mockUpdateProfile = jest.fn()
    jest.mock('services/profile.service', () => {
    
      return jest.fn(() => ({
        updateProfile: mockUpdateProfile,
      }))
    
    
    jest.mock('components/Header/CozyBar', () => 'mock-cozybar')
    jest.mock('components/Header/Header', () => 'mock-header')
    
    jest.mock('components/DateNavigator/DateNavigator', () => 'mock-dateNavigator')
    
    jest.mock('components/Content/Content', () => 'mock-content')
    
    jest.mock(
      'components/Consumption/FluidButtons/FluidButtons',
      () => 'mock-fluidButtons'
    )
    
    jest.mock('components/FluidChart/FluidChart', () => 'mock-fluidchart')
    
    jest.mock(
      'components/Consumption/ConsumptionDetails/ConsumptionDetails',
      () => 'mock-consumptionDetails'
    )
    
    jest.mock('components/Connection/Connection', () => 'mock-connection')
    jest.mock(
      'components/Konnector/KonnectorViewerCard',
    
      () => 'mock-konnectorViewerCard'
    
    jest.mock(
      'components/Connection/SGEConnect/SgeConnectView',
      () => 'mock-SgeConnectView'
    )
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    const setCurrentTimeStepSpy = jest.spyOn(chartActions, 'setCurrentTimeStep')
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const mockFluidStatus = mockInitialEcolyoState.global.fluidStatus
    mockFluidStatus[FluidType.ELECTRICITY].status = FluidState.DONE
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const mockChartStateShowOffline = { ...mockChartState, showOfflineData: true }
    
    describe('ConsumptionView component', () => {
      it('should be rendered correctly', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            ...mockChartState,
    
            showOfflineData: true,
    
          },
          global: {
            fluidStatus: mockFluidStatus,
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        })
    
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
    
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          </Provider>
        )
    
        expect(container.getElementsByTagName('mock-cozybar')[0]).toBeTruthy()
        expect(container.getElementsByTagName('mock-header')[0]).toBeTruthy()
        expect(container.getElementsByTagName('mock-dateNavigator')[0]).toBeTruthy()
        expect(container.getElementsByTagName('mock-fluidButtons')[0]).toBeTruthy()
        expect(container.getElementsByTagName('mock-fluidchart')[0]).toBeTruthy()
        expect(
          container.getElementsByTagName('mock-consumptionDetails')[0]
        ).toBeTruthy()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should set CurrentTimeStep to WEEK when fluid != ELECTRICITY and timeStep = HALF_AN_HOUR', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            ...mockChartState,
    
            currentTimeStep: TimeStep.HALF_AN_HOUR,
          },
          global: {
            fluidStatus: mockInitialEcolyoState.global.fluidStatus,
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        })
    
        render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
    
            <ConsumptionView fluidType={FluidType.GAS} />
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          </Provider>
        )
    
        expect(setCurrentTimeStepSpy).toHaveBeenCalledTimes(1)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        expect(setCurrentTimeStepSpy).toHaveBeenCalledWith(TimeStep.WEEK)
      })
    
      it('should render konnector list when no fluid is connected', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
          global: {
            fluidStatus: [],
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
        const { container } = render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.MULTIFLUID} />
          </Provider>
        )
    
        expect(
          container.getElementsByTagName('mock-consumptionDetails')[0]
        ).toBeTruthy()
    
      it('should render mutlifluid consumption if at least one fluid is connected', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            fluidStatus: mockFluidStatus,
    
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
        const { container } = render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.MULTIFLUID} />
          </Provider>
        )
    
        expect(
          container.getElementsByClassName('consumptionview-content')[0]
        ).toBeInTheDocument()
    
      it('should render Electricity when elec is connected', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            fluidStatus: mockFluidStatus,
    
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
        const { container } = render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
          </Provider>
        )
    
        expect(
          container.getElementsByClassName('consumptionview-content').item(0)
        ).toBeInTheDocument()
        expect(
          container.getElementsByTagName('mock-consumptionDetails').item(0)
        ).toBeInTheDocument()
    
    
      // todo describe and add multiple fluids ?
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      it('should render partner issue Modal', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const updatedStatus = mockInitialEcolyoState.global.fluidStatus
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
          global: {
            fluidStatus: updatedStatus,
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
          modal: {
            ...mockModalState,
            partnersIssueModal: { enedis: false, grdf: false, egl: true },
          },
    
        })
        mockUpdateProfile.mockResolvedValue(mockTestProfile1)
    
        render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
          </Provider>
        )
    
        expect(screen.getByRole('dialog')).toBeInTheDocument()
    
      it('should show expired modal when a GRDF consent is expired', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const updatedStatus = mockInitialEcolyoState.global.fluidStatus
    
        updatedStatus[0] = mockExpiredGas
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
          global: {
            fluidStatus: updatedStatus,
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        })
    
        render(
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          <Provider store={store}>
    
            <ConsumptionView fluidType={FluidType.GAS} />
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          </Provider>
        )
    
        expect(screen.getByText('consent_outdated.title.2')).toBeInTheDocument()
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      })
    
      it('should show expired modal when a Enedis consent is expired', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const updatedStatus = mockInitialEcolyoState.global.fluidStatus
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
          global: {
            fluidStatus: updatedStatus,
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
        render(
    
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
    
        expect(screen.getByText('consent_outdated.title.0')).toBeInTheDocument()
    
      })
      it('should render customPopup Modal', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            fluidStatus: mockFluidStatus,
    
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            ...mockModalState,
    
        })
        mockUpdateProfile.mockResolvedValue(mockTestProfile1)
    
        render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
          </Provider>
        )
    
        expect(screen.getByRole('dialog')).toBeInTheDocument()
    
      it('should render releaseNotesModal if releaseNotes.show is true', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            fluidStatus: mockFluidStatus,
    
            releaseNotes: {
              show: true,
              notes: [{ description: 'description', title: 'title' }],
            },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
        })
        mockUpdateProfile.mockResolvedValue(mockTestProfile1)
    
        render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
          </Provider>
        )
    
        expect(screen.getByRole('dialog')).toBeInTheDocument()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })