import store from 'store'

import persistedState from '../persistedState'
import sessionStore from '../sessionStore'

import {
  ACTIVE_QUESTION,
  CACHED_SESSION_ID
} from '@instructure/quiz-common/lib/constants'

describe('persistedState', () => {
  it('includes taking data', () => {
    const activeQuestionPosition = 2
    const sessionId = '22'

    store.set(ACTIVE_QUESTION, activeQuestionPosition)
    store.set(CACHED_SESSION_ID, sessionId)

    expect(
      persistedState.load().getIn(['taking', 'currentSessionItemPosition'])
    ).to.be.equal(activeQuestionPosition)

    expect(
      persistedState.load().getIn(['taking', 'sessionIdForCurrentItem'])
    ).to.be.equal(sessionId)
  })

  it('sets the active quesiton to 1 if null', () => {
    store.set(ACTIVE_QUESTION, null)

    expect(
      persistedState.load().getIn(['taking', 'currentSessionItemPosition'])
    ).to.be.equal(1)
  })

  it('works when local storage is disabled', () => {
    expect(sessionStore.enabled).to.be.true()
    sessionStore.enabled = false
    expect(persistedState.load()).to.not.be.ok()
    sessionStore.enabled = true
  })
})
