import { fromJS, Map } from 'immutable'

import {
  CREATE_QTI_IMPORT,
  FILE_UPLOAD_URL_CALL,
  FILE_UPLOAD_CALL
} from '@instructure/quiz-common/lib/constants'

import QTIImportModal, { mapStateToProps, mapDispatchToProps } from '../index'
import { progressValue } from '../../../../common/components/ImportModal'

const calls = (call1 = 'success', call2 = 'success', call3 = 'success') => {
  return fromJS({
    [CREATE_QTI_IMPORT]: { requestStatus: call1 },
    [FILE_UPLOAD_URL_CALL]: { requestStatus: call2 },
    [FILE_UPLOAD_CALL]: { requestStatus: call3 }
  })
}
const callsToCheck = [CREATE_QTI_IMPORT, FILE_UPLOAD_CALL, FILE_UPLOAD_URL_CALL]

const qtiRecordStub = (pvNum) => ({ progressNumber: () => pvNum, message: 'hi' })

describe('QTIImportModal', () => {
  describe('.mapStateToProps', () => {
    it('returns the expected props', () => {
      const props = mapStateToProps({get: () => Map()}, {qtiImport: qtiRecordStub(0)})
      expect(props).to.deep.equal({
        acceptFileType: '.zip',
        failureMessage: 'hi',
        importMessage: 'QTI 1.2 and 2.x (.zip) files only',
        importModalId: 'qtiImport',
        progressValue: progressValue(
          qtiRecordStub(0).progressNumber(),
          calls(false, false, false),
          callsToCheck
        )
      })
    })
  })

  describe('.mapDispatchToProps', () => {
    it('returns the correct props', () => {
      const dispatch = sinon.stub()
      const mdtp = mapDispatchToProps(dispatch, {parentId: 1, parentType: 'Bank'})
      expect(mdtp.fileUpload).to.be.a('function')
      expect(mdtp.onContinue).to.be.a('function')
    })
  })

  describe('QTIImportModal Rendering', () => {
    const testbed = createTestbed(withContext(QTIImportModal), {qtiImport: qtiRecordStub(0)})
    shouldRenderComponent(testbed, {}, QTIImportModal)
  })
})
