import { List, fromJS } from 'immutable'

import QuizSession from '../../../../../common/records/QuizSession'

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

describe('ModerateTableRow', () => {
  const props = {
    onModerateLogClick () {},
    rowData: { name: 'lolwut', avatar: 'zomg', quizSessionIds: ['1'] },
    quizSessions: List(new QuizSession({ id: '1' }))
  }

  const testbedWithContext = createTestbed(withContext(ModerateTableRow, {}), props)
  shouldRenderComponent(testbedWithContext, {}, ModerateTableRow)

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

    it('receives quizSessions from state', () => {
      const fakeState = fromJS({
        quizSessions: { 1: { id: '1' } }
      })

      setFakeStore(fakeState)

      const { quizSessions } = mapStateToProps({}, props)
      expect(quizSessions).to.be.instanceOf(List)
      expect(quizSessions.size).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
      })

      setFakeStore(fakeState)

      const { quizSessions } = mapStateToProps({}, props)
      expect(quizSessions).to.be.instanceOf(List)
      expect(quizSessions.size).to.eq(0)
    })

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

      setFakeStore(fakeState)

      const { quizSessions } = mapStateToProps({}, props)
      expect(quizSessions).to.be.instanceOf(List)
      expect(quizSessions.size).to.eq(0)
    })
  })
})
