import { fromJS, Set } from 'immutable'

import reducer from '../banks'

import * as actions from '../../actions/banks'

describe('banks reducer', () => {
  describe('addBanks', () => {
    it('updates state correctly', () => {
      const initialState = fromJS({1: {id: '1', name: 'Uno'}, 2: {id: '2', name: 'Due'}})
      const action = actions.addBanks([{id: '2', name: 'Dos'}, {id: '3', name: 'Tres'}])
      const nextState = reducer(initialState, action)

      expect(nextState.getIn(['1', 'name'])).to.eq('Uno')
      expect(nextState.getIn(['2', 'name'])).to.eq('Dos')
    })
  })

  describe('setBankFilters', () => {
    it('updates filters for the given bank', () => {
      const initialState = fromJS({
        '1': {id: '1', name: 'Uno', filters: null},
        '2': {id: '2', name: 'Due', filters: null}
      })
      const action = actions.setBankFilters('1', { some: 'filters' })
      const nextState = reducer(initialState, action)

      expect(nextState.getIn(['1', 'filters'])).to.deep.eq({ some: 'filters' })
      expect(nextState.getIn(['2', 'filters'])).to.eq(null)
    })
  })

  describe('associateBankEntries', () => {
    it('updates state correctly', () => {
      const initialState = fromJS({ 1: { id: '1', name: 'Uno' } })
      const action = actions.associateEntries('1', [2, 3])
      const nextState = reducer(initialState, action)

      expect(nextState.getIn(['1', 'bankEntries']).toJS()).to.eql([2, 3])
    })

    it('associates unique entry ids', () => {
      const initialState = fromJS({ 1: { id: '1', name: 'Uno', bankEntries: Set([2, 3]) } })
      const action = actions.associateEntries('1', [3, 4])
      const nextState = reducer(initialState, action)

      expect(nextState.getIn(['1', 'bankEntries']).toJS()).to.eql([2, 3, 4])
    })
  })

  describe('disassociateBankEntry', () => {
    it('updates state correctly', () => {
      const initialState = fromJS({ 1: { id: '1', name: 'Uno', bankEntries: Set([2, 3]) } })
      const action = actions.disassociateEntry('1', 2)
      const nextState = reducer(initialState, action)

      expect(nextState.getIn(['1', 'bankEntries']).toJS()).to.eql([3])
    })
  })

  describe('removeBank', () => {
    it('updates state correctly', () => {
      const initialState = fromJS({ 1: 'foo', 2: 'bar' })
      const action = actions.removeBank('1')
      const nextState = reducer(initialState, action)
      expect(nextState.get('2')).to.equal('bar')
      expect(nextState.get('1')).to.be.undefined()
    })
  })

  describe('archivebank', () => {
    it('updates state correctly', () => {
      const initialState = fromJS({ 1: { foo: 'bar' }, 2: 'baz' })
      const action = actions.archiveBank('1')
      const nextState = reducer(initialState, action)
      expect(nextState.get('2')).to.equal('baz')
      expect(nextState.get('1').toJS()).to.deep.equal({ foo: 'bar', archived: true })
    })
  })
})
