Skip to content
Snippets Groups Projects
ActionModal.spec.tsx 2.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Button } from '@material-ui/core'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { mount } from 'enzyme'
    
    import toJson from 'enzyme-to-json'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { Provider } from 'react-redux'
    
    import UsageEventService from 'services/usageEvent.service'
    
    import * as challengeActions from 'store/challenge/challenge.slice'
    
    import { defaultEcogestureData } from 'tests/__mocks__/actionData.mock'
    import { createMockEcolyoStore } from 'tests/__mocks__/store'
    import { waitForComponentToPaint } from 'tests/__mocks__/testUtils'
    import { userChallengeData } from 'tests/__mocks__/userChallengeData.mock'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import ActionModal from './ActionModal'
    
    
    const mockUpdateUserChallenge = jest.fn()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    jest.mock('services/challenge.service', () => {
    
      return jest.fn(() => ({
        updateUserChallenge: mockUpdateUserChallenge,
      }))
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    })
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    jest.mock('services/usageEvent.service')
    const mockAddEvent = jest.fn()
    UsageEventService.addEvent = mockAddEvent
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    describe('ActionModal component', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const store = createMockEcolyoStore()
    
      it('should render correctly', async () => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        const wrapper = mount(
          <Provider store={store}>
            <ActionModal
    
    Yoan VALLET's avatar
    Yoan VALLET committed
              open={true}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              handleCloseClick={jest.fn()}
              action={defaultEcogestureData[1]}
              userChallenge={userChallengeData[1]}
            />
          </Provider>
        )
    
        await waitForComponentToPaint(wrapper)
        expect(toJson(wrapper)).toMatchSnapshot()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      })
      it('should click on button and update action to ongoing', async () => {
        const updateChallengeSpy = jest.spyOn(
          challengeActions,
          'updateUserChallengeList'
        )
    
        mockUpdateUserChallenge.mockResolvedValueOnce(userChallengeData[1])
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
        const wrapper = mount(
          <Provider store={store}>
            <ActionModal
    
    Yoan VALLET's avatar
    Yoan VALLET committed
              open={true}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              handleCloseClick={jest.fn()}
              action={defaultEcogestureData[1]}
              userChallenge={userChallengeData[1]}
            />
          </Provider>
        )
    
        wrapper.find(Button).first().simulate('click')
    
        await waitForComponentToPaint(wrapper)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        expect(updateChallengeSpy).toBeCalledTimes(1)
      })
    })