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

import {
  REGRADE_MODAL
} from '@instructure/quiz-common/lib/constants'

import { createQuizEntryRegrade } from '../quizEntryRegrades'
import { closeModal } from '../../../common/actions/modal'

describe('quizEntryRegrades', () => {
  describe('createQuizEntryRegrade', () => {
    const fetchStub = sinon.stub()
    const dispatchStub = sinon.stub()
    const getStateStub = sinon.stub()
    const quizId = '1'
    const payload = {
      itemId: '3',
      quizEntryId: '2',
      pointsPossible: 1.0,
      scoreReconciliation: 'new_score',
      scoringAlgorithm: 'Equivalence',
      scoringData: { value: true }
    }

    useFakeFetching({dispatchStub, getStateStub, fetchStub})

    it('hits the correct url', () => {
      const expectedUrl = `/api/quizzes/${quizId}/quiz_entry_regrades`
      Fetcher.fakeFetch = fetchStub

      createQuizEntryRegrade(quizId, payload)(dispatchStub, getStateStub)
      expect(fetchStub).to.have.been.calledWith(expectedUrl)
    })

    it('closes the modal after creation', (done) => {
      const response = []
      const fakeFetch = makeFakeFetch(jsoniblePromise(response))
      Fetcher.fakeFetch = fakeFetch

      const fetchPromise = createQuizEntryRegrade(
        quizId, payload
      )(dispatchStub, getStateStub)
      fetchPromise.then(() => {
        expect(dispatchStub).to.have.been.calledWith(closeModal(REGRADE_MODAL))
        done()
      })
    })
  })
})
