all files / src/__tests__/ reducer.change.spec.js

100% Statements 28/28
100% Branches 4/4
100% Functions 8/8
100% Lines 17/17
6 statements, 1 function, 3 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 122 123 124 125 126 127 128 129                                                                                                                                                                                                                                 
import {change} from '../actions'
 
const describeChange = (reducer, expect, {fromJS}) => () => {
  it('should set value on change with empty state', () => {
    const state = reducer(undefined, {
      ...change('myValue'),
      form: 'foo',
      field: 'myField'
    })
    expect(state)
      .toEqualMap({
        foo: {
          values: {
            myField: 'myValue'
          }
        }
      })
  })
 
  it('should set value on change and touch with empty state', () => {
    const state = reducer(undefined, {
      ...change('myValue'),
      form: 'foo',
      field: 'myField',
      touch: true
    })
    expect(state)
      .toEqualMap({
        foo: {
          values: {
            myField: 'myValue'
          },
          fields: {
            myField: {
              touched: true
            }
          }
        }
      })
  })
 
  it('should set value on change and touch with initial value', () => {
    const state = reducer(fromJS({
      foo: {
        values: {
          myField: 'initialValue'
        },
        initial: {
          myField: 'initialValue'
        }
      }
    }), {
      ...change('myValue'),
      form: 'foo',
      field: 'myField',
      touch: true
    })
    expect(state)
      .toEqualMap({
        foo: {
          values: {
            myField: 'myValue'
          },
          initial: {
            myField: 'initialValue'
          },
          fields: {
            myField: {
              touched: true
            }
          }
        }
      })
  })
 
  it('should set value on change and remove field-level submit and async errors', () => {
    const state = reducer(fromJS({
      foo: {
        values: {
          myField: 'initial'
        },
        fields: {
          myField: {
            submitError: 'submit error',
            asyncError: 'async error'
          }
        },
        error: 'some global error'
      }
    }), {
      ...change('different'),
      form: 'foo',
      field: 'myField'
    })
    expect(state)
      .toEqualMap({
        foo: {
          values: {
            myField: 'different'
          },
          fields: {
            myField: {}
          },
          error: 'some global error' // unaffected by CHANGE
        }
      })
  })
 
  it('should set nested value on change with empty state', () => {
    const state = reducer(undefined, {
      ...change('myValue'),
      form: 'foo',
      field: 'myField.mySubField'
    })
    expect(state)
      .toEqualMap({
        foo: {
          values: {
            myField: {
              mySubField: 'myValue'
            }
          }
        }
      })
  })
}
 
export default describeChange