all files / src/__tests__/ Field.spec.js

98.15% Statements 106/108
97.14% Branches 34/35
92.31% Functions 24/26
93.94% Lines 31/33
4 statements, 12 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                                                                                     
/* 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))