1.1.0 • Published 3 years ago

composable-form-tests v1.1.0

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

Composable Form Tests

Exposes functions that test your React components to ensure they correctly implement the Composable Form Spec. Assumes a Jest test environment and requires a peer dev dependency of enzyme.

Installation

$ npm i --save-dev composable-form-tests jest-cli babel-jest enzyme

How to Test an "Input" Component

In your input tests file, which will be run by Jest:

import React from 'react';
import { mount } from 'enzyme';
import { testInput } from 'composable-form-tests';
import MyInputComponent from './MyInputComponent';

testInput({
  component: MyInputComponent,
  mount,
  ...<other options>
});

testInput Options

componentREQUIRED. The component class that you are testing, which you intend to conform to the Composable Form "Input" Specification
defaultValueREQUIRED. The value that your component has when neither the user nor the containing Form passes in a value prop
exampleValueOneREQUIRED. A value that would make sense for your input. Must be different from the defaultValue and from exampleValueTwo
exampleValueTwoREQUIRED. A value that would make sense for your input. Must be different from exampleValueTwo.
mountREQUIRED. The mount function imported from enzyme package
optionsOPTIONAL. If your input takes options, the options array.
propsOPTIONAL. A props object that should be used as props on the input for all tests
simulateChangingOPTIONAL. If your input ever calls onChanging, use this function to simulate one user action that will cause it to happen.
simulateChangedREQUIRED. Use this function to simulate one user action that will cause onChange to be called.
simulateSubmitOPTIONAL. If your input ever calls onSubmit, use this function to simulate one user action that will cause it to happen.
testGetValueOPTIONAL. Default is false. Set to true to add a test for the getValue() instance function
testIsDirtyOPTIONAL. Default is false. Set to true to add a test for the isDirty() instance function
testResetValueOPTIONAL. Default is false. Set to true to add a test for the resetValue() instance function
testSetValueOPTIONAL. Default is false. Set to true to add a test for the setValue() instance function

Basic Input Example

import React from 'react';
import { mount } from 'enzyme';
import { testInput } from 'composable-form-tests';
import Input from './Input';

testInput({
  component: Input,
  defaultValue: null,
  exampleValueOne: 'ONE',
  exampleValueTwo: 'TWO',
  mount,
  simulateChanging(wrapper, value) {
    // Refer to Enzyme documentation
    wrapper.find('input').simulate('change', { target: { value } });
  },
  simulateChanged(wrapper, value) {
    // Refer to Enzyme documentation
    wrapper.find('input').simulate('blur', { target: { value } });
  },
  simulateSubmit(wrapper) {
    // Refer to Enzyme documentation
    wrapper.find('input').simulate('keypress', { which: 13 });
  },
});

Select Input Example

import React from 'react';
import { mount } from 'enzyme';
import { testInput } from 'composable-form-tests';
import SelectInput from './SelectInput';

testInput({
  component: SelectInput,
  defaultValue: null,
  exampleValueOne: 'a',
  exampleValueTwo: 'b',
  mount,
  options: [
    { label: 'A', value: 'a' },
    { label: 'B', value: 'b' },
    { label: 'C', value: 'c' },
  ],
  simulateChanged(wrapper, value) {
    // Refer to Enzyme documentation
    wrapper.find('select').simulate('change', { target: { value } });
  },
});
1.1.0

3 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

7 years ago