Skip to content
Snippets Groups Projects
Bar.spec.tsx 3.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { render } from '@testing-library/react'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { userEvent } from '@testing-library/user-event'
    import { scaleLinear } from 'd3'
    
    import { FluidType, TimeStep } from 'enums'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { DateTime } from 'luxon'
    import React from 'react'
    import { Provider } from 'react-redux'
    
    import * as chartActions from 'store/chart/chart.slice'
    
    import { graphData } from 'tests/__mocks__/chartData.mock'
    
    import {
      createMockEcolyoStore,
      mockChartState,
      mockGlobalState,
    } from 'tests/__mocks__/store'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { mockXScale } from 'tests/__mocks__/xScale.mock'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import Bar from './Bar'
    
    const mockParam = {
      index: 4,
      dataload: graphData.actualData[0],
      compareDataload: graphData.actualData[1],
      fluidType: FluidType.MULTIFLUID,
      timeStep: TimeStep.DAY,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      xScale: mockXScale,
      yScale: scaleLinear(),
      height: 20,
      isSwitching: false,
      isDuel: false,
    }
    
    const setSelectedDateSpy = jest.spyOn(chartActions, 'setSelectedDate')
    
    describe('Bar component test', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const store = createMockEcolyoStore({
        chart: {
    
          ...mockChartState,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          selectedDate: DateTime.fromISO('2020-10-01T00:00:00.000Z', {
            zone: 'utc',
          }),
        },
    
        global: mockGlobalState,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should correctly render Multifluid Bar', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} />
            </svg>
          </Provider>
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should correctly render Electricity Bar', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} fluidType={FluidType.ELECTRICITY} />
            </svg>
          </Provider>
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should correctly render Water Bar', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} fluidType={FluidType.WATER} />
            </svg>
          </Provider>
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should correctly render Gas Bar', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} fluidType={FluidType.GAS} />
            </svg>
          </Provider>
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should correctly render Bar with showCompare', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
    
              <Bar {...mockParam} compare={true} />
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            </svg>
          </Provider>
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should correctly render Bar with isSwitching', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} isSwitching={true} />
            </svg>
          </Provider>
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should correctly render Bar with isDuel', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} isDuel={true} />
            </svg>
          </Provider>
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(container).toMatchSnapshot()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      it('should dispatch selected date when bar is clicked', async () => {
        const { container } = render(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} />
            </svg>
          </Provider>
        )
    
        await userEvent.click(container.querySelector('rect') as Element)
    
        expect(setSelectedDateSpy).toHaveBeenCalledTimes(1)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        expect(setSelectedDateSpy).toHaveBeenCalledWith(
          graphData.actualData[0].date
        )
      })
    })