Skip to content
Snippets Groups Projects
ReportOptions.spec.tsx 2.25 KiB
Newer Older
  • Learn to ignore specific revisions
  • Yoan VALLET's avatar
    Yoan VALLET committed
    import React from 'react'
    import { mount } from 'enzyme'
    import { Provider } from 'react-redux'
    import ReportOptions from 'components/Options/ReportOptions'
    import {
      createMockStore,
      mockInitialEcolyoState,
    } from '../../../tests/__mocks__/store'
    import * as profileActions from 'store/profile/profile.actions'
    
    import { Button } from '@material-ui/core'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    jest.mock('cozy-ui/transpiled/react/I18n', () => {
      return {
        useI18n: jest.fn(() => {
          return {
            t: (str: string) => str,
          }
        }),
      }
    })
    
    const mockUpdateProfile = jest.fn()
    jest.mock('services/profile.service', () => {
      return jest.fn(() => {
        return {
          updateProfile: mockUpdateProfile,
        }
      })
    })
    
    const updateProfileSpy = jest.spyOn(profileActions, 'updateProfile')
    
    describe('ReportOptions component', () => {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      let store: any
      beforeEach(() => {
        store = createMockStore(mockInitialEcolyoState)
        updateProfileSpy.mockClear()
      })
    
      it('should be rendered with sendAnalysisNotification to false', () => {
        const wrapper = mount(
          <Provider store={store}>
            <ReportOptions />
          </Provider>
        )
    
        expect(wrapper.find(Button).text()).toBe('profile.report.activate')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    
      it('should update the profile with sendAnalysisNotification to true', () => {
        const wrapper = mount(
          <Provider store={store}>
            <ReportOptions />
          </Provider>
        )
        wrapper
    
          .find(Button)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          .first()
    
          .simulate('click')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        expect(updateProfileSpy).toBeCalledTimes(1)
        expect(updateProfileSpy).toHaveBeenCalledWith({
          sendAnalysisNotification: true,
        })
      })
    
      it('should be rendered with sendAnalysisNotification true and toggle it to false', () => {
        mockInitialEcolyoState.profile.sendAnalysisNotification = true
        store = createMockStore(mockInitialEcolyoState)
    
        const wrapper = mount(
          <Provider store={store}>
            <ReportOptions />
          </Provider>
        )
        wrapper
          .find(Button)
          .first()
          .simulate('click')
        expect(updateProfileSpy).toBeCalledTimes(1)
        expect(updateProfileSpy).toHaveBeenCalledWith({
          sendAnalysisNotification: false,
        })
      })
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })