import {fromJS} from 'immutable'

import QuizAnalysis from '../QuizAnalysis'

describe('Quiz Analysis Record', () => {
  const analysis = {
    quizId: '0',
    quizAnalysis: {
      centile: Array.from({length: 100}, () => Math.floor(Math.random() * 5)),
      decile: Array.from({length: 10}, () => Math.floor(Math.random() * 5)),
      cronbachsAlpha: 0.75,
      scoreInfo: {
        scoreCount: 20,
        scoreSum: 100,
        scoreMax: 9,
        scoreMin: 2,
        elapsedTimeSum: 61,
        pointsPossible: 10,
        scoreStddevPop: 2.5
      }
    }
  }
  const quizAnalysis = new QuizAnalysis(fromJS(analysis))

  describe('Score Information', () => {
    it('returns scoreInfo', () => {
      expect(quizAnalysis.scoreInfo().get('scoreCount')).to.be.equal(20)
      expect(quizAnalysis.scoreInfo().get('scoreSum')).to.be.equal(100)
    })

    it('calculates mean score percentage', () => {
      expect(quizAnalysis.meanScore()).to.be.equal('50%')
    })

    it('calculates high score percentage', () => {
      expect(quizAnalysis.highScore()).to.be.equal('90%')
    })

    it('calculates low score percentage', () => {
      expect(quizAnalysis.lowScore()).to.be.equal('20%')
    })

    it('calculates standard deviation', () => {
      expect(quizAnalysis.standardDeviation()).to.be.equal('2.5')
    })

    it('calculates mean elapsed time', () => {
      expect(quizAnalysis.meanElapsedTime()).to.be.equal(3)
    })

    it('returns cronbachs alpha', () => {
      expect(quizAnalysis.cronbachsAlpha()).to.be.equal('0.75')
    })

    it('calculates centile distribution', () => {
      expect(quizAnalysis.centileDistribution().length).to.be.equal(100)
    })

    it('calculates decile distribution', () => {
      expect(quizAnalysis.decileDistribution().length).to.be.equal(10)
    })
  })

  describe('Mean elapsed time', () => {
    it('rounds mean time', () => {
      expect(quizAnalysis.meanElapsedTime()).to.be.equal(3)
    })
  })

  context('with no submissions', () => {
    const emptyAnalysis = new QuizAnalysis(fromJS(Object.assign({}, analysis, {
      quizAnalysis: {
        centile: Array.from({length: 100}, () => 0),
        decile: Array.from({length: 10}, () => 0),
        scoreInfo: {
          scoreCount: 0,
          scoreSum: 0,
          elapsedTimeSum: 0
        }
      }
    })))

    it('returns mean elapsed time', () => {
      expect(emptyAnalysis.meanElapsedTime()).to.be.equal(0)
    })
  })
})
