import ReactTestUtils from 'react-dom/test-utils'
import { fromJS } from 'immutable'

import QuizEntryRecord from '../../../../../records/QuizEntry'
import StimulusRecord from '../../../../../records/Stimulus'

import Stimulus from '../presenter'
import Collection from '../Collection'
import Title from '../Title'
import Summary from '../Summary'

describe('Common Stimulus Sidebar Presenter', () => {
  const stimulus = new StimulusRecord({title: '<div>Hello</div>'})
  stimulus.items = fromJS([{}, {}, {}])
  const renderSidebarItemStub = sinon.stub()
  const scrollToItemStub = sinon.stub()
  const stimulusParent = new QuizEntryRecord({ id: '5' })
  sinon.stub(stimulusParent, 'getEntry').returns(stimulus)

  const props = {
    childEntries: fromJS([]),
    displayPosition: 1,
    isDragging: false,
    isShowingItems: false,
    itemId: '1',
    sidebarOpen: true,
    renderSidebarItem: renderSidebarItemStub,
    scrollToItem: scrollToItemStub,
    stimulus,
    stimulusParent: stimulusParent
  }

  afterEach(() => {
    renderSidebarItemStub.resetHistory()
  })

  const testbed = createTestbed(withContext(Stimulus), props)

  shouldRenderComponent(testbed, {}, Stimulus)

  describe('#scrollToItem', () => {
    it('should call scrollToItem with the quizEntry id', () => {
      testbed.render()
      const sidebarItem = testbed.findChildren(el => {
        return el.type === 'button' && testbed.getText(el).includes('Scroll to stimulus')
      })[0]
      testbed.click(sidebarItem)
      expect(scrollToItemStub).to.be.calledWith('5')
    })

    it('should call scrollToItem with the quizEntry id on space or enter', () => {
      testbed.render()
      const sidebarItem = testbed.findChildren(el => {
        return el.type === 'button' && testbed.getText(el).includes('Scroll to stimulus')
      })[0]
      ReactTestUtils.Simulate.keyDown(sidebarItem, { keyCode: 13 })
      expect(scrollToItemStub).to.be.calledWith('5')
    })
  })

  describe('#renderTitle', () => {
    it('renders a title', () => {
      testbed.render()
      expect(testbed.findChildrenByType(Title)).to.not.be.empty()
    })
  })

  describe('#renderCollection', () => {
    context('isDragging', () => {
      it('does not render the collection', () => {
        testbed.render({isDragging: true})
        expect(testbed.findChildrenByType(Collection)).to.be.empty()
      })
    })

    it('renders the collection', () => {
      testbed.render({isDragging: false})
      expect(testbed.findChildrenByType(Collection)).to.not.be.empty()
    })
  })

  describe('#renderSummary', () => {
    it('renders the summary', () => {
      testbed.render()
      expect(testbed.findChildrenByType(Summary)).to.not.be.empty()
    })
  })

  describe('a11y tests', () => {
    it('should meet a11y standards', (done) => {
      testbed.render()
      testbed.checkA11yStandards(done)
    })
  })
})
