describe 'Textarea', ->

  React = require 'react'
  Textarea = React.createFactory require('../../src/components/textarea')
  TestUtils = require 'react-addons-test-utils'
  ReactDOM = require 'react-dom'
  ValidationContext = require '../../src/context_wrapper'

  #--------------------------------------------------------------------- Default Props
  it 'Should have default props', ->

    wrapper = TestUtils.renderIntoDocument(ValidationContext {
      factory: Textarea
    })

    textarea = wrapper.getInput()
    defaultProps = textarea.props

    defaultProps = textarea.props
    expect(defaultProps.className).to.equal('textarea')
    expect(defaultProps.loading).to.equal(no)
    expect(defaultProps.showClear).to.equal(no)
    expect(defaultProps.disabled).to.equal(no)
    expect(defaultProps.autoComplete).to.equal(no)
    expect(defaultProps.validation).to.equal(off)
    expect(defaultProps.isInPopover).to.equal(no)
    expect(defaultProps.spellCheck).to.equal(no)
    expect(defaultProps.focusOnMount).to.equal(no)
    expect(defaultProps.rows).to.equal("4")
    expect(defaultProps.cols).to.equal("50")


  #--------------------------------------------------------------------- Render
  it 'Should Render the spinner when loading property is true', ->
    loading = yes

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        loading: loading
    }

    inputSpinner = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'input-spinner'

    expect(inputSpinner.length).to.equal(1)


  it 'Should NOT Render the spinner when loading property is false', ->
    loading = no

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        loading: loading
    }

    inputSpinner = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'input-spinner'

    expect(inputSpinner.length).to.equal(0)


  it 'Should Render the clear search button when showClear property is true and value is not empty', ->
    showClear = yes

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        showClear: showClear
        value: 'test'
    }

    searchClearBtn = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'search-clear'

    expect(searchClearBtn.length).to.equal(1)

  it 'Should NOT Render the clear search button when showClear property is false and value is not empty', ->
    showClear = no

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        showClear: showClear
        value: 'test'
    }

    searchClearBtn = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'search-clear'

    expect(searchClearBtn.length).to.equal(0)

  it 'Should NOT Render the clear search button when showClear property is true and value is empty', ->
    showClear = yes

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        showClear: showClear
      }

    searchClearBtn = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'search-clear'

    expect(searchClearBtn.length).to.equal(0)


  it 'Should Render the wrapper label when wrapperLabel property is true', ->
    wrapperLabel = yes

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        wrapperLabel: wrapperLabel
    }

    label = TestUtils.scryRenderedDOMComponentsWithTag wrapper, 'label'

    expect(label.length).to.equal(1)

  it 'Should Render the wrapper label when wrapperLabel property is false', ->
    wrapperLabel = no

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        wrapperLabel: wrapperLabel
    }

    label = TestUtils.scryRenderedDOMComponentsWithTag wrapper, 'label'

    expect(label.length).to.equal(0)


  it 'Should Render the appropriate CSS class when wrapperClass property is passed', ->
    wrapperClass = 'wrapper-class'

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        wrapperClass: wrapperClass
    }
  
    mainDiv = TestUtils.scryRenderedDOMComponentsWithClass wrapper, wrapperClass

    expect(mainDiv.length).to.equal(1)


  it 'Should Render the appropriate CSS class when className property is passed', ->
    className = 'new-class'

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        className: className
    }
  
    textarea = TestUtils.scryRenderedDOMComponentsWithClass wrapper, className

    expect(textarea.length).to.equal(1)


  it 'Should Render the appropriate CSS classes when loading property is true', ->
    loading = yes

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        loading: loading
    }
  
    textareaClass = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'loading-spinner'
    errorButtonClass = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'is-hidden'

    expect(textareaClass.length).to.equal(1)
    expect(errorButtonClass.length).to.equal(1)


  it 'Should NOT Render the CSS classes when loading property is false', ->
    loading = no

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        loading: loading
    }
  
    textareaClass = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'loading-spinner'
    errorButtonClass = TestUtils.scryRenderedDOMComponentsWithClass wrapper, 'is-hidden'

    expect(textareaClass.length).to.equal(0)
    expect(errorButtonClass.length).to.equal(0)



  it 'Should fire all other passed handlers: focus, blur, key press, key down', ->

    onBlur = sinon.spy()
    onFocus = sinon.spy()
    onKeyDown = sinon.spy()
    onKeyPress = sinon.spy()

    wrapper = TestUtils.renderIntoDocument ValidationContext {
      factory: Textarea
      childProps:
        onFocus: onFocus
        onBlur: onBlur
        onKeyDown: onKeyDown
        onKeyPress: onKeyPress
        value: 'test'
    }

    textarea = TestUtils.findRenderedDOMComponentWithClass wrapper, 'textarea'

    TestUtils.Simulate.focus textarea
    TestUtils.Simulate.blur textarea
    TestUtils.Simulate.keyDown textarea
    TestUtils.Simulate.keyPress textarea

    expect(onBlur.called).to.equal(true)
    expect(onFocus.called).to.equal(true)
    expect(onKeyDown.called).to.equal(true)
    expect(onKeyPress.called).to.equal(true)
    
  

