File size: 3,756 Bytes
1e92f2d |
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 |
import assert from 'assert';
import { mapValues } from 'lodash';
import formState from '../';
function checkNthState( n, callback ) {
let count = 0;
return function ( state ) {
if ( count === n ) {
callback( state );
}
count++;
};
}
function testController( options ) {
const fieldNames = options.fieldNames;
const defaults = {
loadFunction: function ( onComplete ) {
const fieldValues = Object.fromEntries( fieldNames.map( ( name ) => [ name, 'loaded' ] ) );
onComplete( null, fieldValues );
},
validatorFunction: function ( fieldValues, onComplete ) {
const fieldErrors = mapValues( fieldValues, () => [] );
onComplete( null, fieldErrors );
},
onNewState: function () {},
debounceWait: 0,
};
return formState.Controller( { ...defaults, ...options } );
}
describe( 'index', () => {
describe( '#Controller', () => {
describe( '#getInitialState', () => {
test( 'returns disabled fields', () => {
const controller = testController( { fieldNames: [ 'firstName' ] } );
const state = controller.getInitialState();
expect( formState.isFieldDisabled( state, 'firstName' ) ).toStrictEqual( true );
assert.strictEqual( formState.isFieldDisabled( state, 'firstName' ), true );
} );
} );
test( 'enables the fields on the first event', () => {
return new Promise( ( done ) => {
const onNewState = checkNthState( 0, function ( state ) {
expect( formState.isFieldDisabled( state, 'firstName' ) ).toStrictEqual( false );
done();
} );
testController( {
fieldNames: [ 'firstName' ],
onNewState: onNewState,
} );
} );
} );
describe( '#handleFieldChange', () => {
test( 'updates the field value', () => {
return new Promise( ( done ) => {
const onNewState = checkNthState( 1, function ( state ) {
expect( formState.getFieldValue( state, 'firstName' ) ).toStrictEqual( 'foo' );
done();
} );
const controller = testController( {
fieldNames: [ 'firstName' ],
onNewState: onNewState,
} );
controller.handleFieldChange( {
name: 'firstName',
value: 'foo',
} );
} );
} );
test( 'validates the new value', () => {
return new Promise( ( done ) => {
const validatorFunction = function ( fieldValues, onComplete ) {
onComplete( null, { firstName: [ 'invalid' ] } );
};
const onNewState = checkNthState( 3, function ( state ) {
expect( formState.getErrorMessages( state ) ).toStrictEqual( [ 'invalid' ] );
done();
} );
const controller = testController( {
fieldNames: [ 'firstName' ],
validatorFunction: validatorFunction,
onNewState: onNewState,
} );
controller.handleFieldChange( {
name: 'firstName',
value: 'foo',
} );
} );
} );
describe( 'when there are multiple changes at once', () => {
test( 'only shows errors for the latest values', () => {
return new Promise( ( done ) => {
const validatorFunction = function ( fieldValues, onComplete ) {
onComplete( null, {
firstName: fieldValues.firstName.length > 0 ? [] : [ 'invalid' ],
} );
};
const onNewState = checkNthState( 4, function ( state ) {
expect( formState.getErrorMessages( state ) ).toStrictEqual( [ 'invalid' ] );
done();
} );
const controller = testController( {
fieldNames: [ 'firstName' ],
validatorFunction: validatorFunction,
onNewState: onNewState,
} );
controller.handleFieldChange( {
name: 'firstName',
value: 'foo',
} );
controller.handleFieldChange( {
name: 'firstName',
value: '',
} );
} );
} );
} );
} );
} );
} );
|