import { expect } from 'chai'
import { fromJS } from 'immutable'
import {
  compact,
  keyBy,
  mergeDeepOnlyMaps
} from '../immutableHelpers'

describe('immutableHelpers', () => {
  describe('keyBy', () => {
    it('converts a list to map keyed by value', () => {
      const item1 = { someKey: 'bar', value: 'qux' }
      const item2 = { someKey: 'woz', value: 'quux' }
      const immutableList = fromJS([
        item1, item2
      ])
      expect(
        keyBy(immutableList, 'someKey').equals(
          fromJS({bar: item1, woz: item2})
        )
      ).to.be.true()
    })
  })

  describe('compact', () => {
    it('removes falsey values', () => {
      const immutableList = fromJS([1, null, 2, false, 3, void 0, 4])
      expect(
        compact(immutableList).equals(
          fromJS([1, 2, 3, 4])
        )
      ).to.be.true()
    })
  })

  describe('mergeDeepOnlyMaps', () => {
    it('deep merges maps', () => {
      const oldMap = fromJS({
        value: false,
        otherValue: true,
        deep: {
          value: false
        }
      })
      const newMap = fromJS({
        value: true,
        deep: {
          value: true
        }
      })
      expect(
        oldMap.mergeWith(mergeDeepOnlyMaps, newMap).equals(
          fromJS({
            value: true,
            otherValue: true,
            deep: {
              value: true
            }
          })
        )
      ).to.be.true()
    })

    it('shallow merges lists', () => {
      const oldMap = fromJS({
        odd: [1, 3, 5],
        even: [2, 4, 6],
        tens: [10, 20, 30]
      })
      const newMap = fromJS({
        odd: [3],
        even: []
      })
      expect(
        oldMap.mergeWith(mergeDeepOnlyMaps, newMap).equals(
          fromJS({
            odd: [3],
            even: [],
            tens: [10, 20, 30]
          })
        )
      ).to.be.true()
    })
  })
})
