Skip to content
Snippets Groups Projects
Bar.spec.tsx 3.64 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { scaleBand, ScaleBand, scaleLinear } from 'd3'
    
    import { FluidType, TimeStep } from 'enums'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import { mount } from 'enzyme'
    
    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, mockGlobalState } from 'tests/__mocks__/store'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import Bar from './Bar'
    
    const mockXScale: ScaleBand<string> = scaleBand()
      .domain(
        graphData.actualData.map(d =>
          d.date.toLocaleString(DateTime.DATETIME_SHORT)
        )
      )
      .range([0, 10])
      .padding(0.2)
    
    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: {
          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', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} />
            </svg>
          </Provider>
        )
        expect(wrapper.getElement()).toMatchSnapshot()
      })
    
      it('should correctly render Electricity Bar', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} fluidType={FluidType.ELECTRICITY} />
            </svg>
          </Provider>
        )
        expect(wrapper.getElement()).toMatchSnapshot()
      })
    
      it('should correctly render Water Bar', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} fluidType={FluidType.WATER} />
            </svg>
          </Provider>
        )
        expect(wrapper.getElement()).toMatchSnapshot()
      })
    
      it('should correctly render Gas Bar', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} fluidType={FluidType.GAS} />
            </svg>
          </Provider>
        )
        expect(wrapper.getElement()).toMatchSnapshot()
      })
    
      it('should correctly render Bar with showCompare', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
    
              <Bar {...mockParam} compare={true} />
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            </svg>
          </Provider>
        )
        expect(wrapper.getElement()).toMatchSnapshot()
      })
    
      it('should correctly render Bar with isSwitching', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} isSwitching={true} />
            </svg>
          </Provider>
        )
        expect(wrapper.getElement()).toMatchSnapshot()
      })
    
      it('should correctly render Bar with isDuel', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} isDuel={true} />
            </svg>
          </Provider>
        )
        expect(wrapper.getElement()).toMatchSnapshot()
      })
    
      it('should dispatch selected date when bar is clicked', () => {
        const wrapper = mount(
          <Provider store={store}>
            <svg>
              <Bar {...mockParam} />
            </svg>
          </Provider>
        )
    
        wrapper.find('rect').first().simulate('click')
    
        expect(setSelectedDateSpy).toHaveBeenCalledTimes(1)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        expect(setSelectedDateSpy).toHaveBeenCalledWith(
          graphData.actualData[0].date
        )
      })
    })