import { fromJS } from 'immutable'

import ActionButtons from '../presenter'
import DragHandle from '../../../../../common/components/shared/drag_and_drop/DragHandle'

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

import styles from '../styles.css'

describe('ActionButtons', () => {
  const qe1 = new QuizEntry()
  const qe2 = new QuizEntry()
  const props = {
    connectDragSource: sinon.stub(),
    displayPosition: 1,
    onDelete: sinon.stub(),
    quizEntries: fromJS([qe1, qe2]),
    quizEntry: qe1,
    switchOnEditing: sinon.stub()
  }
  const testbed = createTestbed(ActionButtons, props)

  beforeEach(() => {
    props.connectDragSource.resetHistory()
    props.onDelete.resetHistory()
    props.switchOnEditing.resetHistory()
  })

  function findButtons () {
    return testbed.findWhere(node => (node.name() || '').includes('Button'))
  }

  it('renders two buttons', () => {
    testbed.renderShallowly()
    expect(findButtons().length).to.eq(2)
  })

  it('renders the dragHandle', () => {
    testbed.renderShallowly({connectDragSource: (C) => C})
    expect(testbed.find(DragHandle).length).to.eq(1)
  })

  it('wraps the dragHandle in a dragSource', () => {
    testbed.renderShallowly()
    expect(props.connectDragSource.calledOnce).to.be.true()
  })

  it('hides the edit button when shouldRenderEdit is false', () => {
    testbed.renderShallowly({shouldRenderEdit: false})
    expect(testbed.find(`.${styles.hide}`)).to.not.be.empty()
  })

  it('calls the onDelete button when clicked', () => {
    testbed.renderShallowly()
    const deleteButton = findButtons().last()
    deleteButton.simulate('click')
    expect(props.onDelete.calledOnce).to.be.true()
  })

  it('calls the switchOnEditing button when clicked', () => {
    testbed.renderShallowly()
    const editButton = findButtons().first()
    editButton.simulate('click')
    expect(props.switchOnEditing.calledOnce).to.be.true()
  })

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