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

100% Statements 65/65
100% Branches 4/4
100% Functions 22/22
100% Lines 60/60
1 branch 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                                                                                                                                                                                                                                                                                              
import expect from 'expect';
import { ADD_ARRAY_VALUE, AUTOFILL, BLUR, CHANGE, FOCUS, INITIALIZE, REMOVE_ARRAY_VALUE, RESET, START_ASYNC_VALIDATION,
  START_SUBMIT, STOP_ASYNC_VALIDATION, STOP_SUBMIT, SWAP_ARRAY_VALUES, TOUCH, UNTOUCH, DESTROY } from '../actionTypes';
import {addArrayValue, autofill, blur, change, destroy, focus, initialize, removeArrayValue, reset, startAsyncValidation, startSubmit,
  stopAsyncValidation, stopSubmit, swapArrayValues, touch, untouch} from '../actions';
 
describe('actions', () => {
  it('should create add array value action', () => {
    expect(addArrayValue('foo', undefined, 1)).toEqual({
      type: ADD_ARRAY_VALUE,
      path: 'foo',
      index: 1,
      value: undefined,
      fields: undefined
    });
    expect(addArrayValue('bar.baz')).toEqual({
      type: ADD_ARRAY_VALUE,
      path: 'bar.baz',
      index: undefined,
      value: undefined,
      fields: undefined
    });
    expect(addArrayValue('bar.baz', 'foo', 2)).toEqual({
      type: ADD_ARRAY_VALUE,
      path: 'bar.baz',
      index: 2,
      value: 'foo',
      fields: undefined
    });
    expect(addArrayValue('bar.baz', 'foo', 2, ['x', 'y'])).toEqual({
      type: ADD_ARRAY_VALUE,
      path: 'bar.baz',
      index: 2,
      value: 'foo',
      fields: [ 'x', 'y' ]
    });
  });
 
  it('should create autofill action', () => {
    expect(autofill('foo', 'bar')).toEqual({
      type: AUTOFILL,
      field: 'foo',
      value: 'bar'
    });
    expect(autofill('baz', 7)).toEqual({
      type: AUTOFILL,
      field: 'baz',
      value: 7
    });
  });
 
  it('should create blur action', () => {
    expect(blur('foo', 'bar')).toEqual({
      type: BLUR,
      field: 'foo',
      value: 'bar'
    });
    expect(blur('baz', 7)).toEqual({
      type: BLUR,
      field: 'baz',
      value: 7
    });
  });
 
  it('should create change action', () => {
    expect(change('foo', 'bar')).toEqual({
      type: CHANGE,
      field: 'foo',
      value: 'bar'
    });
    expect(change('baz', 7)).toEqual({
      type: CHANGE,
      field: 'baz',
      value: 7
    });
  });
 
  it('should create focus action', () => {
    expect(focus('foo')).toEqual({
      type: FOCUS,
      field: 'foo'
    });
  });
 
  it('should create initialize action', () => {
    const data = {a: 8, c: 9};
    const fields = ['a', 'c'];
    expect(initialize(data, fields)).toEqual({type: INITIALIZE, data, fields, overwriteValues: true});
    expect(initialize(data, fields)).toEqual({type: INITIALIZE, data, fields, overwriteValues: true});
    expect(initialize(data, fields, false)).toEqual({type: INITIALIZE, data, fields, overwriteValues: false});
    expect(initialize(data, fields, false)).toEqual({type: INITIALIZE, data, fields, overwriteValues: false});
  });
 
  it('should throw an error if initialize is not given a fields array', () => {
    expect(() => initialize({a: 1, b: 2}, undefined)).toThrow(/must provide fields array/);
    expect(() => initialize({a: 1, b: 2}, 'not an array')).toThrow(/must provide fields array/);
    expect(() => initialize({a: 1, b: 2}, {also: 'not an array'})).toThrow(/must provide fields array/);
  });
 
  it('should create remove array value action', () => {
    expect(removeArrayValue('foo', 3)).toEqual({
      type: REMOVE_ARRAY_VALUE,
      path: 'foo',
      index: 3
    });
    expect(removeArrayValue('bar.baz')).toEqual({
      type: REMOVE_ARRAY_VALUE,
      path: 'bar.baz',
      index: undefined
    });
  });
 
  it('should create reset action', () => {
    expect(reset()).toEqual({type: RESET});
  });
 
  it('should create destroy action', () => {
    expect(destroy()).toEqual({type: DESTROY});
  });
 
  it('should create startAsyncValidation action', () => {
    expect(startAsyncValidation('myField')).toEqual({
      type: START_ASYNC_VALIDATION,
      field: 'myField'
    });
  });
 
  it('should create startSubmit action', () => {
    expect(startSubmit()).toEqual({type: START_SUBMIT});
  });
 
  it('should create stopAsyncValidation action', () => {
    const errors = {
      foo: 'Foo error',
      bar: 'Error for bar'
    };
    expect(stopAsyncValidation(errors)).toEqual({
      type: STOP_ASYNC_VALIDATION,
      errors
    });
  });
 
  it('should create stopSubmit action', () => {
    expect(stopSubmit()).toEqual({
      type: STOP_SUBMIT,
      errors: undefined
    });
    const errors = {
      foo: 'Foo error',
      bar: 'Error for bar',
    };
    expect(stopSubmit(errors)).toEqual({
      type: STOP_SUBMIT,
      errors
    });
  });
 
  it('should create swap array value action', () => {
    expect(swapArrayValues('foo', 3, 6)).toEqual({
      type: SWAP_ARRAY_VALUES,
      path: 'foo',
      indexA: 3,
      indexB: 6
    });
    expect(swapArrayValues('foo', 3)).toEqual({
      type: SWAP_ARRAY_VALUES,
      path: 'foo',
      indexA: 3,
      indexB: undefined
    });
    expect(swapArrayValues('bar.baz')).toEqual({
      type: SWAP_ARRAY_VALUES,
      path: 'bar.baz',
      indexA: undefined,
      indexB: undefined
    });
  });
 
  it('should create touch action', () => {
    expect(touch('foo', 'bar')).toEqual({
      type: TOUCH,
      fields: ['foo', 'bar']
    });
    expect(touch('cat', 'dog', 'pig')).toEqual({
      type: TOUCH,
      fields: ['cat', 'dog', 'pig']
    });
  });
 
  it('should create untouch action', () => {
    expect(untouch('foo', 'bar')).toEqual({
      type: UNTOUCH,
      fields: ['foo', 'bar']
    });
    expect(untouch('cat', 'dog', 'pig')).toEqual({
      type: UNTOUCH,
      fields: ['cat', 'dog', 'pig']
    });
  });
 
});