import React from 'react'; import TestUtils from 'react-addons-test-utils'; import Formsy from './..'; import TestInput, {InputFactory} from './utils/TestInput'; import immediate from './utils/immediate'; import sinon from 'sinon'; 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, TestInput); 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, TestInput); 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() { isValid = this.isValid(); } }); const form = TestUtils.renderIntoDocument( ); const input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT'); test.equal(isValid, true); test.done(); }, 'should provide invalidate callback on onValiSubmit': function (test) { const TestForm = React.createClass({ render() { return ( invalidate({ foo: 'bar' })}> ); } }); const form = TestUtils.renderIntoDocument(); const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form'); const input = TestUtils.findRenderedComponentWithType(form, TestInput); TestUtils.Simulate.submit(formEl); test.equal(input.isValid(), false); test.done(); }, 'should provide invalidate callback on onInvalidSubmit': function (test) { const TestForm = React.createClass({ render() { return ( invalidate({ foo: 'bar' })}> ); } }); const form = TestUtils.renderIntoDocument(); const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form'); const input = TestUtils.findRenderedComponentWithType(form, TestInput); TestUtils.Simulate.submit(formEl); test.equal(input.getErrorMessage(), 'bar'); test.done(); }, 'should not invalidate inputs on external errors with preventExternalInvalidation prop': function (test) { const TestForm = React.createClass({ render() { return ( invalidate({ foo: 'bar' })}> ); } }); const form = TestUtils.renderIntoDocument(); const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form'); const input = TestUtils.findRenderedComponentWithType(form, TestInput); TestUtils.Simulate.submit(formEl); test.equal(input.isValid(), true); test.done(); }, 'should invalidate inputs on external errors without preventExternalInvalidation prop': function (test) { const TestForm = React.createClass({ render() { return ( invalidate({ foo: 'bar' })}> ); } }); const form = TestUtils.renderIntoDocument(); const formEl = TestUtils.findRenderedDOMComponentWithTag(form, 'form'); const input = TestUtils.findRenderedComponentWithType(form, TestInput); TestUtils.Simulate.submit(formEl); test.equal(input.isValid(), false); test.done(); } };