require('babel-register');

const test           = require('tape');
const thunk          = require('redux-thunk').default;
const middleware     = require('../lib/middleware').default;
const RemoteResource = require('../lib/RemoteResource').default;
const { createFetchMock, nest }        = require('./mock.helper');
const { applyMiddleware, createStore } = require('redux');

function makeMockStore(remoteResourceOpts = {}) {
  const fetchMock = createFetchMock();
  const middlewares = applyMiddleware(middleware(
    Object.assign({ _fetch: fetchMock.fetchFunc }, remoteResourceOpts)
  ), thunk);

  const actionLogReducer = (state = [], action) => state.concat(action);
  const store = createStore(actionLogReducer, middlewares);

  return { fetch: fetchMock, store };
}

// test action types
const REQUEST = 'REQUEST';
const REQUEST_SUCCESS = 'REQUEST_SUCCESS';
const REQUEST_FAILURE = 'REQUEST_FAILURE';

test.only('middleware integration', st => {
  st.test(nest('idempotent request'), t => {
    const { fetch, store } = makeMockStore();

    const respData = { test: 'data' };
    fetch.respondWith(200, respData);
    const reqHeaders = { 'Accept': 'application/json' };
    const action = {
      [RemoteResource]: {
        uri: 'localhost/someapi',
        headers: reqHeaders,
        lifecycle: {
          request: REQUEST,
          success(data, dispatch) {
            t.deepEquals(data, respData, 'returned correct parsed data');
            dispatch({ type: REQUEST_SUCCESS });
          },
          failure(error, dispatch) {
            t.error(error);
            dispatch({ type: REQUEST_FAILURE });
          }
        }
      }
    };

    store.dispatch(action)
      .then(() => {
        const actions = store.getState();
        t.ok(actions.length, 'state has actions');

        const actionWasDispatched =
          actionType => actions.some(({ type }) => type === actionType);

        t.ok(actionWasDispatched(REQUEST), 'request was dispatched');
        t.ok(actionWasDispatched(REQUEST_SUCCESS),
          'request success was dispatched');

        t.equals(fetch.method(), 'GET', 'fetched w/ method GET by default');
        t.deepEqual(fetch.headers(), reqHeaders,
          'fetch was called with correct headers');
      }).catch(t.fail);

    t.end();
  });

  st.test(nest('post request'), t => t.fail('not implemented'));
  st.test(nest('status actions'), t => t.fail('not implemented'));
  st.test(nest('idempotent request with cache'), t => t.fail('not implemented'));
});

@@ -275,17 +275,20 @@
 nest
- %7D       
+, failTest %7D
  = r
@@ -350,16 +350,19 @@
 eStore %7D
+   
  = requi
@@ -774,16 +774,121 @@
 e %7D;%0A%7D%0A%0A
+const actionWasDispatched = (actionType, actions) =%3E%0A  actions.some((%7B type %7D) =%3E type === actionType);%0A%0A
 // test 
@@ -1227,16 +1227,17 @@
 pData);%0A
+%0A
     cons
@@ -1281,24 +1281,64 @@
 on/json' %7D;%0A
+    const reqUrl = 'localhost/someapi';%0A
     const ac
@@ -1385,35 +1385,22 @@
    uri: 
-'localhost/someapi'
+reqUrl
 ,%0A      
@@ -1939,119 +1939,8 @@
 ');%0A
-%0A        const actionWasDispatched =%0A          actionType =%3E actions.some((%7B type %7D) =%3E type === actionType);%0A%0A
     
@@ -1975,16 +1975,25 @@
 (REQUEST
+, actions
 ), 'requ
@@ -2062,16 +2062,25 @@
 _SUCCESS
+, actions
 ),%0A     
@@ -2147,24 +2147,111 @@
 tch.
-method(), 'GET',
+url(), reqUrl, 'fetch w/ correct url');%0A        t.equals(fetch.method().toLowerCase(), 'get',%0A         
  'fe
@@ -2381,24 +2381,41 @@
  headers');%0A
+        t.end();%0A
       %7D).cat
@@ -2417,22 +2417,30 @@
 ).catch(
-t.
 fail
+Test);%0A  %7D
 );%0A%0A    

Use failTest method, validate fetched URL, pull out utility func