import {fromJS, Map} from 'immutable'
import Item from '../../../../../records/Item'

import ItemEdit, { mapStateToProps } from '../index'
import {withTestFeatures} from '../../../../../util/featureCheck'
import {setFakeStore, unsetFakeStore} from '../../../../../../reduxStore'

describe('ItemEdit', () => {
  const ConnectedItemEdit = withContext(ItemEdit)
  const item = new Item({
    id: '666',
    title: 'Tough Color Question',
    itemBody: 'Which color is happiest?',
    scoringData: {
      value: 'id'
    }
  })
  sinon.stub(item, 'getWorkingInstance').returns(item)
  const defaultProps = {
    entry: item,
    errorsShowing: Map(),
    isValid: true,
    isEditing: true,
    editable: true,
    errors: {},
    stemErrors: {},
    handleOutcomeAlignmentSetChange: sinon.stub()
  }
  const testbed = createTestbed(ConnectedItemEdit, defaultProps)

  useFakeFetching({dispatchStub: sinon.stub(),
    getStateStub: sinon.stub(),
    fetchStub: sinon.stub()})

  shouldRenderComponent(testbed, {}, ItemEdit)

  describe('mapStateToProps', () => {
    const state = fromJS({
      outcomes: {
        contextUuid: 'from state'
      },
      quizzes: {
        activeQuizId: '5',
        5: {
          id: '5',
          oneAtATimeType: 'question'
        }
      }
    })

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

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

    withTestFeatures(['calculator_per_question'])

    it('uses showCalculator value from state', () => {
      const newProps = mapStateToProps(state, defaultProps)
      expect(newProps.showCalculator).to.be.true()
    })

    it('uses oneQuestionAtATime value from state', () => {
      const newProps = mapStateToProps(state, defaultProps)
      expect(newProps.oneQuestionAtATime).to.be.true()
    })

    it('uses contextUuid value from state', () => {
      const newProps = mapStateToProps(state, defaultProps)
      expect(newProps.contextUuid).to.equal('from state')
    })

    it('allows overriding contextUuid via props', () => {
      const props = { ...defaultProps, contextUuid: 'from props' }
      const newProps = mapStateToProps(state, props)
      expect(newProps.contextUuid).to.equal('from props')
    })

    it('allows overriding contextUuid with null', () => {
      const props = { ...defaultProps, contextUuid: null }
      const newProps = mapStateToProps(state, props)
      expect(newProps.contextUuid).to.equal(null)
    })
  })
})
