Skip to content
Snippets Groups Projects
ConsumptionView.spec.tsx 11.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { render, screen, waitFor } 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,
    
      mockGlobalState,
    
    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', () => {
    
      beforeEach(() => {
        jest.clearAllMocks()
      })
    
      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,
    
            ...mockGlobalState,
    
            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>
        )
    
        await waitFor(() => null, { container })
    
        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', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            ...mockChartState,
    
            currentTimeStep: TimeStep.HALF_AN_HOUR,
          },
          global: {
    
            ...mockGlobalState,
    
            fluidStatus: mockInitialEcolyoState.global.fluidStatus,
            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.GAS} />
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          </Provider>
        )
    
        await waitFor(() => null, { container })
    
        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', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
            ...mockGlobalState,
    
            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>
        )
    
        await waitFor(() => null, { container })
    
        expect(
          container.getElementsByTagName('mock-consumptionDetails')[0]
        ).toBeTruthy()
    
      it('should render mutlifluid consumption if at least one fluid is connected without konnectorViewerCard', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
            ...mockGlobalState,
    
    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>
        )
    
        await waitFor(() => null, { container })
    
        expect(
    
          container.getElementsByTagName('mock-consumptionDetails').item(0)
    
        ).toBeInTheDocument()
    
        expect(
          container.getElementsByTagName('mock-konnectorViewerCard').item(0)
        ).not.toBeInTheDocument()
    
      it('should render Electricity when elec is connected with konnectorViewCard', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
          chart: mockChartStateShowOffline,
    
            ...mockGlobalState,
    
    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>
        )
    
        await waitFor(() => null, { container })
    
        expect(
    
          container.getElementsByTagName('mock-consumptionDetails').item(0)
    
        ).toBeInTheDocument()
        expect(
    
          container.getElementsByTagName('mock-konnectorViewerCard').item(0)
    
        ).toBeInTheDocument()
    
      describe('Partner Issue Modal', () => {
        it('should render partner issue Modal for electricity', async () => {
          const updatedStatus = mockInitialEcolyoState.global.fluidStatus
          updatedStatus[0] = { ...updatedStatus[0], maintenance: true }
          const store = createMockEcolyoStore({
            chart: mockChartStateShowOffline,
            global: {
    
              ...mockGlobalState,
    
              fluidStatus: updatedStatus,
              releaseNotes: mockInitialEcolyoState.global.releaseNotes,
            },
            modal: {
              ...mockModalState,
              partnersIssueModal: { enedis: true, grdf: false, egl: false },
            },
          })
          mockUpdateProfile.mockResolvedValue(mockTestProfile1)
          const { container } = render(
            <Provider store={store}>
              <ConsumptionView fluidType={FluidType.ELECTRICITY} />
            </Provider>
          )
          await waitFor(() => null, { container })
          expect(
            screen.getByRole('presentation', {
              name: 'consumption.partner_issue_modal.accessibility_title',
            })
          ).toBeInTheDocument()
        })
    
        it('should render partner issue Modal for water', async () => {
          const updatedStatus = mockInitialEcolyoState.global.fluidStatus
          updatedStatus[1] = { ...updatedStatus[1], maintenance: true }
          const store = createMockEcolyoStore({
            chart: mockChartStateShowOffline,
            global: {
    
              ...mockGlobalState,
    
              fluidStatus: updatedStatus,
              releaseNotes: mockInitialEcolyoState.global.releaseNotes,
            },
            modal: {
              ...mockModalState,
              partnersIssueModal: { enedis: false, grdf: false, egl: true },
            },
          })
          mockUpdateProfile.mockResolvedValue(mockTestProfile1)
          const { container } = render(
            <Provider store={store}>
              <ConsumptionView fluidType={FluidType.WATER} />
            </Provider>
          )
          await waitFor(() => null, { container })
          expect(
            screen.getByRole('presentation', {
              name: 'consumption.partner_issue_modal.accessibility_title',
            })
          ).toBeInTheDocument()
    
      it('should show expired modal when a GRDF consent is expired', async () => {
    
    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,
    
            ...mockGlobalState,
    
            fluidStatus: updatedStatus,
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        })
    
        const { container } = render(
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          <Provider store={store}>
    
            <ConsumptionView fluidType={FluidType.GAS} />
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          </Provider>
        )
    
        await waitFor(() => null, { container })
    
        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', 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,
    
            ...mockGlobalState,
    
            fluidStatus: updatedStatus,
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          modal: mockModalState,
    
        const { container } = render(
    
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
    
        await waitFor(() => null, { container })
    
        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,
    
            ...mockGlobalState,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            fluidStatus: mockFluidStatus,
    
            releaseNotes: mockInitialEcolyoState.global.releaseNotes,
          },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            ...mockModalState,
    
        })
        mockUpdateProfile.mockResolvedValue(mockTestProfile1)
    
        const { container } = render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
          </Provider>
        )
    
        await waitFor(() => null, { container })
    
        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,
    
            ...mockGlobalState,
    
    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)
    
        const { container } = render(
    
          <Provider store={store}>
            <ConsumptionView fluidType={FluidType.ELECTRICITY} />
          </Provider>
        )
    
        await waitFor(() => null, { container })
    
        expect(screen.getByRole('dialog')).toBeInTheDocument()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })