describe 'Dialogue', ->
  React = require 'react'
  Dialogue = React.createFactory require('../../src/components/dialogue')
  TestUtils = require 'react-dom/test-utils'
  ReactDOM = require 'react-dom'
  dispatcher = require('../../src/dispatcher')
  ValidationContext = require '../../src/context_wrapper'


  afterEach -> dispatcher.dispatch 'clear-all'

  
  ###
  
  Note About ValidationContext

  The input validation uses context to gain access to the app level validation methods
  Because context is only present when the component is a child of the application, it is not present in tests
  The ValidationContext component, simply wraps the input component, and adds the validation context methods so they are present in the tests
  
  ###

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

    dialogue = TestUtils.renderIntoDocument Dialogue {}

    defaultProps = dialogue.props

    expect(defaultProps.message).to.equal('')
    expect(defaultProps.confirmText).to.equal('OK')
    expect(defaultProps.cancelText).to.equal('Cancel')
    expect(defaultProps.confirmCallback).to.be.a('function')
    expect(defaultProps.cancelCallback).to.be.a('function')
    expect(defaultProps.width).to.equal(400)
    expect(defaultProps.lightBackdrop).to.equal(no)


  #--------------------------------------------------------------------- CSS Classes
  it 'Should Render the appropriate CSS class when lightBackdrop is true', ->
    
    dialogue = TestUtils.renderIntoDocument Dialogue {
        lightBackdrop: yes
      }
   
    dialogueClass = TestUtils.scryRenderedDOMComponentsWithClass dialogue, 'dialogue-container'
    lightBackdropClass = TestUtils.scryRenderedDOMComponentsWithClass dialogue, 'light-backdrop'
   
    expect(dialogueClass.length).to.equal(1)
    expect(lightBackdropClass.length).to.equal(1)


  it 'Should Render the appropriate CSS class when lightBackdrop is false', ->
    
    dialogue = TestUtils.renderIntoDocument Dialogue {}

    dialogueClass = TestUtils.scryRenderedDOMComponentsWithClass dialogue, 'dialogue-container'
    lightBackdropClass = TestUtils.scryRenderedDOMComponentsWithClass dialogue, 'light-backdrop'
   
    expect(dialogueClass.length).to.equal(1)
    expect(lightBackdropClass.length).to.equal(0)


  #--------------------------------------------------------------------- Methods called
  # These need a fix from react-transition group, if we get it, then they can be uncommented

  it 'Should call confirmCallback when confirm button is clicked', ->

    confirmCallback = sinon.spy()
    cancelCallback = sinon.spy()
    handleButtonClick = sinon.spy()
    
    node = document.createElement('div')

    document.body.appendChild(node)

    dialogue = ReactDOM.render Dialogue({
      confirmCallback: confirmCallback
      cancelCallback: cancelCallback
    }), node

    dialogueEl = ReactDOM.findDOMNode(dialogue)

    okBtn = dialogueEl.getElementsByClassName('okay-btn')

    okBtn[0].click()

    ReactDOM.unmountComponentAtNode(node)

    expect(dialogue.props.confirmCallback.called).to.equal(true)
    expect(dialogue.props.cancelCallback.called).to.equal(true)

  it 'Should call cancelCallback when confirm button is clicked', ->

    confirmCallback = sinon.spy()
    cancelCallback = sinon.spy()
        
    node = document.createElement('div')

    document.body.appendChild(node)

    dialogue = ReactDOM.render Dialogue({
      confirmCallback: confirmCallback
      cancelCallback: cancelCallback
    }), node

    dialogueEl = ReactDOM.findDOMNode(dialogue)

    cancelBtn = dialogueEl.getElementsByClassName('cancel-btn')

    cancelBtn[0].click()

    ReactDOM.unmountComponentAtNode(node)

    expect(dialogue.props.confirmCallback.called).to.equal(false)
    expect(dialogue.props.cancelCallback.called).to.equal(true)


  it 'Should set Confirm button text to the confirmText property', ->

    text = 'Confirm Text'

    dialogue = TestUtils.renderIntoDocument Dialogue {
        confirmText: text
      }

    okBtn = TestUtils.findRenderedDOMComponentWithClass dialogue, 'okay-btn'

    expect(okBtn.innerHTML).to.equal(text)


  it 'Should set Confirm button text to the confirmText property', ->

    text = 'Cancel Text'

    dialogue = TestUtils.renderIntoDocument Dialogue {
        cancelText: text
      }

    cancelBtn = TestUtils.findRenderedDOMComponentWithClass dialogue, 'cancel-btn'

    expect(cancelBtn.innerHTML).to.equal(text)


  it 'Should set Message to the message property', ->

    message = 'New Message'

    dialogue = TestUtils.renderIntoDocument Dialogue {
        message: message
      }

    messageDiv = TestUtils.findRenderedDOMComponentWithClass dialogue, 'message'

    expect(messageDiv.innerHTML).to.equal(message)
   




    
   

