import { List } from 'immutable'

import * as actions from '../../actions/bankSearch'
import reducer from '../bankSearch'

describe('search reducer', () => {
  const active = () => reducer(reducer(null, {}), actions.reset('42'))

  describe('update', () => {
    it('updates the search text', () => {
      const initialState = active()
      const action = actions.update('example')
      const nextState = reducer(initialState, action)

      expect(nextState.get('text')).to.eq('example')
    })
  })

  describe('toggle', () => {
    it('toggles filters on / off', () => {
      const initialState = active()
      const action = actions.toggle('interactionTypes', '8')

      const firstState = reducer(initialState, action)
      expect(firstState.get('interactionTypes').toJS()).to.deep.eq(['8'])

      const toggleOther = actions.toggle('interactionTypes', '9')
      const middleState = reducer(firstState, toggleOther)
      expect(middleState.get('interactionTypes').toJS()).to.deep.eq(['8', '9'])

      const finalState = reducer(middleState, action)
      expect(finalState.get('interactionTypes').toJS()).to.deep.eq(['9'])
    })
  })

  describe('setFilter', () => {
    it('can set the value of filters', () => {
      const initialState = active()
      const action = actions.setFilter('tags', [{ id: '8' }])

      const state = reducer(initialState, action)
      expect(state.get('tags')).to.deep.eq([{ id: '8' }])
    })
  })

  describe('reset', () => {
    it('resets all search state', () => {
      const initialState = active()
      const update = actions.update('example')
      const toggle = actions.toggle('interactionTypes', '8')

      const searching = reducer(reducer(initialState, update), toggle)
      const reset = reducer(searching, actions.reset())

      expect(reset.get('text', null)).to.eq(null)
      expect(reset.get('interactionTypes', List()).toJS()).to.deep.eq([])
    })
  })
})
