import { fromJS, Map, is } from 'immutable'

import { ADD_QTI_IMPORTS, CLEAR_QTI_IMPORTS } from '@instructure/quiz-common/lib/constants'
import qtiImportsReducer from '../qtiImports'

describe('qtiImports reducer', () => {
  describe('adding imports', () => {
    it('adds imports to the store indexed by id', () => {
      const firstImport = {id: 1, status: 'completed', parentId: '1'}
      const secondImport = {id: 9, status: 'started', parentId: '1'}
      const qtiImports = [firstImport, secondImport]
      const action = {
        type: ADD_QTI_IMPORTS,
        payload: qtiImports
      }

      const nextState = qtiImportsReducer(Map(), action)
      const expectedNextState = fromJS({
        1: firstImport,
        9: secondImport
      })

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

  describe('clearing imports', () => {
    it('clears all imports', () => {
      const firstImport = {id: 1, status: 'completed', parentId: '1'}
      const secondImport = {id: 9, status: 'started', parentId: '1'}
      const initialState = { 1: firstImport, 2: secondImport }
      const action = {
        type: CLEAR_QTI_IMPORTS
      }

      const nextState = qtiImportsReducer(initialState, action)
      const expectedNextState = fromJS({})

      expect(nextState).to.deep.eq(expectedNextState)
    })
  })
})
