Skip to content
Snippets Groups Projects
QuizQuestionContentCustom.spec.tsx 2.32 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { render, screen } from '@testing-library/react'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { userEvent } from '@testing-library/user-event'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import React from 'react'
    
    import { Provider } from 'react-redux'
    
    import { questionEntity } from 'tests/__mocks__/quizData.mock'
    import { createMockEcolyoStore } from 'tests/__mocks__/store'
    import { userChallengeData } from 'tests/__mocks__/userChallengeData.mock'
    import QuizQuestionContentCustom from './QuizQuestionContentCustom'
    
    
    const mockHistoryPush = jest.fn()
    jest.mock('react-router-dom', () => ({
      ...jest.requireActual('react-router-dom'),
    
      useNavigate: () => ({
    
        push: mockHistoryPush,
      }),
    }))
    
    const mockUpdateUserQuiz = jest.fn()
    jest.mock('services/quiz.service', () => {
    
      return jest.fn(() => ({
        updateUserQuiz: mockUpdateUserQuiz,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        getCustomQuestion: jest.fn(),
    
    const mockUpdateUserChallenge = jest.fn()
    
    jest.mock('services/challenge.service', () => {
    
      return jest.fn(() => ({
    
        updateUserChallenge: mockUpdateUserChallenge,
    
    })
    
    describe('QuizCustomQuestionContent component', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const store = createMockEcolyoStore()
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      it('should be rendered correctly', async () => {
    
          <Provider store={store}>
    
            <QuizQuestionContentCustom
    
              userChallenge={userChallengeData[0]}
    
              customQuestion={questionEntity}
    
              goBack={mockHistoryPush('/challenges')}
            />
          </Provider>
        )
    
        await userEvent.click(screen.getAllByRole('button')[0])
    
        expect(mockHistoryPush).toHaveBeenCalledWith('/challenges')
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(screen.getByText(questionEntity.questionLabel)).toBeInTheDocument()
        expect(screen.getAllByRole('radio').length).toBe(3)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      it('should display QuizExplanationModal when click on Button', async () => {
    
        mockUpdateUserChallenge.mockResolvedValue(userChallengeData[0])
    
          <Provider store={store}>
    
            <QuizQuestionContentCustom
    
              userChallenge={userChallengeData[0]}
    
              customQuestion={questionEntity}
    
              goBack={mockHistoryPush('/challenges')}
            />
          </Provider>
        )
    
        await userEvent.click(screen.getAllByRole('radio')[0])
        await userEvent.click(screen.getByText('quiz.button_validate'))
    
        expect(mockUpdateUserQuiz).toHaveBeenCalledWith(
          userChallengeData[0].quiz,
          questionEntity.answers[0].isTrue
        )
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        expect(screen.getByRole('dialog')).toBeInTheDocument()