require('../support')
import { mocks, helpers } from '../../support'
import CommunityJoinLinkHandler from '../../../src/containers/community/CommunityJoinLinkHandler.js'
import { renderIntoDocument } from 'react-addons-test-utils'
import { NAVIGATE } from '../../../src/actions'
const { createElement } = helpers

describe.only('CommunityJoinLinkHandler', () => {
  var store, redirectUrl

  const renderAndWait = (props, callback) => {
    const component = createElement(CommunityJoinLinkHandler, props, {store})
    renderIntoDocument(component).getWrappedInstance()
    setTimeout(callback, 10)
  }

  beforeEach(() => {
    store = mocks.redux.store({
      people: {current: {id: 42}},
      errors: {}
    })
    store.transformAction(NAVIGATE, action => redirectUrl = action.payload)
  })

  it('redirects to onboarding with new membership', done => {
    store.getState().people.current.memberships = [{
      community: {id: '1', name: 'Foomunity', slug: 'foomunity'}
    }]
    renderAndWait({params: {id: 'foomunity', tagName: 'bar'}}, () => {
      expect(redirectUrl).to.equal('/c/foomunity/onboarding')
      done()
    })
    // this is to compensate for the setTimeout in CommunityJoinLinkHandler
  })

  it('redirects to community profile with existing membership', done => {
    store.getState().people.current.memberships = [{
      community: {id: '1', name: 'Foomunity', slug: 'foomunity'},
      preexisting: true
    }]
    renderAndWait({params: {id: 'foomunity'}}, () => {
      expect(redirectUrl).to.equal('/c/foomunity')
      done()
    })
  })

  it('redirects to tag page with tagName param existing membership', done => {
    store.getState().people.current.memberships = [{
      community: {id: '1', name: 'Foomunity', slug: 'foomunity'},
      preexisting: true
    }]
    renderAndWait({params: {id: 'foomunity', tagName: 'bar'}}, () => {
      expect(redirectUrl).to.equal('/c/foomunity/tag/bar')
      done()
    })
  })
})

@@ -319,13 +319,8 @@
 ribe
-.only
 ('Co
@@ -566,27 +566,40 @@
     
-setTimeout(
+return helpers.wait(10, 
 callback
 , 10
@@ -598,12 +598,8 @@
 back
-, 10
 )%0A  
@@ -839,36 +839,34 @@
 ew membership', 
-done
+()
  =%3E %7B%0A    store.
@@ -976,32 +976,39 @@
 ty'%7D%0A    %7D%5D%0A    
+return 
 renderAndWait(%7Bp
@@ -1120,29 +1120,16 @@
 rding')%0A
-      done()%0A
     %7D)%0A 
@@ -1265,36 +1265,34 @@
 ng membership', 
-done
+()
  =%3E %7B%0A    store.
@@ -1427,32 +1427,39 @@
 true%0A    %7D%5D%0A    
+return 
 renderAndWait(%7Bp
@@ -1544,29 +1544,16 @@
 unity')%0A
-      done()%0A
     %7D)%0A 
@@ -1626,20 +1626,18 @@
 rship', 
-done
+()
  =%3E %7B%0A  
@@ -1784,24 +1784,31 @@
 %0A    %7D%5D%0A    
+return 
 renderAndWai
@@ -1925,21 +1925,8 @@
 r')%0A
-      done()%0A
     

use wait