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

import callsReducer from '../calls'
import {
  CALL_STATUS,
  SET_CALLBACKS_STATUS
} from '@instructure/quiz-common/lib/constants'

describe('calls reducer', () => {
  const nextStateEquals = (nextState, stateExpectations) => {
    expect(fromJS(nextState).equals(fromJS(stateExpectations))).to.be.true()
  }

  describe('call status', () => {
    it('should be able to set callStatus on an empty call', () => {
      const initialState = fromJS({})
      const action = {
        type: CALL_STATUS,
        payload: {
          call: 'quizSaveCall',
          status: 'calling'
        }
      }

      nextStateEquals(
        callsReducer(initialState, action),
        {quizSaveCall: {requestStatus: 'calling'}}
      )
    })

    it('should be able to set callStatus on an call with a value', () => {
      const initialState = fromJS({quizSaveCall: {requestStatus: 'calling'}})
      const action = {
        type: CALL_STATUS,
        payload: {
          call: 'quizSaveCall',
          status: 'success'
        }
      }

      nextStateEquals(
        callsReducer(initialState, action),
        {quizSaveCall: {requestStatus: 'success'}}
      )
    })
  })

  describe('callback status', () => {
    it('sets callback status properly', () => {
      const initialState = fromJS({})
      const action = {
        type: SET_CALLBACKS_STATUS,
        payload: {
          call: 'quizSaveCall',
          status: 'completed'
        }
      }

      nextStateEquals(
        callsReducer(initialState, action),
        {quizSaveCall: {callbackStatus: 'completed'}}
      )
    })
  })
})
