import { fromJS } from 'immutable'
import { withTestFeatures } from '../../util/featureCheck'
import * as calculator from '../calculator'
import { setFakeStore } from '../../../reduxStore'

describe('calculator selectors', () => {
  const fakeState = fromJS({
    quizSessions: {
      activeQuizSessionId: '1',
      '1': {
        oneAtATimeType: 'question'
      }
    },
    taking: {
      currentSessionItemPosition: 3
    },
    sessionItems: {
      '1': {
        position: 1,
        quizSessionId: '1',
        calculatorType: 'none',
        id: '1'
      },
      '2': {
        position: 2,
        quizSessionId: '1',
        calculatorType: 'none',
        id: '2'
      },
      '3': {
        position: 3,
        quizSessionId: '1',
        calculatorType: 'basic',
        id: '3'
      },
      '4': {
        position: 4,
        quizSessionId: '1',
        calculatorType: 'none',
        id: '4'
      },
      // calc in wrong session, should not interfere
      '5': {
        position: 2,
        quizSessionId: '2',
        calculatorType: 'basic',
        id: '5'
      }
    }
  })
  describe('hasVisibleQuestionCalculator', () => {
    it('Sould always be false with flag off', () => {
      const result = calculator.hasVisibleQuestionCalculator(fromJS({}))
      expect(result).to.eq(false)
    })

    describe('with calculator per question', () => {
      withTestFeatures(['calculator_per_question'])

      describe('One question at a time', () => {
        it('returns true when current question has calculator', () => {
          setFakeStore(fakeState)

          const result = calculator.hasVisibleQuestionCalculator(fakeState)

          expect(result).to.eq(true)
        })

        it('returns false when current question calculator state is "none"', () => {
          const merged = fakeState.mergeDeep({ taking: { currentSessionItemPosition: 2 }})
          setFakeStore(merged)

          const result = calculator.hasVisibleQuestionCalculator(merged)

          expect(result).to.eq(false)
        })
      })

      describe('More than one question at a time', () => {
        it('returns true when any question in current session has a calculator', () => {
          const merged = fakeState.mergeDeep({
            quizSessions: {
              activeQuizSessionId: '1',
              '1': {
                oneAtATimeType: null
              }
            }
          })
          setFakeStore(merged)

          const result = calculator.hasVisibleQuestionCalculator(merged)

          expect(result).to.eq(true)
        })

        it('returns false when no questions in current session have a calculator', () => {
          const merged = fakeState.mergeDeep({
            quizSessions: {
              '1': {
                oneAtATimeType: null
              }
            },
            sessionItems: {
              '3': {
                calculatorType: 'none'
              }
            }
          })
          setFakeStore(merged)
          expect(merged.getIn(['quizSessions', 'activeQuizSessionId'])).to.eq('1')

          const result = calculator.hasVisibleQuestionCalculator(merged)

          expect(result).to.eq(false)
        })
      })
    })
  })
})