import { expect } from 'chai'
import { fromJS } from 'immutable'

import eventLoggerMiddlewareCreator from '../eventLoggerMiddleware'

const fakeLoggerFetch = sinon.stub().returns({
  then: () => {}
})

const createFakeStore = (fakeData) => ({
  getState () {
    return fromJS(fakeData)
  }
})

const fakeSerializer = sinon.stub()

const fakeAction = {type: 'STRING'}

const fakeDispatchWithDefaults = (storeData = {}, action, serializer = fakeSerializer) => {
  let dispatched = null
  const eventLoggerMiddleware = eventLoggerMiddlewareCreator(fakeLoggerFetch, serializer)

  const fakeActionTrigger = (actionAttempt) => {
    dispatched = actionAttempt
    return actionAttempt
  }

  const dispatch = eventLoggerMiddleware(
    createFakeStore(storeData)
  )(fakeActionTrigger)

  dispatch(action)
  return dispatched
}

describe('eventLoggerMiddleware', () => {
  it('calls for the kinesis client when serializer returns data', () => {
    fakeSerializer.returns({})
    fakeDispatchWithDefaults({}, fakeAction, fakeSerializer)
    expect(fakeLoggerFetch.called).to.be.true()
    fakeLoggerFetch.resetHistory()
  })

  it('does not call for kinesis client when serializer returns no data', () => {
    fakeSerializer.returns(null)
    fakeDispatchWithDefaults({}, fakeAction, fakeSerializer)
    expect(fakeLoggerFetch.called).to.not.be.true()
    fakeLoggerFetch.resetHistory()
  })
})
