<commit_before>import Connect   from '../../src/addons/connect'
import DOM       from 'react-dom'
import Microcosm from '../../src/Microcosm'
import Provider  from '../../src/addons/provider'
import React     from 'react'
import Test      from 'react-addons-test-utils'
import assert    from 'assert'

function assertText(component, text) {
  assert.equal(DOM.findDOMNode(component).textContent, text)
}

describe('Connect Add-on', function() {

  it ('given context, it injects an application instance into a component as a prop', function(done) {
    let app = new Microcosm()

    app.start()

    let Namer = Connect()(React.createClass({
      componentDidMount() {
        assert.equal(this.props.app, app)
        done()
      },
      render() {
        return (<p>Test</p>)
      }
    }))

    Test.renderIntoDocument(<Provider app={ app }><Namer /></Provider>)
  })

  it ('listens to an application when it mounts', function(done) {
    let app = new Microcosm()

    app.start()
    app.listen = () => done()

    let Component = Connect()(props => <p>MVP</p>)
    let component = Test.renderIntoDocument(<Component app={ app } />)
  })

  it ('ignores an application when it unmounts', function(done) {
    let app = new Microcosm()

    app.start()
    app.ignore = () => done()

    let Child  = Connect()(props => <p>MVP</p>)
    let Parent = React.createClass({
      getInitialState() {
        return { display: true }
      },
      render() {
        return this.state.display ? <Child app={ app } /> : null
      }
    })

    let component = Test.renderIntoDocument(<Parent />)

    component.setState({ display: false })
  })

  it ('maps application state to props', function () {
    let app = new Microcosm()

    app.start().replace({ name: 'Kurtz' })

    let Namer = Connect(function(props) {
      return { name: state => props.prefix + ' ' + state.name }
    })(function({ name }) {
      return (<p>{ name }</p>)
    })

    let component = Test.renderIntoDocument(<Namer app={ app } prefix="Colonel" />)

    assertText(component, '<NAME>')
  })

  it ('sends new props to the component when an application changes', function () {
    let app = new Microcosm()

    app.start().replace({ name: 'Kurtz' })

    let Namer = Connect(function(props) {
      return { name: state => props.prefix + ' ' + state.name }
    })(function({ name }) {
      return (<p>{ name }</p>)
    })

    let parent = Test.renderIntoDocument(<Namer app={ app } prefix="Colonel" />)

    assertText(parent, '<NAME>')
    app.replace({ name: 'Hawk' })
    assertText(parent, '<NAME>')
  })

  context('when the connection is pure (default option)', function() {

    it ('does not cause a re-render if mapped state values do not change', function () {
      let app = new Microcosm()
      let renders = 0

      app.start().replace({ name: 'Kurtz' })

      let Namer = Connect(function() {
        return { name: state => state.name }
      })(function({ name }) {
        renders += 1
        return (<p>{ name }</p>)
      })

      Test.renderIntoDocument(<Namer app={ app } />)

      app.replace({ name: 'Kurtz', unrelated: true })

      assert.equal(renders, 1)
    })

    it ('does not cause a re-render if state is the same', function () {
      let app = new Microcosm()
      let renders = 0

      app.start().replace({ name: 'Kurtz' })

      let Namer = Connect(function() {
        return { name: state => state.name }
      })(function({ name }) {
        renders += 1
        return (<p>{ name }</p>)
      })

      Test.renderIntoDocument(<Namer app={ app } />)
      app.publish()
      assert.equal(renders, 1)
    })

  })

  context('when the connection is impure', function() {

    it ('always re-renders when the app changes', function () {
      let app = new Microcosm()
      let renders = 0

      app.start().replace({ name: 'Kurtz' })

      let Namer = Connect(function() {<commit_msg>Test that replacing the same state does not trigger a change in connect<commit_after>import Connect   from '../../src/addons/connect'
import DOM       from 'react-dom'
import Microcosm from '../../src/Microcosm'
import Provider  from '../../src/addons/provider'
import React     from 'react'
import Test      from 'react-addons-test-utils'
import assert    from 'assert'

function assertText(component, text) {
  assert.equal(DOM.findDOMNode(component).textContent, text)
}

describe('Connect Add-on', function() {

  it ('given context, it injects an application instance into a component as a prop', function(done) {
    let app = new Microcosm()

    app.start()

    let Namer = Connect()(React.createClass({
      componentDidMount() {
        assert.equal(this.props.app, app)
        done()
      },
      render() {
        return (<p>Test</p>)
      }
    }))

    Test.renderIntoDocument(<Provider app={ app }><Namer /></Provider>)
  })

  it ('listens to an application when it mounts', function(done) {
    let app = new Microcosm()

    app.start()
    app.listen = () => done()

    let Component = Connect()(props => <p>MVP</p>)
    let component = Test.renderIntoDocument(<Component app={ app } />)
  })

  it ('ignores an application when it unmounts', function(done) {
    let app = new Microcosm()

    app.start()
    app.ignore = () => done()

    let Child  = Connect()(props => <p>MVP</p>)
    let Parent = React.createClass({
      getInitialState() {
        return { display: true }
      },
      render() {
        return this.state.display ? <Child app={ app } /> : null
      }
    })

    let component = Test.renderIntoDocument(<Parent />)

    component.setState({ display: false })
  })

  it ('maps application state to props', function () {
    let app = new Microcosm()

    app.start().replace({ name: 'Kurtz' })

    let Namer = Connect(function(props) {
      return { name: state => props.prefix + ' ' + state.name }
    })(function({ name }) {
      return (<p>{ name }</p>)
    })

    let component = Test.renderIntoDocument(<Namer app={ app } prefix="Colonel" />)

    assertText(component, '<NAME>')
  })

  it ('sends new props to the component when an application changes', function () {
    let app = new Microcosm()

    app.start().replace({ name: 'Kurtz' })

    let Namer = Connect(function(props) {
      return { name: state => props.prefix + ' ' + state.name }
    })(function({ name }) {
      return (<p>{ name }</p>)
    })

    let parent = Test.renderIntoDocument(<Namer app={ app } prefix="Colonel" />)

    assertText(parent, '<NAME>')
    app.replace({ name: 'Hawk' })
    assertText(parent, '<NAME>')
  })

  context('when the connection is pure (default option)', function() {

    it ('does not cause a re-render if mapped state values do not change', function () {
      let app = new Microcosm()
      let renders = 0

      app.start().replace({ name: 'Kurtz' })

      let Namer = Connect(function() {
        return { name: state => state.name }
      })(function({ name }) {
        renders += 1
        return (<p>{ name }</p>)
      })

      Test.renderIntoDocument(<Namer app={ app } />)

      app.replace({ name: 'Kurtz', unrelated: true })

      assert.equal(renders, 1)
    })

    it ('does not cause a re-render if state is the same', function () {
      let app = new Microcosm()
      let renders = 0

      app.start().replace({ name: 'Kurtz' })

      let Namer = Connect(function() {
        return { name: state => state.name }
      })(function({ name }) {
        renders += 1
        return (<p>{ name }</p>)
      })

      Test.renderIntoDocument(<Namer app={ app } />)

      app.replace({ name: 'Kurtz' })

      assert.equal(renders, 1)
    })

  })

  context('when the connection is impure', function() {

    it ('always re-renders when the app changes', function () {
      let app = new Microcosm()
      let renders = 0

      app.start().replace({ name: 'Kurtz' })

      let Namer = Connect(function() {
{'fraction_non_alphanumeric': 0.15678126180581792, 'fraction_numerical': 0.0017630021407883138, 'mean_word_length': 2.5678346810422283, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '492329', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2354, 'n_words': 702}