import { Map } from 'immutable'

import {
  CLEAR_ALT_TEXT,
  CLEAR_FILES,
  FILE_RECEIVED,
  FILE_UPLOAD_CALL,
  FILE_UPLOAD_STATUS,
  SET_ALT_TEXT
} from '@instructure/quiz-common/lib/constants'

import uploadReducer from '../upload'

describe('upload reducer', () => {
  const file = 'FILE'

  const sharedExample = (initialState, action, stateKey, expectedResult) => {
    const resultState = uploadReducer(initialState, action)
    expect(resultState.get(stateKey)).to.eql(expectedResult)
  }

  describe('CLEAR_ALT_TEXT', () => {
    it('clears the altText', () => {
      const exampleAction = {
        type: CLEAR_ALT_TEXT
      }
      sharedExample(Map({altText: 'some text'}), exampleAction, 'altText', void 0)
    })
  })

  describe('CLEAR_FILES', () => {
    it('clears the files', () => {
      const exampleReceiveAction = {
        type: CLEAR_FILES
      }
      sharedExample(Map(), exampleReceiveAction, 'files', void 0)
    })
  })

  describe('FILE_RECEIVED', () => {
    it('sets files to the new file', () => {
      const exampleReceiveAction = {
        type: FILE_RECEIVED,
        payload: {file: [file]}
      }
      sharedExample(Map(), exampleReceiveAction, 'files', [file])
    })
  })

  describe('FILE_UPLOAD_CALL', () => {
    it('sets files to empty', () => {
      const exampleUploadAction = {
        type: FILE_UPLOAD_CALL,
        payload: { file: [] }
      }
      sharedExample(Map({files: [file]}), exampleUploadAction, 'files', [])
    })
  })

  describe('FILE_UPLOAD_STATUS', () => {
    it('sets files to empty', () => {
      const exampleUploadAction = {
        type: FILE_UPLOAD_STATUS,
        payload: { status: 'ASTATUS' }
      }
      sharedExample(Map({satus: 'FIRST_STATUS'}), exampleUploadAction, 'status', 'ASTATUS')
    })
  })

  describe('SET_ALT_TEXT', () => {
    it('sets the alt text', () => {
      const exampleAction = {
        type: SET_ALT_TEXT,
        payload: { altText: 'some alt text' }
      }
      sharedExample(Map({altText: ''}), exampleAction, 'altText', 'some alt text')
    })
  })
})
