
import Fetcher from '../../../common/util/Fetcher'
import makeFakeFetch from '../../../../tests/util/fakeFetch'
import {jsoniblePromise} from '../../../common/util/jsonify'

import { handleQuizAnalysisResponse } from '../../../common/api/callHandlers'

import { getQuizAnalysis } from '../quizAnalyses'

describe('reporting api actions', () => {
  const quizId = '456'

  const quizAnalysisData = [{quiz_id: quizId, quiz_analysis: {}}]

  const fetchStub = sinon.stub()
  const dispatchStub = sinon.stub()
  const dispatchStubIgnore = sinon.stub()
  const getStateStub = sinon.stub()

  useFakeFetching({dispatchStub, getStateStub, fetchStub})

  describe('getQuizAnalysis', () => {
    const stubbedGetQuizAnalysisCall = () => {
      const response = jsoniblePromise([quizAnalysisData])
      Fetcher.fakeFetch = makeFakeFetch(response)
      return getQuizAnalysis(quizId)(dispatchStub, getStateStub)
    }

    it('should hit the right endpoint', () => {
      Fetcher.fakeFetch = fetchStub
      getQuizAnalysis(quizId)(dispatchStubIgnore, getStateStub)
      expect(
        fetchStub.calledWith(`/api/quizzes/${quizId}/stats/quiz_analysis`)
      ).to.be.true()
    })

    it('should trigger additional actions', async () => {
      await stubbedGetQuizAnalysisCall()

      const expectedResponse = handleQuizAnalysisResponse([quizAnalysisData])
      checkActionDispatched(dispatchStub, expectedResponse[0])
    })
  })
})
