Skip to content
Snippets Groups Projects
AnalysisView.spec.tsx 2.73 KiB
Newer Older
  • Learn to ignore specific revisions
  • import AnalysisView from 'components/Analysis/AnalysisView'
    
    import { mount } from 'enzyme'
    
    import { Provider } from 'react-redux'
    
    import * as globalActions from 'store/global/global.slice'
    
    import * as storeHooks from 'store/hooks'
    
    import * as profileActions from 'store/profile/profile.slice'
    
    import {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      createMockEcolyoStore,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      mockChartState,
      mockProfileState,
    
    } from 'tests/__mocks__/store'
    
    jest.mock('components/Header/CozyBar', () => 'mock-cozybar')
    jest.mock('components/Header/Header', () => 'mock-header')
    jest.mock('components/Content/Content', () => 'mock-content')
    jest.mock('components/DateNavigator/DateNavigator', () => 'mock-datenavigator')
    jest.mock('components/Analysis/MonthlyAnalysis', () => 'mock-monthlyanalysis')
    
    
    jest.mock('react-router-dom', () => ({
      ...jest.requireActual('react-router-dom'),
      useLocation: () => ({
        pathname: 'ecolyo.cozy.localhost:8080/#/analysis',
      }),
    }))
    
    
    const mockAppDispatch = jest.spyOn(storeHooks, 'useAppDispatch')
    
    const toggleAnalysisNotificationSpy = jest.spyOn(
      globalActions,
      'toggleAnalysisNotification'
    )
    const updateProfileSpy = jest.spyOn(profileActions, 'updateProfile')
    
    describe('AnalysisView component', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const store = createMockEcolyoStore()
    
      beforeEach(() => {
    
        jest.clearAllMocks()
    
      })
    
      it('should be rendered correctly', () => {
        const wrapper = mount(
          <Provider store={store}>
            <AnalysisView />
          </Provider>
        )
    
        expect(wrapper.find('mock-cozybar').exists()).toBeTruthy()
        expect(wrapper.find('mock-header').exists()).toBeTruthy()
        expect(wrapper.find('mock-datenavigator').exists()).toBeTruthy()
        expect(wrapper.find('mock-monthlyanalysis').exists()).toBeTruthy()
    
      })
    
      it('should update profile and toggle analysis notification to false if notification is true', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        const store = createMockEcolyoStore({
    
          analysis: mockAnalysisState,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          chart: mockChartState,
          global: { analysisNotification: true },
          profile: { ...mockProfileState, haveSeenLastAnalysis: true },
    
        mockAppDispatch.mockReturnValue(jest.fn())
    
        const wrapper = mount(
          <Provider store={store}>
            <AnalysisView />
          </Provider>
        )
    
        expect(wrapper.find('mock-cozybar').exists()).toBeTruthy()
        expect(wrapper.find('mock-header').exists()).toBeTruthy()
        expect(wrapper.find('mock-datenavigator').exists()).toBeTruthy()
        expect(wrapper.find('mock-monthlyanalysis').exists()).toBeTruthy()
    
        expect(updateProfileSpy).toHaveBeenCalledTimes(1)
    
        expect(updateProfileSpy).toHaveBeenCalledWith({
          haveSeenLastAnalysis: true,
        })
    
        expect(toggleAnalysisNotificationSpy).toHaveBeenCalledTimes(1)
    
        expect(toggleAnalysisNotificationSpy).toHaveBeenCalledWith(false)
      })
    })