import Fetcher from '../../util/Fetcher'

import makeFakeFetch from '../../../../tests/util/fakeFetch'
import {jsoniblePromise} from '../../util/jsonify'

import { addFeatures } from '../../actions/features'
import { getFeatures } from '../features'

describe('features api action', () => {
  describe('getFeatures', () => {
    const fetchStub = sinon.stub()
    const dispatchStub = sinon.stub()
    const getStateStub = sinon.stub()

    useFakeFetching({dispatchStub, getStateStub, fetchStub})

    it('hits the correct url', () => {
      const expectedUrl = '/api/features'
      Fetcher.fakeFetch = fetchStub

      getFeatures()(dispatchStub, getStateStub)
      expect(fetchStub.calledWith(expectedUrl)).to.be.true()
    })

    it('dispatches addFeatures with the response', (done) => {
      const response = ['feature1', 'feature2']
      const fakeFetch = makeFakeFetch(jsoniblePromise(response))
      Fetcher.fakeFetch = fakeFetch

      const fetchPromise = getFeatures()(dispatchStub, getStateStub)
      fetchPromise.then(() => {
        expect(dispatchStub.calledWith(addFeatures(response))).to.be.true()
        done()
      })
    })
  })
})
