/* eslint react/no-multi-comp:0 */
import expect, { createSpy } from 'expect'
import React, { Component, PropTypes } from 'react'
import ReactDOMServer from 'react-dom/server'
import { connect, Provider } from 'react-redux'
import { combineReducers as plainCombineReducers, createStore } from 'redux'
import { combineReducers as immutableCombineReducers } from 'redux-immutablejs'
import TestUtils from 'react-addons-test-utils'
import createReduxForm from '../createReduxForm'
import createReducer from '../createReducer'
import createField from '../createField'
import plain from '../structure/plain'
import plainExpectations from '../structure/plain/expectations'
import immutable from '../structure/immutable'
import immutableExpectations from '../structure/immutable/expectations'
import addExpectations from './addExpectations'
const createRestorableSpy = (fn) => {
return createSpy(fn, function restore() {
this.calls = []
})
}
const describeField = (name, structure, combineReducers, expect) => {
const reduxForm = createReduxForm(structure)
const Field = createField(structure)
const reducer = createReducer(structure)
const {empty, setIn, fromJS} = structure
const makeStore = (initial = {}) => createStore(
combineReducers({ form: reducer }), fromJS({ form: initial }))
class TestInput extends Component {
render() {
return <div>TEST INPUT</div>
}
}
const testProps = (state, propsCallback) => {
const store = makeStore({ testForm: state })
class Form extends Component {
render() {
return <div><Field name="foo" component={TestInput}/></div>
}
}
const TestForm = reduxForm({ form: 'testForm' })(Form)
const dom = TestUtils.renderIntoDocument(
<Provider store={store}>
<TestForm/>
</Provider>
)
const stub = TestUtils.findRenderedComponentWithType(dom, TestInput)
propsCallback(stub.props)
}
describe(name, () => {
it('should throw an error if not in ReduxForm', () => {
expect(() => {
TestUtils.renderIntoDocument(<div>
<Field name="foo" component={TestInput}/>
</div>
)
}).toThrow(/must be inside a component decorated with reduxForm/)
})
it('should get value from Redux state', () => {
testProps({
values: {
foo: 'bar'
}
}, props => {
expect(props.value).toBe('bar')
})
})
it('should get dirty/pristine from Redux state', () => {
testProps({
initial: {
foo: 'bar'
},
values: {
foo: 'bar'
}
}, props => {
expect(props.pristine).toBe(true)
expect(props.dirty).toBe(false)
})
testProps({
initial: {
foo: 'bar'
},
values: {
foo: 'baz'
}
}, props => {
expect(props.pristine).toBe(false)
expect(props.dirty).toBe(true)
})
})
//it('should render without error', () => {
// const store = makeStore()
// expect(() => {
// try {
// const Decorated = reduxForm({ form: 'testForm' })(Form)
// TestUtils.renderIntoDocument(
// <Provider store={store}>
// <Decorated/>
// </Provider>
// )
// } catch (e) {
// console.error(e)
// }
// }).toNotThrow()
//})
})
}
describeField('Field.plain', plain, plainCombineReducers, addExpectations(plainExpectations))
describeField('Field.immutable', immutable, immutableCombineReducers, addExpectations(immutableExpectations))
|