import assert from 'assert'
import {spy} from 'sinon'
import {createRequestMiddleware} from '../src'

const TYPE = 'ACTIONTYPE'

function createSpy() {
  return spy(action => {
    assert.ok(action)
    assert.ok(action.type === TYPE)
  })
}

function createMiddlewareSpy() {
  const spy_fn = spy(action => {
    assert.ok(action)

    if (spy_fn.calledOnce) {
      assert.ok(action.type === TYPE+'_START')
    }
    else if (spy_fn.calledTwice) {
      if (action.error) {
        assert.ok(action.type === TYPE+'_ERROR')
      }
      else {
        assert.ok(action.res)
        assert.ok(action.type === TYPE+'_SUCCESS')
      }
    }
  })
  return spy_fn
}

describe('requestMiddleware', () => {

  it('Passes through an action without a request', () => {
    let done = createSpy()
    let action = {type: TYPE}
    let middleware = createRequestMiddleware()
    middleware()(done)(action)
    assert.ok(done.calledOnce)
  })

  it('Passes through an action with a request field that isnt a function', () => {
    let done = createSpy()
    let action = {type: TYPE, request: 'lol'}
    let middleware = createRequestMiddleware()
    middleware()(done)(action)
    assert.ok(done.calledOnce)
  })

  it('Passes through an action with a configured request field that isnt a function', () => {
    let done = createSpy()
    let action = {type: TYPE, req: 'lol'}
    let middleware = createRequestMiddleware({request_name: 'req'})
    middleware()(done)(action)
    assert.ok(done.calledOnce)
  })

  it('Calls a request', () => {
    let req = {end: spy(callback => callback(null, {success: true}))}
    let done = createMiddlewareSpy()
    let action = {type: TYPE, request: req}

    let middleware = createRequestMiddleware()
    middleware()(done)(action)

    assert.ok(done.calledTwice)
    assert.ok(req.end.calledOnce)
  })

  it('Calls a request with config', () => {
    let req = {done: spy(callback => callback(null, {success: true}))}
    let done = createMiddlewareSpy()
    let action = {type: TYPE, a_request: req}

    let middleware = createRequestMiddleware({request_name: 'a_request', method: 'done'})
    middleware()(done)(action)

    assert.ok(done.calledTwice)
    assert.ok(req.done.calledOnce)
  })

  it('Errors from an error callback', () => {
    let req = {done: spy(callback => callback(new Error('failed')))}
    let done = createMiddlewareSpy()
    let action = {type: TYPE, a_request: req}

    let middleware = createRequestMiddleware({request_name: 'a_request', method: 'done'})
    middleware()(done)(action)

    assert.ok(done.calledTwice)
    assert.ok(req.done.calledOnce)
  })

  it('Errors from an error body property', () => {
    let req = {done: spy(callback => callback(null, {body: {error: 'failed'}}))}
    let done = createMiddlewareSpy()
    let action = {type: TYPE, a_request: req}

    let middleware = createRequestMiddleware({request_name: 'a_request', method: 'done'})
    middleware()(done)(action)

    assert.ok(done.calledTwice)
    assert.ok(req.done.calledOnce)
  })

})

@@ -751,34 +751,36 @@
 t', () =%3E %7B%0A    
-le
+cons
 t done = createS
@@ -780,34 +780,36 @@
 createSpy()%0A    
-le
+cons
 t action = %7Btype
@@ -816,26 +816,28 @@
 : TYPE%7D%0A    
-le
+cons
 t middleware
@@ -1012,34 +1012,36 @@
 n', () =%3E %7B%0A    
-le
+cons
 t done = createS
@@ -1041,34 +1041,36 @@
 createSpy()%0A    
-le
+cons
 t action = %7Btype
@@ -1089,34 +1089,36 @@
 est: 'lol'%7D%0A    
-le
+cons
 t middleware = c
@@ -1304,26 +1304,28 @@
 () =%3E %7B%0A    
-le
+cons
 t done = cre
@@ -1329,34 +1329,36 @@
 createSpy()%0A    
-le
+cons
 t action = %7Btype
@@ -1377,26 +1377,28 @@
  'lol'%7D%0A    
-le
+cons
 t middleware
@@ -1543,34 +1543,36 @@
 t', () =%3E %7B%0A    
-le
+cons
 t req = %7Bend: sp
@@ -1615,34 +1615,36 @@
 s: true%7D))%7D%0A    
-le
+cons
 t done = createM
@@ -1654,34 +1654,36 @@
 lewareSpy()%0A    
-le
+cons
 t action = %7Btype
@@ -1701,34 +1701,36 @@
 uest: req%7D%0A%0A    
-le
+cons
 t middleware = c
@@ -1898,34 +1898,36 @@
 g', () =%3E %7B%0A    
-le
+cons
 t req = %7Bdone: s
@@ -1971,34 +1971,36 @@
 s: true%7D))%7D%0A    
-le
+cons
 t done = createM
@@ -2010,34 +2010,36 @@
 lewareSpy()%0A    
-le
+cons
 t action = %7Btype
@@ -2059,34 +2059,36 @@
 uest: req%7D%0A%0A    
-le
+cons
 t middleware = c
@@ -2302,34 +2302,36 @@
 k', () =%3E %7B%0A    
-le
+cons
 t req = %7Bdone: s
@@ -2373,34 +2373,36 @@
 failed')))%7D%0A    
-le
+cons
 t done = createM
@@ -2412,34 +2412,36 @@
 lewareSpy()%0A    
-le
+cons
 t action = %7Btype
@@ -2461,34 +2461,36 @@
 uest: req%7D%0A%0A    
-le
+cons
 t middleware = c
@@ -2717,18 +2717,20 @@
 %3E %7B%0A    
-le
+cons
 t req = 
@@ -2800,18 +2800,20 @@
 ))%7D%0A    
-le
+cons
 t done =
@@ -2839,18 +2839,20 @@
 y()%0A    
-le
+cons
 t action
@@ -2888,18 +2888,20 @@
 q%7D%0A%0A    
-le
+cons
 t middle

test style