Skip to content
Snippets Groups Projects
ChallengeCardDone.spec.tsx 3.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Button } from '@material-ui/core'
    
    import ChallengeCardDone from 'components/Challenge/ChallengeCardDone/ChallengeCardDone'
    
    import { mount } from 'enzyme'
    import toJson from 'enzyme-to-json'
    
    import React from 'react'
    
    import { Provider } from 'react-redux'
    import configureStore from 'redux-mock-store'
    
    import * as storeHooks from 'store/hooks'
    
    import { waitForComponentToPaint } from 'tests/__mocks__/testUtils'
    import { userChallengeData } from 'tests/__mocks__/userChallengeData.mock'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    
    const mockUpdateUserChallenge = jest.fn()
    jest.mock('services/challenge.service', () => {
    
      return jest.fn(() => ({
        updateUserChallenge: mockUpdateUserChallenge,
      }))
    
    })
    
    const mockStore = configureStore([])
    const mockDispatch = jest.fn()
    
    const mockAppDispatch = jest.spyOn(storeHooks, 'useAppDispatch')
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    describe('ChallengeCardDone component', () => {
    
      const storeNoCurrentChallenge = mockStore({
        ecolyo: {
          challenge: { currentChallenge: null },
        },
      })
      it('should be rendered correctly', async () => {
        const wrapper = mount(
          <Provider store={storeNoCurrentChallenge}>
            <ChallengeCardDone userChallenge={userChallengeData[0]} />
          </Provider>
        )
        await waitForComponentToPaint(wrapper)
        expect(toJson(wrapper)).toMatchSnapshot()
      })
    
      describe('Reset final challenge', () => {
        beforeEach(() => {
    
          jest.clearAllMocks()
    
        })
        it('should reset challenge if no other challenge is on going', async () => {
    
          mockAppDispatch.mockImplementationOnce(() => mockDispatch)
    
          const wrapper = mount(
            <Provider store={storeNoCurrentChallenge}>
              <ChallengeCardDone userChallenge={userChallengeData[0]} />
            </Provider>
          )
          wrapper.find(Button).last().simulate('click')
          await waitForComponentToPaint(wrapper)
    
          expect(mockDispatch).toHaveBeenCalledTimes(1)
          expect(mockDispatch).toHaveBeenCalledWith({
    
            type: 'challenge/updateUserChallengeList',
          })
    
          expect(mockUpdateUserChallenge).toHaveBeenCalledTimes(1)
    
        })
        it('should not reset challenge if another challenge is on going', async () => {
    
          mockAppDispatch.mockImplementationOnce(() => mockDispatch)
    
          const store = mockStore({
            ecolyo: {
              challenge: { currentChallenge: userChallengeData[1] },
            },
          })
          const wrapper = mount(
            <Provider store={store}>
              <ChallengeCardDone userChallenge={userChallengeData[0]} />
            </Provider>
          )
          wrapper.find(Button).last().simulate('click')
          await waitForComponentToPaint(wrapper)
    
          expect(mockDispatch).toHaveBeenCalledTimes(0)
          expect(mockUpdateUserChallenge).toHaveBeenCalledTimes(0)
    
        })
        it('should be primary button is challenge is lost', async () => {
          const wrapper = mount(
            <Provider store={storeNoCurrentChallenge}>
              <ChallengeCardDone userChallenge={userChallengeData[1]} />
            </Provider>
          )
          await waitForComponentToPaint(wrapper)
          const resetButton = wrapper.find('button').last()
          expect(resetButton.hasClass('btn-primary-challenge')).toBe(true)
        })
        it('should be secondary button is challenge is won', async () => {
          const wrapper = mount(
            <Provider store={storeNoCurrentChallenge}>
              <ChallengeCardDone userChallenge={userChallengeData[0]} />
            </Provider>
          )
          await waitForComponentToPaint(wrapper)
          const resetButton = wrapper.find('button').last()
          expect(resetButton.hasClass('btn-secondary-negative')).toBe(true)
        })
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      })
    })