Skip to content
Snippets Groups Projects
TermsView.spec.tsx 2.56 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { Button } from '@material-ui/core'
    
    import { mount } from 'enzyme'
    
    import toJson from 'enzyme-to-json'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import * as reactRedux from 'react-redux'
    
    import { Provider } from 'react-redux'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { createMockEcolyoStore } from '../../../tests/__mocks__/store'
    
    import { mockUpToDateTerm } from '../../../tests/__mocks__/termsData.mock'
    import TermsView from './TermsView'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    jest.mock('cozy-ui/transpiled/react/I18n', () => ({
      useI18n: jest.fn(() => ({
        t: (str: string) => str,
      })),
    }))
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    const mockCreateTerm = jest.fn()
    
    const mockGetTermsVersionType = jest.fn()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    jest.mock('services/terms.service', () => {
      return jest.fn(() => {
        return {
          createTerm: mockCreateTerm,
    
          getTermsVersionType: mockGetTermsVersionType,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
      })
    })
    
    const mockedNavigate = jest.fn()
    jest.mock('react-router-dom', () => ({
      ...jest.requireActual('react-router-dom'),
      useNavigate: () => mockedNavigate,
    }))
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    const mockUseDispatch = jest.spyOn(reactRedux, 'useDispatch')
    
    const mockUpdateProfile = jest.fn()
    jest.mock('services/profile.service', () => {
      return jest.fn(() => {
        return {
          updateProfile: mockUpdateProfile,
        }
      })
    })
    
    describe('TermsView component', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const store = createMockEcolyoStore()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      it('should valid checkboxes and valid consent', () => {
        mockCreateTerm.mockResolvedValueOnce(mockUpToDateTerm)
        const wrapper = mount(
          <Provider store={store}>
            <TermsView />
          </Provider>
        )
    
        wrapper
          .find('input')
          .at(0)
          .simulate('change', { target: { checked: true } })
    
        expect(wrapper.find('input').at(0).props().checked).toEqual(true)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        wrapper
          .find('input')
          .at(1)
          .simulate('change', { target: { checked: true } })
    
        expect(wrapper.find('input').at(1).props().checked).toEqual(true)
        expect(wrapper.find(Button).first().hasClass('disabled')).toBeFalsy()
        wrapper.find(Button).first().simulate('click')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        expect(mockUseDispatch).toHaveBeenCalledTimes(3)
      })
      it('should be rendered correctly', () => {
        const component = mount(
          <Provider store={store}>
            <TermsView />
          </Provider>
    
        )
        expect(toJson(component)).toMatchSnapshot()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      })
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      it('should be unable to valid consent', async () => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        mockCreateTerm.mockResolvedValueOnce(mockUpToDateTerm)
        const wrapper = mount(
          <Provider store={store}>
            <TermsView />
          </Provider>
        )
    
        wrapper.find(Button).first().simulate('click')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
        expect(wrapper.find(Button).first().hasClass('disabled')).toBeTruthy()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        expect(mockUpdateProfile).toHaveBeenCalledTimes(0)
      })
    })