/*globals describe,it*/
import connect from '../src/index'
const expect = require('chai').expect

const dummyProtocol = (config, register, dispatch) => {
  register('test', () => {
    return 'It works!'
  })

  setTimeout(() => {
    if (config.multipleParams) {
      dispatch('test event', {success: true}, false)
    } else {
      dispatch('test event', {success: true})
    }
  }, 5)
}

describe('connect function', () => {
  describe('core functionality', () => {
    it('should return an error if protocol is not defined', (done) => {
      expect(() => { connect({}) }).to.throw(Error)

      done()
    })

    it('should return an error if the protocol is not a function or a string', (done) => {
      expect(() => { connect({ protocol: 4 }) }).to.throw(Error)

      done()
    })

    it('should return an error if the protocol can not be found', (done) => {
      expect(() => { connect({ protocol: 'somestupidprotocolthatshouldntexist' + Math.floor(Math.random() * 999) }) }).to.throw(Error)

      done()
    })

    it('should return an object with two functions - "on" and "call"', (done) => {
      let c = connect({
        protocol: dummyProtocol
      })

      expect(c.on).to.exist
      expect(c.call).to.exist

      done()
    })
  })

  describe('event dispatcher', () => {
    it('should successfully dispatch the events to be subscribed to', (done) => {
      let c = connect({
        protocol: dummyProtocol
      })

      c.on('test event', () => {
        done()
      })
    })

    it('should pass paraemeters to the event callback', (done) => {
      let c = connect({
        protocol: dummyProtocol
      })

      c.on('test event', (e) => {
        expect(e.success).to.eql(true)

        done()
      })
    })

    it('allow multiple parameters in the event callback', (done) => {
      let c = connect({
        protocol: dummyProtocol,
        multipleParams: true
      })

      c.on('test event', (e, bool) => {
        expect(e.success).to.eql(true)
        expect(bool).to.eql(false)

        done()
      })
    })
  })

  // TODO: Add unit tests for new API
  // describe('register() and call()', () => {
  //   it('should throw an error if there\'s no method registered', (done) => {
  //     let c = connect({
  //       protocol: dummyProtocol
  //     })
  //
  //     expect(() => { c.call('bleh') }).to.throw(Error)
  //
  //     done()
  //   })
  //
  //   it('should return whatever the registered function returns to the user', (done) => {
  //     let c = connect({
  //       protocol: dummyProtocol
  //     })
  //
  //     expect(c.call('test')).to.eql('It works!')
  //
  //     done()
  //   })
  // })
})

@@ -125,18 +125,8 @@
 fig,
- register,
  dis
@@ -143,131 +143,54 @@
 %7B%0A  
-register('test', () =%3E %7B%0A    return 'It works!'%0A  %7D)%0A%0A  setTimeout(() =%3E %7B%0A    if (config.multipleParams) %7B%0A      dispatch(
+setTimeout(() =%3E %7B%0A    dispatch(%7B%0A      type: 
 'tes
@@ -190,34 +190,39 @@
 e: 'test event',
+%0A     
  
-%7B
 success: true%7D, 
@@ -222,90 +222,97 @@
 true
-%7D, false)%0A    %7D else %7B%0A      dispatch('test event', %7Bsuccess: true%7D)%0A    %7D%0A  %7D, 5)
+%0A    %7D)%0A  %7D, 5)%0A%0A  return event =%3E event.type === 'test' ? 'It works!' : 'Unknown event.'
 %0A%7D%0A%0A
@@ -1011,20 +1011,20 @@
 n%22 and %22
-call
+send
 %22', (don
@@ -1140,20 +1140,20 @@
 xpect(c.
-call
+send
 ).to.exi
@@ -1646,321 +1646,8 @@
 e)%0A%0A
-        done()%0A      %7D)%0A    %7D)%0A%0A    it('allow multiple parameters in the event callback', (done) =%3E %7B%0A      let c = connect(%7B%0A        protocol: dummyProtocol,%0A        multipleParams: true%0A      %7D)%0A%0A      c.on('test event', (e, bool) =%3E %7B%0A        expect(e.success).to.eql(true)%0A        expect(bool).to.eql(false)%0A%0A
     

fix unittests