Skip to content
Snippets Groups Projects
ChallengeCardUnlocked.spec.tsx 2.88 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { mount } from 'enzyme'
    import { Provider } from 'react-redux'
    import configureStore from 'redux-mock-store'
    
    import ChallengeCardUnlocked from './ChallengeCardUnlocked'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import { userChallengeData } from '../../../tests/__mocks__/userChallengeData.mock'
    import { globalStateData } from '../../../tests/__mocks__/globalStateData.mock'
    
    import ChallengeNoFluidModal from './ChallengeNoFluidModal'
    
    import { FluidType } from 'enum/fluid.enum'
    
    
    jest.mock('cozy-ui/transpiled/react/I18n', () => {
      return {
        useI18n: jest.fn(() => {
          return {
            t: (str: string) => str,
          }
        }),
      }
    })
    
    const mockStartUserChallenge = jest.fn()
    
    jest.mock('services/challenge.service', () => {
    
      return jest.fn(() => {
        return {
    
          startUserChallenge: mockStartUserChallenge,
    
        }
      })
    })
    
    const mockStore = configureStore([])
    
    describe('ChallengeCardUnlocked component', () => {
    
      it('should be rendered correctly', () => {
    
        const store = mockStore({
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          ecolyo: {
            global: globalStateData,
          },
    
        })
        const wrapper = mount(
          <Provider store={store}>
    
            <ChallengeCardUnlocked userChallenge={userChallengeData[0]} />
    
          </Provider>
        )
    
        expect(wrapper.find('.challengeTitle').text()).toEqual(
    
          userChallengeData[0].title
        )
    
        expect(wrapper.find('.btn-duel-active').exists()).toBeTruthy()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        expect(wrapper.find(ChallengeNoFluidModal).exists()).toBeTruthy()
        expect(wrapper.find(ChallengeNoFluidModal).prop('open')).toBeFalsy()
    
      it('should display ChallengeNoFluidModal when launching challenge without configured fluid', () => {
    
        const store = mockStore({
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          ecolyo: {
            global: globalStateData,
          },
    
        })
        const wrapper = mount(
          <Provider store={store}>
    
            <ChallengeCardUnlocked userChallenge={userChallengeData[0]} />
    
          </Provider>
        )
        wrapper
    
          .find('.btn-duel-active')
    
          .first()
          .simulate('click')
    
        expect(wrapper.find(ChallengeNoFluidModal).exists()).toBeTruthy()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        expect(wrapper.find(ChallengeNoFluidModal).prop('open')).toBeTruthy()
    
      it('should not display ChallengeNoFluidModal and update userChallenge when launching challenge with configured fluid', () => {
    
        const updateGlobalStoreData = {
          ...globalStateData,
          fluidTypes: [FluidType.ELECTRICITY],
    
        }
        const store = mockStore({
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          ecolyo: {
            global: updateGlobalStoreData,
          },
    
        })
        const wrapper = mount(
          <Provider store={store}>
    
            <ChallengeCardUnlocked userChallenge={userChallengeData[0]} />
    
          </Provider>
    
          .find('.btn-duel-active')
    
          .first()
          .simulate('click')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        expect(wrapper.find(ChallengeNoFluidModal).exists()).toBeTruthy()
        expect(wrapper.find(ChallengeNoFluidModal).prop('open')).toBeFalsy()
    
        expect(mockStartUserChallenge).toHaveBeenCalledWith(userChallengeData[0])