Skip to content
Snippets Groups Projects
ActionDone.spec.tsx 2 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Button } from '@material-ui/core'
    
    import { mount } from 'enzyme'
    
    import toJson from 'enzyme-to-json'
    
    import { Provider } from 'react-redux'
    
    import UsageEventService from 'services/usageEvent.service'
    
    import * as challengeActions from 'store/challenge/challenge.slice'
    
    import { createMockEcolyoStore } from 'tests/__mocks__/store'
    import { waitForComponentToPaint } from 'tests/__mocks__/testUtils'
    import { userChallengeData } from 'tests/__mocks__/userChallengeData.mock'
    
    import ActionDone from './ActionDone'
    
    
    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
    jest.mock('services/usageEvent.service')
    const mockAddEvent = jest.fn()
    UsageEventService.addEvent = mockAddEvent
    
    
    const mockedNavigate = jest.fn()
    
    jest.mock('react-router-dom', () => ({
      ...jest.requireActual('react-router-dom'),
    
      useNavigate: () => mockedNavigate,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    const mockupdateUserChallenge = jest.fn()
    jest.mock('services/challenge.service', () => {
      return jest.fn(() => {
        return {
          updateUserChallenge: mockupdateUserChallenge,
        }
      })
    })
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
    describe('ActionDone component', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const store = createMockEcolyoStore()
    
      it('should be rendered correctly', async () => {
    
        const wrapper = mount(
          <Provider store={store}>
            <ActionDone currentChallenge={userChallengeData[1]} />
          </Provider>
        )
    
        await waitForComponentToPaint(wrapper)
        expect(toJson(wrapper)).toMatchSnapshot()
    
      })
      it('should click on button and update action to done', async () => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        const updateChallengeSpy = jest.spyOn(
          challengeActions,
          'updateUserChallengeList'
        )
        mockupdateUserChallenge.mockResolvedValueOnce(userChallengeData[1])
    
        const wrapper = mount(
          <Provider store={store}>
            <ActionDone currentChallenge={userChallengeData[1]} />
          </Provider>
        )
    
        wrapper.find(Button).first().simulate('click')
    
        await waitForComponentToPaint(wrapper)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        expect(updateChallengeSpy).toBeCalledTimes(1)