import LearnosityCalculator from 'quiz-interactions/lib/components/common/components/LearnosityCalculator'
import Calculator, { mapDispatchToProps } from '../index'
import withContext from '../../../../../../../tests/util/withContext'
import {authenticateLearnosity} from '../../../../../../taking/api/taking'
import {setFakeStore, unsetFakeStore} from '../../../../../../reduxStore'
import {fromJS} from 'immutable'
import { setActiveCalculatorId } from '../../../../../../common/actions/ui'

describe('Calculator', () => {
  const calculatorType = 'basic'
  const activeQuizId = '42'
  const quizSessionId = 24
  const activeCalculatorId = 'abc'
  const state = {
    quizzes: {
      activeQuizId,
      [activeQuizId]: {
        id: activeQuizId,
        calculatorType
      }
    },
    quizSessions: {
      activeQuizSessionId: quizSessionId
    },
    ui: {
      activeCalculatorId
    }
  }

  const testbed = createTestbed(withContext(Calculator, state))

  beforeEach(() => {
    setFakeStore(fromJS(state))
  })

  afterEach(() => {
    unsetFakeStore()
  })

  it('correctly maps props from state', async () => {
    testbed.render()

    const learnosityCalculator = testbed.findChildByType(LearnosityCalculator)
    expect(learnosityCalculator.props).to.contain({
      activeCalculatorId,
      calculatorType,
      quizSessionId
    })
  })

  describe('mapDispatchToState', () => {
    it('correctly passes authenticateLearnosity', async () => {
      const props = mapDispatchToProps(sinon.stub())
      expect(props.authenticateLearnosity).to.eq(authenticateLearnosity)
    })

    it('correctly maps setUI to setActiveCalculatorId', () => {
      const dispatch = sinon.stub()
      const props = mapDispatchToProps(dispatch)
      props.setActiveCalculatorId('abc')
      expect(dispatch).to.have.been.called.with(setActiveCalculatorId('abc'))
    })
  })
})
