import ReactDOM from 'react-dom'
import ReactTestUtils from 'react-dom/test-utils'

import Button from '@instructure/ui-buttons/lib/components/Button'
import IconPinLine from '@instructure/ui-icons/lib/Line/IconPin'
import IconPinSolid from '@instructure/ui-icons/lib/Solid/IconPin'

import Pin from '../index'

describe('Pin', () => {
  const pinSessionItemStub = sinon.stub()
  const props = {
    pinSessionItem: pinSessionItemStub,
    isPinned: true,
    position: 1
  }

  const testbed = createTestbed(Pin, props)

  describe('pin', () => {
    const pinSessionItemStub = sinon.stub()
    afterEach(() => {
      pinSessionItemStub.resetHistory()
    })

    context('pinning the item', () => {
      it('calls pinSessionItem', () => {
        testbed.render({
          pinSessionItem: pinSessionItemStub
        })

        const pinBtn = ReactDOM.findDOMNode(testbed.findChildByType(Button))
        ReactTestUtils.Simulate.click(pinBtn)

        expect(pinSessionItemStub.called).to.be.true()
      })

      it('stops the click from propagating', () => {
        testbed.render({
          pinSessionItem: pinSessionItemStub
        })
        const stopPropagationStub = sinon.stub()

        const pinBtn = ReactDOM.findDOMNode(testbed.findChildByType(Button))
        ReactTestUtils.Simulate.click(pinBtn, { stopPropagation: stopPropagationStub })

        expect(stopPropagationStub.called).to.be.true()
      })
    })

    context('isPinned is true', () => {
      it('shows the line icon', () => {
        testbed.render({isPinned: true})
        const solidPin = testbed.findChildrenByType(IconPinSolid)[0]
        expect(solidPin).not.to.be.undefined()
      })
    })

    context('isPinned is false', () => {
      it('shows the line icon', () => {
        testbed.render({isPinned: false})
        const emptyPin = testbed.findChildrenByType(IconPinLine)[0]
        expect(emptyPin).not.to.be.undefined()
      })
    })
  })

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