<commit_before>import * as R from 'ramda';

import reducer, * as duck from '../formsDuck';

import { form, field } from '../utils/containers';


describe('#formsDuck', () => {
  // Action creators
  // ---

  it('should create an ADD_FORM action', () => {
    expect(duck.addForm('form')).toEqual({
      type: duck.ADD_FORM,
      payload: { name: 'form' },
    });
  });

  it('should create a REMOVE_FORM action', () => {
    expect(duck.removeForm('form')).toEqual({
      type: duck.REMOVE_FORM,
      payload: { name: 'form' },
    });
  });

  it('should create an ADD_FIELD action', () => {
    expect(duck.addField('form', 'field', field)).toEqual({
      type: duck.ADD_FIELD,
      payload: { form: 'form', id: 'field', field },
    });
  });

  it('should create a REMOVE_FIELD action', () => {
    expect(duck.removeField('form', 'field')).toEqual({
      type: duck.REMOVE_FIELD,
      payload: { form: 'form', id: 'field' },
    });
  });

  it('should create a FIELD_CHANGE action', () => {
    expect(duck.fieldChange('form', 'field', 'value', 'error', true)).toEqual({
      type: duck.FIELD_CHANGE,
      payload: { form: 'form', field: 'field', value: 'value', error: 'error', dirty: true },
    });
  });

  it('should create a FIELD_FOCUS action', () => {
    expect(duck.fieldFocus('form', 'field')).toEqual({
      type: duck.FIELD_FOCUS,
      payload: { form: 'form', field: 'field' },
    });
  });

  it('should create a FIELD_BLUR action', () => {
    expect(duck.fieldBlur('form', 'field', 'error', true)).toEqual({
      type: duck.FIELD_BLUR,
      payload: { form: 'form', field: 'field', error: 'error', dirty: true },
    });
  });

  // Reducer
  // ---

  it('should return initial state', () => {
    const state = reducer(undefined, <any> {});

    expect(state).toEqual({});
  });

  it('should add a form', () => {
    const state = reducer({}, duck.addForm('form'));

    expect(state).toEqual({ form });
  });

  it('should remove a form', () => {
    const state = reducer({ form }, duck.removeForm('form'));

    expect(state).toEqual({});
  });

  it('should add a field', () => {
    const state: any = reducer({
      form: { fields: {} },
    }, duck.addField('form', 'field', field));

    expect(state.form.fields.field).toEqual(field);
  });

  it('should remove a field', () => {
    const state: any = reducer({
      form: { fields: { field } },
    }, duck.removeField('form', 'field'));

    expect(state.form.fields).toEqual({});
  });

  it('should change a field', () => {
    const state: any = reducer({
      form: { fields: { field } },
    }, duck.fieldChange('form', 'field', 'doge', 'error', true));

    expect(state.form.fields.field).toEqual({
      value: 'doge',
      error: 'error',
      dirty: true,
      touched: false,
      visited: false,
      active: false,
    });
  });

  it('should focus a field', () => {
    const state: any = reducer({
      form: { fields: { field } },
    }, duck.fieldFocus('form', 'field'));

    expect(state.form.fields.field).toEqual({
      value: '',
      error: null,
      dirty: false,
      touched: false,
      visited: true,
      active: true,
    });
  });

  it('should blur a field', () => {
    const state: any = reducer({
      form: { fields: { field: R.assoc('active', true, field) } },
    }, duck.fieldBlur('form', 'field', 'error', true));

    expect(state.form.fields.field).toEqual({
      value: '',
      error: 'error',
      dirty: true,
      touched: true,
      visited: false,
      active: false,
    });
  });
});<commit_msg>formsDuck.spec: Add cases of unwanted change<commit_after>import reducer, * as duck from '../formsDuck';

import { form, field } from '../utils/containers';


describe('#formsDuck', () => {
  // Action creators
  // ---

  it('should create an ADD_FORM action', () => {
    expect(duck.addForm('form')).toEqual({
      type: duck.ADD_FORM,
      payload: { name: 'form' },
    });
  });

  it('should create a REMOVE_FORM action', () => {
    expect(duck.removeForm('form')).toEqual({
      type: duck.REMOVE_FORM,
      payload: { name: 'form' },
    });
  });

  it('should create an ADD_FIELD action', () => {
    expect(duck.addField('form', 'field', field)).toEqual({
      type: duck.ADD_FIELD,
      payload: { form: 'form', id: 'field', field },
    });
  });

  it('should create a REMOVE_FIELD action', () => {
    expect(duck.removeField('form', 'field')).toEqual({
      type: duck.REMOVE_FIELD,
      payload: { form: 'form', id: 'field' },
    });
  });

  it('should create a FIELD_CHANGE action', () => {
    expect(duck.fieldChange('form', 'field', 'value', 'error', true)).toEqual({
      type: duck.FIELD_CHANGE,
      payload: { form: 'form', field: 'field', value: 'value', error: 'error', dirty: true },
    });
  });

  it('should create a FIELD_FOCUS action', () => {
    expect(duck.fieldFocus('form', 'field')).toEqual({
      type: duck.FIELD_FOCUS,
      payload: { form: 'form', field: 'field' },
    });
  });

  it('should create a FIELD_BLUR action', () => {
    expect(duck.fieldBlur('form', 'field', 'error', true)).toEqual({
      type: duck.FIELD_BLUR,
      payload: { form: 'form', field: 'field', error: 'error', dirty: true },
    });
  });

  // Reducer
  // ---

  it('should return initial state', () => {
    const state = reducer(undefined, <any> {});

    expect(state).toEqual({});
  });

  it('should add a form', () => {
    const state = reducer({}, duck.addForm('form'));

    expect(state).toEqual({ form });
  });

  it('should remove a form', () => {
    const state = reducer({ form }, duck.removeForm('form'));

    expect(state).toEqual({});
  });

  it('should add a field', () => {
    const state: any = reducer({
      form: { fields: {} },
    }, duck.addField('form', 'field', field));

    expect(state.form.fields.field).toEqual(field);
  });

  it('should remove a field', () => {
    const state: any = reducer({
      form: { fields: { field } },
    }, duck.removeField('form', 'field'));

    expect(state.form.fields).toEqual({});
  });

  it('should change a field', () => {
    const state: any = reducer({
      form: { fields: { field } },
    }, duck.fieldChange('form', 'field', 'doge', 'error', true));

    expect(state.form.fields.field).toEqual({
      value: 'doge',
      error: 'error',
      dirty: true,
      touched: false,
      visited: false,
      active: false,
    });
  });

  it('should not change unwanted props', () => {
    const newField = {
      ...field,
      touched: true,
      visited: true,
      active: true,
    };

    const state: any = reducer({
      form: { fields: { field: newField } },
    }, duck.fieldChange('form', 'field', 'doge', 'error', true));

    expect(state.form.fields.field.touched).toBe(true);
    expect(state.form.fields.field.visited).toBe(true);
    expect(state.form.fields.field.active).toBe(true);
  });

  it('should focus a field', () => {
    const state: any = reducer({
      form: { fields: { field } },
    }, duck.fieldFocus('form', 'field'));

    expect(state.form.fields.field).toEqual({
      value: '',
      error: null,
      dirty: false,
      touched: false,
      visited: true,
      active: true,
    });
  });

  it('should not focus unwanted props', () => {
    const newField = {
      ...field,
      value: 'doge',
      error: 'error',
      dirty: true,
      touched: true,
    };

    const state: any = reducer({
      form: { fields: { field: newField } },
    }, duck.fieldFocus('form', 'field'));

    expect(state.form.fields.field.value).toBe('doge');
    expect(state.form.fields.field.error).toBe('error');
    expect(state.form.fields.field.dirty).toBe(true);
    expect(state.form.fields.field.touched).toBe(true);
  });

  it('should blur a field', () => {
    const newField = {
      ...field,
      active: true,
    };

    const state: any = reducer({
      form: { fields: { field: newField } },
    }, duck.fieldBlur('form', 'field', 'error', true));

    expect(state.form.fields.field).toEqual({
      value: '',
      error: 'error',
      dirty: true,
      touched: true,
      visited: false,
      active: false,
    });
  });

  it('should not blur unwanted props', () => {
    const newField = {
      ...field,
      value: 'doge',
      visited: true,
    };

    const state: any = reducer({
      form: { fields: { field: newField } },
    }, duck.fieldBlur('form', 'field', 'error', true));

    expect(state.form.fields.field.value).toBe('doge');
    expect(state.form.fields.field.visited).toBe(true);
  });
});
{'fraction_non_alphanumeric': 0.2046327552089396, 'fraction_numerical': 0.0, 'mean_word_length': 2.7389033942558747, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '3605414', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2803, 'n_words': 629}