<commit_before>import * as React from 'react';
import ReactDOM from 'react-dom';
import * as TestUtils from 'react-addons-test-utils';
import '@reactivex/rxjs'
import X, { x } from '../../src/x';
import * as RX from '../../src/engine/rx'
import { StaticStream } from '../../src/engine'
const compose = f => g => x => g(f(x));

const CounterView = React.createClass({
  render() {
    return (
      <div>
        <span className="count">{this.props.count}</span>
        <span className="wrapperProps">{this.props.wrapperProps}</span>
        <span className="overwritedProps">{this.props.overwritedProps}</span>
      </div>
    )
  }
})

CounterView.defaultProps = { count: 0, overwritedProps: 'inner' }
interface Intent {
  type: string
  value?: any
}
const counterWrapper = x<RX.URI, any, any>((intent$) => {
  return {
    update$: intent$.map((intent: Intent) => {
      switch (intent.type) {
        case 'inc':
          return state => ({ count: state.count + 1 })
        case 'dec':
          intent$.next({ type: 'dec triggered' })
          return state => ({ count: state.count - 1 })
        case 'changeWrapperProps':
          return state => ({
            wrapperProps: intent.value,
            overwritedProps: intent.value
          })
        case 'changeDefaultProps':
          return state => ({ count: intent.value })
        default:
          return state => state
      }
    }),
    actions: {
      inc: () => ({ type: 'inc' }),
      dec: () => ({ type: 'dec' }),
      changeWrapperProps: (value) => ({ type: 'changeWrapperProps', value }),
      changeDefaultProps: (value) => ({ type: 'changeDefaultProps', value }),
    }
  }
})
let XX = x => React.createFactory(X)({ engine: RX }, x)
const Counter = counterWrapper(CounterView)
describe('react-most', () => {
  describe('actions', () => {
    it('add intent to intent$ and go through sink$', () => {
      let counterWrapper = TestUtils.renderIntoDocument(
        XX(<Counter history={true} />)
      )
      let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
      let source = counter.machine.update$
        .scan((cs, f) => f.call(null, cs), { count: 0 })
        .take(3)
        .toPromise()

      counter.machine.actions.inc()
      counter.machine.actions.inc()
      counter.machine.actions.inc()

      return source.then(x => expect(x.count).toBe(3))


    })

    // it('async action', () => {
    //   let counterWrapper = TestUtils.renderIntoDocument(
    //     <Most engine={Engine}>
    //       <Counter history={true} />
    //     </Most>
    //   )
    //   let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
    //   counter.machine.actions.inc();
    //   counter.machine.actions.fromEvent({ type: 'inc' });
    //   return counter.machine.actions.fromPromise(Promise.resolve({ type: 'inc' }))
    //     .then(() => {
    //       expect(stateHistoryOf(counter)[2].count).toBe(3)
    //     })

    // })

    // it('sink can also generate intent', () => {
    //   let counterWrapper = TestUtils.renderIntoDocument(
    //     <Most engine={Engine}>
    //       <Counter history={true} />
    //     </Most>
    //   )
    //   let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)

    //   counter.machine.actions.dec()
    //   expect(intentHistoryOf(counter)[1].type).toBe('dec triggered')
    // })

    // it('props will overwirte components default props', () => {
    //   let counterWrapper = TestUtils.renderIntoDocument(
    //     <Most engine={Engine} >
    //       <Counter count={9} history={true} />
    //     </Most>
    //   )
    //   let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
    //   counter.machine.actions.inc();
    //   expect(stateHistoryOf(counter)[0].count).toBe(10)
    // })

    // it('props that not overlap with views defaultProps can not be changed', () => {
    //   let CounterWrapper = React.createClass({
    //     getInitialState() {
    //       return {
    //         wrapperProps: 'heheda',
    //         overwritedProps: 'hoho',
    //         count: 0,
    //       }
    //     },
    //     render() {
    //       return <Counter
    //         count={this.state.count}
    //         wrapperProps={this.state.wrapperProps}
    //         overwritedProps={this.state.overwritedProps}
    //         history={true} />
    //     }
    //   })
    //   let counterMostWrapper = TestUtils.renderIntoDocument(
    //     <Most engine={Engine}>
    //       <CounterWrapper />
    //     </Most>
    //   )
    //   let counterWrapper = TestUtils.findRenderedComponentWithType(counterMostWrapper, CounterWrapper)
    //   let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
    //   counter.machine.actions.changeWrapperProps('miao')
    //   counter.machine.actions.changeDefaultProps(19)<commit_msg>fix more tests<commit_after>import * as React from 'react';
import ReactDOM from 'react-dom';
import * as TestUtils from 'react-addons-test-utils';
import '@reactivex/rxjs'
import X, { x } from '../../src/x';
import * as RX from '../../src/engine/rx'
import { StaticStream } from '../../src/engine'
const compose = f => g => x => g(f(x));

const CounterView: React.SFC<any> = props => (
  <div>
    <span className="count">{props.count}</span>
    <span className="wrapperProps">{props.wrapperProps}</span>
    <span className="overwritedProps">{props.overwritedProps}</span>
  </div>
)

CounterView.defaultProps = { count: 0, overwritedProps: 'inner' }
interface Intent {
  type: string
  value?: any
}
const counterWrapper = x<RX.URI, any, any>((intent$) => {
  return {
    update$: intent$.map((intent: Intent) => {
      switch (intent.type) {
        case 'inc':
          return state => ({ count: state.count + 1 })
        case 'dec':
          intent$.next({ type: 'abs' })
          return state => ({ count: state.count - 1 })
        case 'abs':
          return state => ({ count: Math.abs(state.count) })
        case 'changeWrapperProps':
          return state => ({
            wrapperProps: intent.value,
            overwritedProps: intent.value
          })
        case 'changeDefaultProps':
          return state => ({ count: intent.value })
        default:
          return state => state
      }
    }),
    actions: {
      inc: () => ({ type: 'inc' }),
      dec: () => ({ type: 'dec' }),
      changeWrapperProps: (value) => ({ type: 'changeWrapperProps', value }),
      changeDefaultProps: (value) => ({ type: 'changeDefaultProps', value }),
    }
  }
})
let XX = x => React.createFactory(X)({ engine: RX }, x)

function Test(initState) {
  this.plans = 0
  this.things = []
  this.initState = initState
}
Test.prototype.plan = function(n) {
  this.plans = n
  return this
}
Test.prototype.do = function(things) {
  this.things = things
  return this
}
Test.prototype.collect = function(component) {
  let latestState = component.machine.update$
    .scan((cs, f) => f.call(null, cs), this.initState)
    .take(this.plans).toPromise()
  this.things.forEach(f => f())
  return latestState
}
const Counter = counterWrapper(CounterView)
describe('X', () => {
  describe('actions', () => {
    let counterWrapper, counter, t
    beforeEach(() => {
      counterWrapper = TestUtils.renderIntoDocument(
        XX(<Counter />)
      )
      counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)

    })
    t = new Test({ count: 0 });
    it('add intent to intent$ and go through sink$', () => {
      return t.plan(3)
        .do([
          counter.machine.actions.inc,
          counter.machine.actions.inc,
          counter.machine.actions.inc
        ])
        .collect(counter)
        .then(x => expect(x.count).toBe(3))
    })

    it('async action', () => {
      return t.plan(3)
        .do([
          counter.machine.actions.inc,
          () => counter.machine.actions.fromEvent({ type: 'inc' }),
          () => counter.machine.actions.fromPromise(Promise.resolve({ type: 'inc' }))
        ])
        .collect(counter)
        .then(state => expect(state.count).toBe(3))
    })

    it('update can also generate new intent', () => {
      return new Test({ count: -3 }).plan(3)
        .do([
          counter.machine.actions.dec
        ]).collect(counter)
        .then(state => expect(state.count).toBe(2))
    })

    it('props will overwirte components default props', () => {
      counterWrapper = TestUtils.renderIntoDocument(
        XX(<Counter count={9} />)
      )
      counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
      return new Test(counter.props).plan(1)
        .do([
          counter.machine.actions.inc
        ]).collect(counter)
        .then(state => expect(state.count).toBe(10))
    })

    // it('props that not overlap with views defaultProps can not be changed', () => {
    //   let CounterWrapper = React.createClass({
    //     getInitialState() {
    //       return {
    //         wrapperProps: 'heheda',
    //         overwritedProps: 'hoho',
    //         count: 0,
    //       }
    //     },
    //     render() {
    //       return <Counter
    //         count={this.state.count}
    //         wrapperProps={this.state.wrapperProps}
    //         overwritedProps={this.state.overwritedProps}
    //         history={true} />
    //     }
    //   })
    //   let counterMostWrapper = TestUtils.renderIntoDocument(
    //     <Most engine={Engine}>
    //       <CounterWrapper />
    //     </Most>
    //   )
    //   let counterWrapper = TestUtils.findRenderedComponentWithType(counterMostWrapper, CounterWrapper)
    //   let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
    //   counter.machine.actions.changeWrapperProps('miao')
    //   counter.machine.actions.changeDefaultProps(19)
{'fraction_non_alphanumeric': 0.1618391735871987, 'fraction_numerical': 0.0035446627506582947, 'mean_word_length': 2.286189683860233, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 100, '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': '3810497', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2904, 'n_words': 588}