import { fromJS } from 'immutable'

import { singleRelationCheck } from '../../../../tests/util/recordTests'
import Item from '../Item'
import SessionItemResult from '../SessionItemResult'
import SessionItem from '../SessionItem'

describe('SessionItemResult Record', () => {
  describe('relationships', () => {
    const itemData = { id: '2' }
    const exampleState = {
      sessionItems: {
        '1_1': {
          item: itemData
        }
      }
    }

    describe('#getItem', () => {
      const options = {
        RelationRecordType: Item,
        relationAttrs: itemData,
        recordAttributes: { itemId: itemData.id, id: '1_1' },
        necessaryState: fromJS(exampleState)
      }

      singleRelationCheck(SessionItemResult, 'getItem', options)
    })
  })

  describe('#defaultResult', () => {
    it('builds a SessionItemResult with the proper attributes', () => {
      const si = new SessionItem(fromJS({
        item: {id: '2'},
        id: '1_1',
        position: 1
      }))

      const expectedDefaultSir = new SessionItemResult({
        attempt: 1,
        itemId: '2',
        id: si.id,
        position: si.position
      }).toJS()

      const sir = new SessionItemResult()
      const defaultResult = sir.defaultResult(si).toJS()
      expect(defaultResult).to.be.deep.equal(expectedDefaultSir)
    })
  })

  describe('#isLoaded', () => {
    it('returns false if itemId is null', () => {
      const sir = new SessionItemResult({ itemId: null })
      expect(sir.isLoaded()).to.be.false()
    })
    it('returns true if itemId is not null', () => {
      const sir = new SessionItemResult({ itemId: '1' })
      expect(sir.isLoaded()).to.be.true()
    })
  })

  describe('#isGraded', () => {
    it('returns false if itemId is null', () => {
      const sir = new SessionItemResult({ gradedAt: null })
      expect(sir.isGraded()).to.be.false()
    })
    it('returns true if itemId is not null', () => {
      const sir = new SessionItemResult({ gradedAt: '11/12/44' })
      expect(sir.isGraded()).to.be.true()
    })
  })

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

    it('returns true if modifications exist', () => {
      const result = new SessionItemResult()
      const modifiedResult = result.merge({ score: 99 })
      sinon.stub(result, 'getWorkingInstance').returns(modifiedResult)
      expect(result.hasModifications()).to.be.true()
    })
  })
})
