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

import { defaultChecks } from '../../../../tests/util/recordTests'
import QuizSessionResult from '../QuizSessionResult'
import { setFakeStore, unsetFakeStore } from '../../../reduxStore'

describe('QuizSessionResult Record', () => {
  describe('#constructor', () => {
    const defaultsToCheck = { id: '0' }
    defaultChecks(QuizSessionResult, defaultsToCheck)
  })

  describe('#isLoaded', () => {
    it('is loaded when there is a nonzero id', () => {
      const result = new QuizSessionResult({ id: '2' })
      expect(result.isLoaded()).to.be.true()
    })

    it('is not loaded by default', () => {
      const result = new QuizSessionResult()
      expect(result.isLoaded()).to.be.false()
    })

    it('is not loaded with id = 0', () => {
      const result = new QuizSessionResult({ id: '0' })
      expect(result.isLoaded()).to.be.false()
    })
  })

  describe('#isGraded', () => {
    it('is graded when status is set to "graded"', () => {
      const result = new QuizSessionResult({ status: 'graded' })
      expect(result.isGraded()).to.be.true()
    })

    it('is not graded by default', () => {
      const result = new QuizSessionResult()
      expect(result.isGraded()).to.be.false()
    })

    it('is not graded when status is not "graded"', () => {
      const result = new QuizSessionResult({ status: 'somethingelse' })
      expect(result.isGraded()).to.be.false()
    })
  })

  describe('#getSessionItemResults', () => {
    it('gets the relation list', () => {
      const result = new QuizSessionResult()
      const results = []
      result.getRelationList = sinon.stub().returns(results)
      expect(result.getSessionItemResults()).to.equal(results)
      expect(result.getRelationList.calledWith('sessionItemResults', 'findSessionItemResult')).to.be.true()
    })
  })

  describe('#getWorkingInstance', () => {
    after(() => {
      unsetFakeStore()
    })

    it('merges the right modifications into itself', () => {
      const modifiedFudgePoints = 5
      setFakeStore(
        fromJS({
          modifications: {
            quizSessionResults: {
              22: { id: '22', fudgePoints: modifiedFudgePoints }
            }
          }
        })
      )
      const quizSessionResult = new QuizSessionResult({ id: '22' })
      expect(quizSessionResult.getWorkingInstance().fudgePoints).to.be.equal(modifiedFudgePoints)
    })
  })

  describe('#hasModifications', () => {
    it('returns false if it has no modifications', () => {
      const result = new QuizSessionResult()
      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 QuizSessionResult()
      const modifiedResult = result.merge({ fudgePoints: 99 })
      sinon.stub(result, 'getWorkingInstance').returns(modifiedResult)
      expect(result.hasModifications()).to.be.true()
    })
  })
})
