import { fromJS, Map } from 'immutable'

import {
  OPEN_MODAL,
  CLOSE_MODAL,
  OPEN_ADD_CONTENT_MODAL,
  OPEN_ADD_CONTENT_MODAL_DATA,
  ADD_CONTENT_MODAL,
  OPEN_DELETE_STIMULUS_MODAL,
  DELETE_STIMULUS_MODAL
} from '@instructure/quiz-common/lib/constants'

import modalReducer from '../modal'

describe('modal reducer', () => {
  describe('OPEN_ADD_CONTENT_MODAL', () => {
    it('sets isOpen to ADD_CONTENT_MODAL', () => {
      const initialState = Map({isOpen: false})
      const nextState = modalReducer(initialState, {
        type: OPEN_ADD_CONTENT_MODAL,
        payload: { x: 1 }
      })
      expect(nextState.get('isOpen')).to.eql(ADD_CONTENT_MODAL)
      expect(nextState.get(OPEN_ADD_CONTENT_MODAL_DATA)).to.eql(Map({x: 1}))
    })
  })

  describe('OPEN_DELETE_STIMULUS_MODAL', () => {
    it('sets isOpen to DELETE_STIMULUS_MODAL', () => {
      const initialState = Map({isOpen: false})
      const nextState = modalReducer(initialState, {type: OPEN_DELETE_STIMULUS_MODAL})
      expect(nextState.get('isOpen')).to.eql(DELETE_STIMULUS_MODAL)
    })
  })

  describe('OPEN_MODAL', () => {
    const title = 'Title'
    const options = {example: 'options'}
    const exampleOpenAction = {
      type: OPEN_MODAL,
      payload: {options, title}
    }

    it('sets isOpen to true', () => {
      const initialState = Map({isOpen: false})
      const nextState = modalReducer(initialState, exampleOpenAction)
      expect(nextState.get('isOpen')).to.eql(title)
    })

    it('sets the options to the payload options', () => {
      const initialState = Map({isOpen: false})
      const nextState = modalReducer(initialState, exampleOpenAction)
      expect(nextState.get('options')).to.be.eql(fromJS(options))
    })
  })

  describe('CLOSE_MODAL', () => {
    const exampleCloseAction = {
      type: CLOSE_MODAL
    }

    it('sets isOpen to false', () => {
      const initialState = Map({isOpen: true})
      const nextState = modalReducer(initialState, exampleCloseAction)
      expect(nextState.get('isOpen')).to.be.false()
    })
  })
})
