import {
  propsForInteractionEdit,
  propsForInteractionShow
} from '../interactionTypePropsHelper'

import Item from '../../records/Item'
import { PARENT_TYPE_QUIZ } from '@instructure/quiz-common/lib/constants'

function noop () {}

describe('interactionTypePropsHelpers', () => {
  describe('#propsForInteractionEdit', () => {
    const defaultInputProps = {
      workingItem: new Item({}),
      parentType: PARENT_TYPE_QUIZ,
      parentId: '2'
    }

    it('replaces null item bodies with an empty string', () => {
      const newProps = propsForInteractionEdit({
        itemBody: null,
        workingItem: {
          toJS: () => ({}),
          getErrors: () => ({toJS: () => ({})}),
          errorsFor: () => ({toJS: () => ({})})
        }
      })
      expect(newProps.itemBody).to.be.eql('')
    })

    describe('mediaUpload', () => {
      it('calls interactionFileUpload with the right arguments', () => {
        const interactionFileUploadStub = sinon.stub()
        const inputProps = Object.assign({},
          defaultInputProps,
          {interactionFileUpload: interactionFileUploadStub}
        )

        const returnedProps = propsForInteractionEdit(inputProps)

        returnedProps.mediaUpload('file', noop)

        expect(
          interactionFileUploadStub.calledWith(PARENT_TYPE_QUIZ, '2', 'file')
        ).to.be.true()
      })

      it('calls the callback in its onSuccess arg', () => {
        const clock = sinon.useFakeTimers()

        const execSuccess = (_parent, _id, _file, onSuccess) => {
          onSuccess('someReturnedUrl')
        }

        const inputProps = Object.assign({},
          defaultInputProps,
          {interactionFileUpload: execSuccess}
        )

        const returnedProps = propsForInteractionEdit(inputProps)
        const asyncSuccessCb = sinon.stub()

        returnedProps.mediaUpload('file', asyncSuccessCb)
        clock.tick(1200)

        expect(asyncSuccessCb.calledWith('someReturnedUrl')).to.be.true()
        clock.restore()
      })

      it('calls addError with the right args', () => {
        const execError = (_parent, _id, _file, _onSuccess, onError) => {
          onError()
        }
        const addErrorStub = sinon.stub()

        const inputProps = Object.assign({},
          defaultInputProps,
          {
            addError: addErrorStub,
            interactionFileUpload: execError
          }
        )

        const returnedProps = propsForInteractionEdit(inputProps)

        returnedProps.mediaUpload('file', noop)

        expect(
          addErrorStub.calledWith('There was an error uploading your file.')
        ).to.be.true()
      })
    })
  })

  describe('#propsForInteractionShow', () => {
    it('returns an object with the expected keys', () => {
      const exampleItem = {
        id: '1'
      }
      const returnedProps = propsForInteractionShow(exampleItem)
      const keySet = Object.keys(returnedProps).sort()

      const expectedKeys = [
        'itemId',
        'interactionData',
        'itemBody',
        'properties',
        'notifyScreenreader',
        'scoringData'
      ].sort()

      expect(keySet).to.be.eql(expectedKeys)
    })
  })

  it('replaces null item bodies with an empty string', () => {
    const newProps = propsForInteractionShow({itemBody: null})
    expect(newProps.itemBody).to.be.eql('')
  })
})
