import keys from 'lodash/keys'

import makeEditable from '../makeEditable'
import { startEdit, endEdit } from '../../../../actions/editing'
import ComposedComponent from '../../../../../../tests/util/PlainComponent'

describe('makeEditable', () => {
  const fakeGuid = 'fakeGUIDValue'
  const dispatchStub = sinon.stub()

  const EditableComponent = makeEditable(ComposedComponent)
  const EditableComponentWithContext = withContext(EditableComponent, {dispatchStub})

  const testbed = createTestbed(
    EditableComponentWithContext, {guid: fakeGuid}
  )

  const getComponents = (testbed) => {
    const composedComponent = testbed.findChildByType(ComposedComponent)
    const editableComponent = testbed.findChildByType(EditableComponent)
    return [composedComponent, editableComponent]
  }

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

  it('should render a child component', () => {
    testbed.render()
    const composedComponent = getComponents(testbed)[0]

    expect(composedComponent).to.not.be.undefined()
  })

  it('the composed component has the proper state passed via props', () => {
    testbed.render()
    const composedComponent = getComponents(testbed)[0]

    expect(keys(composedComponent.props)).to.include('isEditing')
  })

  it('the composed component has the proper actions', () => {
    testbed.render()
    const composedComponent = getComponents(testbed)[0]

    expect(keys(composedComponent.props)).to.include('switchOnEditing')
    expect(keys(composedComponent.props)).to.include('switchOffEditing')
  })

  it('switchOnEditing props dispatches well-formed event', () => {
    testbed.render()

    const [composedComponent] = getComponents(testbed)
    const startAction = startEdit(fakeGuid)

    expect(dispatchStub.calledWith(startAction)).to.be.false()
    composedComponent.props.switchOnEditing()
    expect(dispatchStub.calledWith(startAction)).to.be.true()
  })

  it('switchOffEditing props dispatches well-formed event', () => {
    testbed.render()

    const [composedComponent] = getComponents(testbed)
    const endAction = endEdit(fakeGuid)

    expect(dispatchStub.calledWith(endAction)).to.be.false()
    composedComponent.props.switchOffEditing()
    expect(dispatchStub.calledWith(endAction)).to.be.true()
  })

  context('with editing true', () => {
    const ComponentWithEditingOn = withContext(EditableComponent, {editing: {foo: true}})
    const testbedWithEditing = createTestbed(
      ComponentWithEditingOn, {guid: fakeGuid}
    )

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