import { expect } from 'chai'
import { SessionStore } from '../sessionStore'

describe('SessionStore', () => {
  context('with a storage that raises on set', () => {
    const errorOnSetStorage = {
      set: () => {
        throw new Error('no!')
      }
    }

    it('is not enabled', () => {
      const sessionStore = new SessionStore(errorOnSetStorage)
      expect(sessionStore.enabled).to.be.false()
    })
  })

  context('with a storage that can be set', () => {
    let setValue
    const removeStub = sinon.stub()
    const setableStorage = {
      setItem: (key, value) => {
        setValue = value
      },
      getItem: () => {
        return setValue
      },
      removeItem: removeStub
    }
    const sessionStore = new SessionStore(setableStorage)

    it('is enabled', () => {
      expect(sessionStore.enabled).to.be.true()
    })

    context('value is empty', () => {
      it('removes the stored value', () => {
        const key = 'key'
        removeStub.resetHistory()
        sessionStore.set(key)
        expect(removeStub.calledWith(key)).to.be.true()
      })
    })
  })
})
