import { Map } from 'immutable'
import { expect } from 'chai'

import sessionItemsReducer from '../sessionItems'
import {addSessionItems} from '../../actions/sessionItems'

describe('sessionItems reducer', () => {
  describe('add sessionItems', () => {
    it('should add sessionItems with ADD_SESSION_ITEMS action', () => {
      const initialState = Map({})
      const sessionItems = [
        {
          item: '47',
          id: '1_1'
        },
        {
          item: '48',
          id: '2_2'
        }
      ]

      const action = addSessionItems(sessionItems)
      const nextState = sessionItemsReducer(initialState, action)

      const firstID = nextState.getIn(['1_1', 'item'])
      expect(firstID).to.equal('47')

      const secondID = nextState.getIn(['2_2', 'item'])
      expect(secondID).to.equal('48')
    })

    it('should store nested data as an immutable map (not plain JS)', () => {
      const initialState = Map({})
      const sessionItems = [
        {
          item: '55',
          id: '1_1',
          scoringData: {value: {foo: 'bar'}}
        }
      ]

      const action = addSessionItems(sessionItems)
      const nextState = sessionItemsReducer(initialState, action)
      // getIn won't work with plain JS
      const nestedValue = nextState.getIn(['1_1', 'scoringData', 'value', 'foo'])
      expect(nestedValue).to.be.equal('bar')
    })
  })
})
