import QuizEntry from '../../../../../common/records/QuizEntry'

import MoveImmutableModal from '../presenter'
import ConnectedMoveImmutableModal from '../index'

describe('MoveImmutableModal', () => {
  const closeModalStub = sinon.stub()
  const setUIStub = sinon.stub()
  const moveImmutableQuizEntryStub = sinon.stub().resolves()
  const quizEntry = new QuizEntry({ id: '42' })

  const defaultProps = {
    closeModal: closeModalStub,
    modalOpen: true,
    moveImmutableQuizEntry: moveImmutableQuizEntryStub,
    position: 5,
    quizEntry: quizEntry,
    quizId: '1',
    setUI: setUIStub,
    stimulusQuizEntryId: '7'
  }

  const testbed = createTestbed(withContext(MoveImmutableModal), defaultProps)

  shouldRenderComponent(
    testbed, {}, MoveImmutableModal
  )

  afterEach(() => {
    setUIStub.resetHistory()
    closeModalStub.resetHistory()
    moveImmutableQuizEntryStub.resetHistory()
  })

  const submit = () => {
    const button = window.document.querySelectorAll('button')[2]
    expect(button.innerText).to.eq('Clone and Move')
    button.click()
  }

  const close = () => {
    const button = window.document.querySelectorAll('button')[1]
    expect(button.innerText).to.include('Cancel')
    button.click()
  }

  context('#handleSubmit', () => {
    it('calls all the right functions', (done) => {
      testbed.render()
      submit()
      setTimeout(() => {
        expect(closeModalStub).to.be.called()
        expect(moveImmutableQuizEntryStub).to.be.called()
        expect(setUIStub).to.be.called()
        done()
      })
    })

    it('calls moveImmutableQuizEntry with right args', (done) => {
      testbed.render()
      submit()
      setTimeout(() => {
        expect(moveImmutableQuizEntryStub).to.be.calledWith('1', 5, '7', quizEntry.id)
        done()
      })
    })
  })

  context('#closeAction', () => {
    it('closes the modal', () => {
      testbed.render()
      close()
      expect(closeModalStub).to.be.called()
    })

    it('does not move the item', () => {
      testbed.render()
      close()
      expect(moveImmutableQuizEntryStub).not.to.have.been.called()
    })
  })

  describe('a11y tests', () => {
    it('should meet a11y standards', (done) => {
      testbed.render()
      testbed.checkA11yStandards(done, { ignores: ['label', 'label-title-only'] })
    })
  })

  describe('test with connect', () => {
    const connectedTestbed = createTestbed(
      (withContext(ConnectedMoveImmutableModal)),
      defaultProps
    )

    shouldRenderComponent(
      connectedTestbed, {}, MoveImmutableModal
    )
  })
})
