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

import HeaderMenuButton, { mapStateToProps, mapDispatchToProps } from '../index'

import noop from '../../../../../util/noop'
import { toggleSidebar } from '../../../../../actions/sidebar'

import styles from '../styles.css'

describe('HeaderMenuButton', () => {
  describe('mapStateToProps', () => {
    context('on take', () => {
      context('with no backtracking', () => {
        it('hides the menu button', () => {
          const fakeState = fromJS({
            quizSessions: {
              activeQuizSessionId: '1',
              '1': {
                oneAtATimeType: 'question',
                allowBacktracking: false
              }
            }
          })

          const props = mapStateToProps(fakeState, {onTake: true})
          expect(props.hidden).to.be.true()
        })
      })

      context('with backtracking', () => {
        it('shows the menu button', () => {
          const fakeState = fromJS({
            quizSessions: {
              activeQuizSessionId: '1',
              '1': {
                oneAtATimeType: 'question',
                allowBacktracking: true
              }
            }
          })

          const props = mapStateToProps(fakeState, {onTake: true})
          expect(props.hidden).to.be.false()
        })
      })

      context('with an unloaded quizSession', () => {
        it('hides the menu button', () => {
          const fakeState = fromJS({
            quizSessions: {
              activeQuizSessionId: '0'
            }
          })

          const props = mapStateToProps(fakeState, {onTake: true})
          expect(props.hidden).to.be.true()
        })
      })
    })

    context('not on take', () => {
      context('with an unloaded quizSession', () => {
        it('shows the menu button', () => {
          const fakeState = fromJS({
            quizSessions: {
              activeQuizSessionId: '0'
            }
          })

          const props = mapStateToProps(fakeState, {onTake: false})
          expect(props.hidden).to.be.false()
        })
      })
    })
  })

  describe('mapDispatchToProps', () => {
    const dispatch = sinon.spy()

    afterEach(() => dispatch.resetHistory())

    context('when component receives sidebarMenuClick prop', () => {
      it('passes sidebarMenuClick prop through', () => {
        const props = { sidebarMenuClick: () => {} }
        expect(mapDispatchToProps(dispatch, props)).to.deep.equal(props)
      })
    })

    context('when component does not receive sidebarMenuClick prop', () => {
      it('uses toggleSidebar action creator', () => {
        mapDispatchToProps(dispatch, {}).sidebarMenuClick()
        expect(dispatch).to.have.been.calledWith(toggleSidebar())
      })
    })
  })

  const testbed = createTestbed(
    withContext(HeaderMenuButton, {
      quizSessions: {
        activeQuizSessionId: '1',
        '1': {
          oneAtATimeType: 'question',
          allowBacktracking: false
        }
      }
    }), {sidebarMenuClick: noop}
  )

  it('should toggle the menu if the button is clicked', () => {
    const toggleMenuSpy = sinon.spy()
    testbed.render({
      sidebarMenuClick: toggleMenuSpy
    })

    const menuButton = testbed.findChildrenByClass(styles.menuButton)[0]
    ReactTestUtils.Simulate.click(menuButton)

    expect(toggleMenuSpy).to.have.been.calledOnce()
  })

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