import { Map } from 'immutable'

import reducer from '../ui'

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

describe('ui reducer', () => {
  describe('toggle', () => {
    it('adds new keys', () => {
      const initialState = Map()
      const action = actions.toggle('my_key')
      const nextState = reducer(initialState, action)

      expect(nextState.get('my_key')).to.eq(true)
    })

    it('updates existing keys', () => {
      const initialState = Map({ my_key: true })
      const action = actions.toggle('my_key')
      const nextState = reducer(initialState, action)

      expect(nextState.get('my_key')).to.eq(false)
    })
  })

  describe('set', () => {
    it('sets value', () => {
      const initialState = Map({ my_key: 'some_value' })
      const action = actions.set('my_key', 'a_different_value')
      const nextState = reducer(initialState, action)

      expect(nextState.get('my_key')).to.eq('a_different_value')
    })
  })

  describe('merge', () => {
    it('adds new keys', () => {
      const initialState = Map()
      const action = actions.merge('my_key', { my: { nested: 'object' } })
      const nextState = reducer(initialState, action)

      expect(nextState.get('my_key').toJS()).to.eql({ my: { nested: 'object' } })
    })

    it('merges with existing keys', () => {
      const initialState = Map({ my_key: Map({ my: Map({ nested: 'object' }) }) })
      const action = actions.merge('my_key', { my: { sibling: 'object' } })
      const nextState = reducer(initialState, action)

      expect(nextState.get('my_key').toJS()).to.eql({ my: { nested: 'object', sibling: 'object' } })
    })
  })

  describe('occludeQuizEntries', () => {
    it('should add quizEntriesOccluded to state', () => {
      const initialState = Map()
      const action = actions.occludeQuizEntries(true)
      const nextState = reducer(initialState, action)
      const occluded = nextState.get('quizEntriesOccluded')
      expect(occluded).to.equal(true)
    })
  })

  it('handles ACTIVE_BANK_ENTRY_CHANGE', () => {
    const initialState = Map({activeBankEntryId: '900'})
    const action = actions.newActiveBankEntry('1900')
    const nextState = reducer(initialState, action)

    expect(nextState.get('activeBankEntryId')).to.equal('1900')
  })

  it('handles ACTIVE_BANK_CHANGE', () => {
    const initialState = Map({activeBankId: '800'})
    const action = actions.newActiveBank('1200')
    const nextState = reducer(initialState, action)

    expect(nextState.get('activeBankId')).to.equal('1200')
  })
})
