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

import { setFakeStore, unsetFakeStore } from '../../../reduxStore'
import Item from '../Item'

describe('Item Record', () => {
  describe('#hasModifications', () => {
    it('returns false if it has no modifications', () => {
      const item = new Item()
      const unmodifiedItem = item.merge({})
      sinon.stub(item, 'getWorkingInstance').returns(unmodifiedItem)
      expect(item.hasModifications()).to.be.false()
    })

    it('returns true if modifications exist', () => {
      const item = new Item()
      const unmodifiedItem = item.merge({title: 'Some new title'})
      sinon.stub(item, 'getWorkingInstance').returns(unmodifiedItem)
      expect(item.hasModifications()).to.be.true()
    })
  })

  describe('#getWorkingInstance', () => {
    const fakeState = fromJS({
      modifications: {
        items: {
          4: { title: 'some updated title' }
        }
      }
    })

    before(() => setFakeStore(fakeState))

    after(() => unsetFakeStore())

    it('merges modifications from state', () => {
      const item = new Item({ id: '4', title: 'some title' })
      expect(item.getWorkingInstance().title).to.equal('some updated title')
    })

    it('merges passed overrides', () => {
      const item = new Item({ scoringData: { value: 'origValue' } })
      const overrides = { scoringData: { value: 'overrideValue' } }
      expect(item.getWorkingInstance(overrides).scoringData.get('value')).to.equal('overrideValue')
    })
  })

  describe('#hasFeedback', () => {
    it('returns false if it has no feedback', () => {
      const item = new Item()
      expect(item.hasFeedback()).to.not.be.ok()
    })

    it('returns true if feedback exists', () => {
      const item = new Item().merge({feedback: {
        neutral: 'neutral feedback'
      }})
      expect(item.hasFeedback()).to.be.ok()
    })
  })

  describe('#getChoice', () => {
    it('returns the proper choice', () => {
      const choiceToFindId = '23'
      const choiceToFind = fromJS({id: choiceToFindId})
      const item = new Item(fromJS({
        interactionData: {
          choices: [{id: '239'}, choiceToFind, {id: '34'}]
        }
      }))

      expect(item.getChoice(choiceToFindId))
        .to.be.equal(choiceToFind)
    })
  })
})
