import { fromJS, is } from 'immutable'

import { ADD_QUIZ_SESSION_EVENTS } from '@instructure/quiz-common/lib/constants'
import quizSessionEventsReducer from '../quizSessionEvents'

describe('quizSessionEvents reducer', () => {
  describe('adding events', () => {
    const eventA = {createdAt: 'yesterday', quizSessionId: 'lol'}
    const eventB = {createdAt: 'a bit ago', quizSessionId: 'lol'}
    const eventC = {createdAt: 'just now', quizSessionId: 'lol'}

    it('creates a new events list for a new quizSessionId', () => {
      const action = {
        type: ADD_QUIZ_SESSION_EVENTS,
        payload: [eventA]
      }

      const nextState = quizSessionEventsReducer(void 0, action)
      const expectedNextState = fromJS({ lol: [eventA] })

      expect(
        is(nextState, expectedNextState)
      ).to.be.true()
    })

    it('concats the events list for a preexisting quizSessionId', () => {
      const action = {
        type: ADD_QUIZ_SESSION_EVENTS,
        payload: [eventB, eventC]
      }

      const currentState = fromJS({ lol: [eventA] })
      const nextState = quizSessionEventsReducer(currentState, action)
      const expectedNextState = fromJS({
        lol: [eventA, eventB, eventC]
      })

      expect(
        is(nextState, expectedNextState)
      ).to.be.true()
    })
  })
})
