import {
  CLOSE_MODAL,
  OPEN_MODAL,
  OPEN_DELETE_STIMULUS_MODAL,
  OPEN_ADD_CONTENT_MODAL
} from '@instructure/quiz-common/lib/constants'

import {
  closeModal,
  openModal,
  withConfirm,
  openAddContentModal,
  openDeleteStimulusModal
} from '../modal'

describe('modal action creators', () => {
  describe('withConfirm', () => {
    it('returns sane default options', () => {
      const cb = () => {}
      const opts = {text: 'Are you sure?'}
      const withConfirmAction = withConfirm(cb, opts)

      expect(withConfirmAction.payload.options.confirmation).to.be.equal(true)
      expect(withConfirmAction.payload.options.onContinue).to.be.equal(cb)
      expect(withConfirmAction.payload.options.text).to.be.equal('Are you sure?')
      expect(withConfirmAction.payload.options.title).to.be.undefined()
      expect(withConfirmAction.payload.options.onCancel).to.be.undefined()
      expect(withConfirmAction.payload.options.continueText).to.be.undefined()
      expect(withConfirmAction.payload.options.cancelText).to.be.undefined()
    })

    it('overrides default options', () => {
      const cb = () => {}
      const opts = {
        text: 'This overrides the default',
        onCancel: cb
      }
      const withConfirmAction = withConfirm(cb, opts)

      expect(withConfirmAction.payload.options.confirmation).to.be.equal(true)
      expect(withConfirmAction.payload.options.text).to.be.equal('This overrides the default')
      expect(withConfirmAction.payload.options.onCancel).to.be.equal(cb)
    })

    it('returns a properly typed action', () => {
      const cb = () => {}
      const withConfirmAction = withConfirm(cb, {})
      expect(withConfirmAction.type).to.be.equal(OPEN_MODAL)
    })
  })

  describe('openDeleteStimulusModal', () => {
    it('returns a properly types action', () => {
      const openDeleteStimulusModalAction = openDeleteStimulusModal({x: 1})
      expect(openDeleteStimulusModalAction.type).to.be.equal(OPEN_DELETE_STIMULUS_MODAL)
      expect(openDeleteStimulusModalAction.payload.entry).to.be.eql({x: 1})
    })
  })

  describe('openAddContentModal', () => {
    it('returns a properly types action', () => {
      const openAddContentModalAction = openAddContentModal({
        allowStimulusCreation: true,
        parentEntryId: 'parent123',
        position: 56,
        quizId: 'quiz123',
        stimulusId: 'stimulus123'
      })
      expect(openAddContentModalAction.type).to.be.equal(OPEN_ADD_CONTENT_MODAL)
      expect(openAddContentModalAction.payload).to.be.eql({
        allowStimulusCreation: true,
        parentEntryId: 'parent123',
        position: 56,
        quizId: 'quiz123',
        stimulusId: 'stimulus123'
      })
    })
  })

  describe('closeModal', () => {
    it('returns a properly typed action', () => {
      const closeModalAction = closeModal()
      expect(closeModalAction.type).to.be.equal(CLOSE_MODAL)
    })
  })

  describe('openModal', () => {
    it('returns a properly typed action', () => {
      const openModalAction = openModal()
      expect(openModalAction.type).to.be.equal(OPEN_MODAL)
    })
  })
})
