import { fromJS } from 'immutable'
import { expect } from 'chai'

import quizSessionReducer from '../quizSessions'

import {addSessionItems} from '../../actions/sessionItems'
import {
  addQuizSessions
} from '../../actions/quizSessions'

describe('quizSession reducer', () => {
  describe('addSessionItems', () => {
    it('adds session items to quiz sessions', () => {
      const initialState = fromJS({
        33: {
          sessionItems: ['33_1']
        },
        44: {
          sessionItems: ['44_1', '44_2']
        }
      })
      const action = addSessionItems([
        {id: '44_3', quizSessionId: '44'},
        {id: '33_2', quizSessionId: '33'}
      ])

      expect(quizSessionReducer(initialState, action).toJS()).to.eql({
        33: {
          sessionItems: ['33_1', '33_2']
        },
        44: {
          sessionItems: ['44_1', '44_2', '44_3']
        }
      })
    })

    it('works on sessions with no prior session items', () => {
      const initialState = fromJS({
        33: {}
      })
      const action = addSessionItems([
        {id: '33_1', quizSessionId: '33'},
        {id: '33_2', quizSessionId: '33'}
      ])

      expect(quizSessionReducer(initialState, action).toJS()).to.eql({
        33: {
          sessionItems: ['33_1', '33_2']
        }
      })
    })

    it('removes duplicates', () => {
      const initialState = fromJS({
        33: {
          sessionItems: ['33_1', '33_2']
        }
      })
      const action = addSessionItems([
        {id: '33_1', quizSessionId: '33'},
        {id: '33_2', quizSessionId: '33'},
        {id: '33_3', quizSessionId: '33'}
      ])

      expect(quizSessionReducer(initialState, action).toJS()).to.eql({
        33: {
          sessionItems: ['33_1', '33_2', '33_3']
        }
      })
    })
  })

  describe('addQuizSession', () => {
    it('should add quizSession with ADD_QUIZ_SESSIONS action', () => {
      const initialState = fromJS({})
      const newQuizSession = {id: '1', quiz: '2', title: 'fin 283 pop quiz'}

      const action = addQuizSessions(newQuizSession)
      const nextState = quizSessionReducer(initialState, action)
      const nextTitle = nextState.getIn(['1', 'title'])

      expect(nextTitle).to.equal('fin 283 pop quiz')
    })

    it('should replace quizSessions with ADD_QUIZ_SESSIONS action', () => {
      const initialState = fromJS({
        2: {quiz: '2', state: 'some intial title'}
      })

      const newQuizSession = {id: '2', quiz: '2', status: 'some new status'}

      const action = addQuizSessions(newQuizSession)
      const nextState = quizSessionReducer(initialState, action)
      const nextTitle = nextState.getIn(['2', 'status'])

      expect(nextTitle).to.equal('some new status')
    })

    it('should not replace lists with null if the new session has no value', () => {
      const initialState = fromJS({
        2: {sessionItems: ['1', '2']}
      })

      const newQuizSession = {id: '2', quiz: '2'}

      const action = addQuizSessions(newQuizSession)
      const nextState = quizSessionReducer(initialState, action)
      const nextSessionItems = nextState.getIn(['2', 'sessionItems']).toJS()

      expect(nextSessionItems).to.eql(['1', '2'])
    })
  })
})
