import React from 'react';
import TestUtils from 'react-dom/test-utils';
import Formsy, { withFormsy } from './..';
import { InputFactory } from './utils/TestInput';
import immediate from './utils/immediate';
import sinon from 'sinon';
class MyTest extends React.Component {
static defaultProps = { type: 'text' };
handleChange = (event) => {
this.props.setValue(event.target.value);
}
render() {
return ;
}
}
const FormsyTest = withFormsy(MyTest);
export default {
'should reset only changed form element when external error is passed': function (test) {
const form = TestUtils.renderIntoDocument(
invalidate({ foo: 'bar', bar: 'foo' })}>
);
const input = TestUtils.scryRenderedDOMComponentsWithTag(form, 'INPUT')[0];
const inputComponents = TestUtils.scryRenderedComponentsWithType(form, FormsyTest);
form.submit();
test.equal(inputComponents[0].isValid(), false);
test.equal(inputComponents[1].isValid(), false);
TestUtils.Simulate.change(input, {target: {value: 'bar'}});
immediate(() => {
test.equal(inputComponents[0].isValid(), true);
test.equal(inputComponents[1].isValid(), false);
test.done();
});
},
'should let normal validation take over when component with external error is changed': function (test) {
const form = TestUtils.renderIntoDocument(
invalidate({ foo: 'bar' })}>
);
const input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
const inputComponent = TestUtils.findRenderedComponentWithType(form, FormsyTest);
form.submit();
test.equal(inputComponent.isValid(), false);
TestUtils.Simulate.change(input, {target: {value: 'bar'}});
immediate(() => {
test.equal(inputComponent.getValue(), 'bar');
test.equal(inputComponent.isValid(), false);
test.done();
});
},
'should trigger an onValid handler, if passed, when form is valid': function (test) {
const onValid = sinon.spy();
const onInvalid = sinon.spy();
TestUtils.renderIntoDocument(
);
test.equal(onValid.called, true);
test.equal(onInvalid.called, false);
test.done();
},
'should trigger an onInvalid handler, if passed, when form is invalid': function (test) {
const onValid = sinon.spy();
const onInvalid = sinon.spy();
TestUtils.renderIntoDocument(
);
test.equal(onValid.called, false);
test.equal(onInvalid.called, true);
test.done();
},
'should be able to use provided validate function': function (test) {
let isValid = false;
const CustomInput = InputFactory({
componentDidMount: function() {
isValid = this.props.isValid();
}
});
const form = TestUtils.renderIntoDocument(
);
const input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
test.equal(isValid, true);
test.done();
},
'should provide invalidate callback on onValidSubmit': function (test) {
class TestForm extends React.Component {
render() {
return (
invalidate({ foo: 'bar' })}>
);
}
}
const form = TestUtils.renderIntoDocument();
const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form');
const input = TestUtils.findRenderedComponentWithType(form, FormsyTest);
TestUtils.Simulate.submit(formEl);
test.equal(input.isValid(), false);
test.done();
},
'should provide invalidate callback on onInvalidSubmit': function (test) {
class TestForm extends React.Component {
render() {
return (
invalidate({ foo: 'bar' })}>
);
}
}
const form = TestUtils.renderIntoDocument();
const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form');
const input = TestUtils.findRenderedComponentWithType(form, FormsyTest);
TestUtils.Simulate.submit(formEl);
test.equal(input.getErrorMessage(), 'bar');
test.done();
},
'should not invalidate inputs on external errors with preventExternalInvalidation prop': function (test) {
class TestForm extends React.Component {
render() {
return (
invalidate({ foo: 'bar' })}>
);
}
}
const form = TestUtils.renderIntoDocument();
const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form');
const input = TestUtils.findRenderedComponentWithType(form, FormsyTest);
TestUtils.Simulate.submit(formEl);
test.equal(input.isValid(), true);
test.done();
},
'should invalidate inputs on external errors without preventExternalInvalidation prop': function (test) {
class TestForm extends React.Component {
render() {
return (
invalidate({ foo: 'bar' })}>
);
}
}
const form = TestUtils.renderIntoDocument();
const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form');
const input = TestUtils.findRenderedComponentWithType(form, FormsyTest);
TestUtils.Simulate.submit(formEl);
test.equal(input.isValid(), false);
test.done();
}
};