
import test from 'tape'
import { createStore, applyMiddleware } from 'redux'
import sagaMiddleware from '../src'
import { is } from '../src/utils'

test('middleware output', assert => {


  const middleware = sagaMiddleware();

  assert.equal(typeof middleware, 'function',
    'middleware factory must return a function to handle {getState, dispatch}');

  assert.equal(middleware.length, 1,
    'middleware returned function must take exactly 1 argument');

  const nextHandler = middleware({});

  assert.equal(typeof nextHandler, 'function',
    'next handler must return a function to handle action');

  assert.equal(nextHandler.length, 1,
    'next handler must take exactly 1 argument');

  const actionHandler = nextHandler();

  assert.equal(typeof actionHandler, 'function',
    'next handler must return a function to handle action');

  assert.equal(actionHandler.length, 1,
    'action handler must take exactly 1 argument');


  assert.end();
});

test('middleware\'s action handler output', assert => {

  const action = {};
  const actionHandler = sagaMiddleware()({})(action => action);

  assert.equal(actionHandler(action), action,
    'action handler must return the result of the next argument');

  assert.end();
});

test('middleware.run', assert => {

  let actual

  function* saga(...args) {
    actual = args
  }

  const middleware = sagaMiddleware()

  try {
    middleware.run(function* () {})
  } catch (e) {
    assert.ok(e instanceof Error, 'middleware.run must throw an Error when executed before the middleware is connected to a Store')
  }

  createStore(()=>{}, applyMiddleware(middleware))
  const task = middleware.run(saga, 'argument')

  assert.ok(is.task(task), 'middleware.run must return a Task Object')

  const expected = ['argument']
  assert.deepEqual(actual, expected,
    'middleware must run the Saga and provides it with the given arguments'
  )

  assert.end()
})

@@ -1912,8 +1912,401 @@
 nd()%0A%7D)%0A
+%0Atest('middleware%5C's custom emitter', assert =%3E %7B%0A  const emittedActions = %5B%5D;%0A  const action = %7B%7D;%0A  sagaMiddleware(%7B%0A    emitter: function (emit) %7B%0A      return function (action) %7B%0A        emittedActions.push(action);%0A      %7D%0A    %7D%0A  %7D)(%7B%7D)(action =%3E action)(action);%0A%0A  assert.deepEqual(emittedActions, %5Baction%5D,%0A    'custom emitter needs be executed when specified');%0A%0A  assert.end();%0A%7D);%0A

test custom emitter option