import addRecordActionCreatorCheck from '../../../../tests/util/addRecordActionCreatorCheck'
import Fetcher from '../../util/Fetcher'
import { jsoniblePromise } from '../../util/jsonify'

import makeFakeFetch from '../../../../tests/util/fakeFetch'

import { ADD_QUIZ_SESSIONS, ACTIVE_QUIZ_SESSION_CHANGE } from '@instructure/quiz-common/lib/constants'
import {
  addQuizSessions,
  getSessionItems,
  getSessionItemResults,
  getQuizSessionResult,
  getResultInfo,
  newActiveQuizSession
} from '../quizSessions'

describe('adding a QuizSession action creators', () => {
  addRecordActionCreatorCheck(
    'QuizSession',
    addQuizSessions,
    ADD_QUIZ_SESSIONS
  )
})

it('returns a well-formed activeQuizSessionId change action', () => {
  const newActiveQuizSessionAction = newActiveQuizSession('21')

  expect(newActiveQuizSessionAction)
    .to.deep.equal({
      type: ACTIVE_QUIZ_SESSION_CHANGE,
      payload: '21'
    })
})

describe('action calls for quizSession', () => {
  const dispatchStub = sinon.stub()
  const getStateStub = sinon.stub()
  const fetchStub = sinon.stub()
  useFakeFetching({dispatchStub, getStateStub, fetchStub})

  const actsAsAction = (action, params) => {
    it('calls the given api action', () => {
      const apiActionStub = sinon.stub()
      action(...params, apiActionStub)()
      expect(apiActionStub.called).to.be.true()
    })

    it('dispatches the action', async () => {
      Fetcher.fakeFetch = makeFakeFetch(jsoniblePromise([]))
      await action(...params)(dispatchStub, getStateStub)
      expect(dispatchStub.called).to.be.true()
    })
  }

  describe('getSessionItems', () => {
    actsAsAction(getSessionItems, [1])
  })

  describe('getSessionItemResults', () => {
    actsAsAction(getSessionItemResults, [1, 2])
  })

  describe('getQuizSessionResult', () => {
    actsAsAction(getQuizSessionResult, [1, 2])
  })

  describe('getResultInfo', () => {
    actsAsAction(getResultInfo, [1])
  })
})
