import { fromJS } from 'immutable'
import { mapStateToProps } from '../index'
import { setFakeStore, unsetFakeStore } from '../../../../../reduxStore'
import {
  PENDING, IN_PROGRESS, GRADED
} from '@instructure/quiz-common/lib/constants'

describe('ResetQuiz', () => {
  describe('mapStateToProps', () => {
    afterEach(() => {
      unsetFakeStore()
    })

    it('passes open/closed state to the component', () => {
      const fakeState = fromJS({
        quizSessions: { 1: { id: '1' } },
        moderation: { selectedSessions: { id: '1' }}
      })

      setFakeStore(fakeState)

      const { sessionId } = mapStateToProps(fakeState)
      expect(sessionId).to.eq('1')
    })

    it('filters to use only loaded quizSessions', () => {
      const fakeState = fromJS({
        quizSessions: { 1: { id: '0' } },// NOTE: 0 id means it has not loaded
        moderation: { selectedSessions: { id: '1' }}
      })

      setFakeStore(fakeState)

      const { sessionId } = mapStateToProps(fakeState)
      expect(sessionId).not.to.exist()
    })

    it('filters to exclude pending quizSessions', () => {
      const fakeState = fromJS({
        quizSessions: {
          1: { id: '1', status: PENDING }
        },
        moderation: { selectedSessions: { id: '1' }}
      })

      setFakeStore(fakeState)

      const { sessionId } = mapStateToProps(fakeState)
      expect(sessionId).not.to.exist()
    })

    it('marks in_progress sessions as incomplete', () => {
      const fakeState = fromJS({
        quizSessions: {
          1: { id: '1', status: IN_PROGRESS }
        },
        moderation: { selectedSessions: { id: '1' }}
      })

      setFakeStore(fakeState)

      const { isComplete } = mapStateToProps(fakeState)
      expect(isComplete).to.be.false()
    })

    it('marks graded sessions as complete', () => {
      const fakeState = fromJS({
        quizSessions: {
          1: { id: '1', status: GRADED }
        },
        moderation: { selectedSessions: { id: '1' }}
      })

      setFakeStore(fakeState)

      const { isComplete } = mapStateToProps(fakeState)
      expect(isComplete).to.be.true()
    })
  })
})
