import { List } from 'immutable'
import QuizEntry from '../../records/QuizEntry'
import Bank from '../../records/Bank'
import generateDisplayPositions from '../generateDisplayPositions'

describe('generateDisplayPositions', () => {
  const bank = new Bank({entryCount: 9, itemEntryCount: 9})
  const qe1 = new QuizEntry()
  const qe2 = new QuizEntry()
  const qe3 = new QuizEntry({ id: '8', entryType: 'Bank' })
  const qe4 = new QuizEntry({ id: '8', entryType: 'Stimulus' })
  const qe5 = new QuizEntry()

  sinon.stub(qe3, 'getEntry').returns(bank)
  sinon.stub(qe4, 'getNestedEntries').returns(List([qe5, qe5, qe5]))

  it('returns an array of tuples of position - record', () => {
    const records = [qe1, qe2]
    const out = generateDisplayPositions(List(records))
    expect(out.length).to.eq(2)
    expect(out[0].displayPosition).to.eq(1)
    expect(out[0].record).to.eq(qe1)
    expect(out[1].displayPosition).to.eq(2)
    expect(out[1].record).to.eq(qe2)
  })

  it('returns the correct number for displayPosition when nestedRecords are present', () => {
    const qe6 = new QuizEntry()
    const records = [qe1, qe4, qe6, qe6, qe6, qe2, qe3, qe5]
    const out = generateDisplayPositions(List(records))
    expect(out.length).to.eq(5)
    expect(out[0].displayPosition).to.eq(1)
    expect(out[0].record).to.eq(qe1)

    // Stimulus
    expect(out[1].displayPosition).to.eq(2)
    expect(out[1].record).to.eq(qe4)

    expect(out[2].displayPosition).to.eq(5)
    expect(out[2].record).to.eq(qe2)

    // Banks
    expect(out[3].displayPosition).to.eq(6)
    expect(out[3].record).to.eq(qe3)

    expect(out[4].displayPosition).to.eq(15)
    expect(out[4].record).to.eq(qe5)
  })
})
