import { Map } from 'immutable'

import QuizSessionEvent from '../QuizSessionEvent'

describe('QuizSessionEvent record', () => {
  // testrail test_id:  C2954598, C2954599, C2954603 , C2954602
  describe('#isLoaded', () => {
    it('returns true if createdAt exists', () => {
      const eventRecord = new QuizSessionEvent({createdAt: '2016-09-28T18:45:29.929Z|462090529'})
      expect(eventRecord.isLoaded()).to.be.true()
    })
    it('returns false if createdAt is undefined', () => {
      const eventRecord = new QuizSessionEvent({})
      expect(eventRecord.isLoaded()).to.be.false()
    })
  })

  describe('#createdDate', () => {
    it('returns a Date object with correctly parsed data', () => {
      const eventRecord = new QuizSessionEvent({createdAt: '2016-09-28T18:45:29.929Z|462090529'})
      const expectedDate = new Date('2016-09-28T18:45:29.929Z')
      expect(eventRecord.createdDate().getTime())
        .to.be.equal(expectedDate.getTime())
    })
  })

  describe('#getSessionItem', () => {
    it('uses the position to fetch the correct session item', () => {
      const eventRecord = new QuizSessionEvent({quizSessionId: 'foo', eventData: Map({ position: 2 })})
      const fakeSessionItem = {}
      const findSessionItem = sinon.stub().returns(fakeSessionItem)
      eventRecord.stateHelper = () => ({ findSessionItem })
      expect(eventRecord.getSessionItem()).to.equal(fakeSessionItem)
      expect(findSessionItem.calledWith('foo_2')).to.be.true()
    })
  })

  describe('#getItem', () => {
    it('proxies a call to getItem from the SessionItem', () => {
      const eventRecord = new QuizSessionEvent()
      const fakeItem = {}
      const getItem = sinon.stub().returns(fakeItem)
      eventRecord.getSessionItem = () => ({ getItem })
      expect(eventRecord.getItem()).to.equal(fakeItem)
      expect(getItem).to.have.been.called()
    })
  })
})
