import { Map, List, fromJS } from 'immutable'
import { expect } from 'chai'

import quizzesReducer from '../quizzes'

import {
  addQuizzes,
  associateQuizEntries,
  disassociateQuizEntry,
  newActiveQuiz
} from '../../actions/quizzes'

describe('quizzes reducer', () => {
  describe('add quiz', () => {
    it('should add quizzes with ADD_QUIZZES action', () => {
      const initialState = Map({})
      const newQuizzes = {
        1: {id: '1', title: 'econ 231 online test'},
        2: {id: '2', title: 'fin 283 pop quiz'}
      }

      const action = addQuizzes(newQuizzes)
      const nextState = quizzesReducer(initialState, action)
      const nextTitle = nextState.get('2').get('title')

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

    it('should add quizzes with ADD_QUIZZES action and array payload', () => {
      const initialState = Map({})
      const newQuizzes = [
        {id: '1', title: 'econ 231 online test'},
        {id: '2', title: 'fin 283 pop quiz'}
      ]

      const action = addQuizzes(newQuizzes)
      const nextState = quizzesReducer(initialState, action)
      const nextTitle = nextState.get('2').get('title')

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

    it('should replace quizzes with ADD_QUIZZES action', () => {
      const initialState = fromJS({
        2: {id: '2', title: 'some intial title'}
      })

      const newQuizzes = {
        1: {id: '1', title: 'econ 231 online test'},
        2: {id: '2', title: 'some new title'}
      }

      const action = addQuizzes(newQuizzes)
      const nextState = quizzesReducer(initialState, action)
      const nextTitle = nextState.get('2').get('title')

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

  it('handles ACTIVE_QUIZ_CHANGE', () => {
    const initialState = Map({activeQuizId: '8'})
    const action = newActiveQuiz('12')
    const nextState = quizzesReducer(initialState, action)

    expect(nextState.get('activeQuizId')).to.equal('12')
  })

  describe('assocQuizEntries', () => {
    it('properly associates a list of quizEntries', () => {
      const initialState = fromJS({2: {id: '2', quizEntries: List()}})
      const newQuizEntries = {
        1: {id: '1', title: 'econ 231 online test'},
        2: {id: '2', title: 'fin 283 pop quiz'}
      }

      const action = associateQuizEntries('2', newQuizEntries)
      const nextState = quizzesReducer(initialState, action)

      const quizEntries = nextState.get('2').get('quizEntries')

      expect(quizEntries.toJS()).to.deep.eq(['1', '2'])
    })
  })

  describe('dissocQuizEntry', () => {
    it('properly disassociated quizEntry from quizzes', () => {
      const initialState = fromJS({
        2: {id: '2', quizEntries: ['1', '2', '3']}
      })
      const assocAction = disassociateQuizEntry('2', '2')
      const nextState = quizzesReducer(initialState, assocAction)
      const nextItems = nextState.get('2').get('quizEntries')

      expect(nextItems.equals(fromJS(['1', '3']))).to.be.true()
    })
  })
})
