Skip to content

Commit 4cef4df

Browse files
author
Paul LeMarquand
committed
Support React 15.2.0. Update Babel dependencies.
Fixes heilhead#65 Fixes heilhead#66
1 parent f1bcbd0 commit 4cef4df

File tree

6 files changed

+112
-31
lines changed

6 files changed

+112
-31
lines changed

.babelrc

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
{ "presets": ["es2015","react"] }
1+
{
2+
"presets": ["es2015","react"],
3+
"plugins": ["transform-object-rest-spread"]
4+
}

__tests__/ValidationTests.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
import Form from '../src/Form';
3+
import ValidatedInput from '../src/ValidatedInput';
4+
import { mount } from 'enzyme';
5+
import { expect } from 'chai';
6+
import sinon from 'sinon';
7+
8+
// Note: There is an issue with react-bootstrap and mocking. For now the whole node_modules directory has been unmocked.
9+
describe('React bootstrap validation compilation test', () => {
10+
var React = require('react');
11+
var TestUtils = require('react-addons-test-utils');
12+
13+
beforeEach(function() {
14+
15+
});
16+
17+
it('Calls onValidSubmit when input is valid', () => {
18+
const spy = sinon.spy(), invalidSpy = sinon.spy();
19+
const form = mount(
20+
<Form onValidSubmit={spy} onInvalidSubmit={invalidSpy}>
21+
<ValidatedInput name='foo' type='text' value='bar'></ValidatedInput>
22+
</Form>
23+
);
24+
25+
form.simulate('submit');
26+
27+
expect(spy.calledOnce).to.equal(true);
28+
expect(invalidSpy.called).to.equal(false);
29+
});
30+
31+
it('Calls onInvalidSubmit when input is not valid', () => {
32+
const spy = sinon.spy(), invalidSpy = sinon.spy();
33+
const form = mount(
34+
<Form onValidSubmit={invalidSpy} onInvalidSubmit={spy}>
35+
<ValidatedInput name='foo' type='text' validate='required' value=''></ValidatedInput>
36+
</Form>
37+
);
38+
39+
form.simulate('submit');
40+
41+
expect(spy.calledOnce).to.equal(true);
42+
expect(invalidSpy.called).to.equal(false);
43+
});
44+
45+
it('Validates a checkbox', () => {
46+
const spy = sinon.spy(), invalidSpy = sinon.spy();
47+
const form = mount(
48+
<Form onValidSubmit={spy} onInvalidSubmit={invalidSpy}>
49+
<ValidatedInput type='checkbox' name='agree' checked={true} label='I agree to the terms and conditions' validate='isChecked'/>
50+
</Form>
51+
);
52+
53+
form.simulate('submit');
54+
55+
expect(spy.calledOnce).to.equal(true);
56+
expect(invalidSpy.called).to.equal(false);
57+
});
58+
59+
it('Validates a file input', () => {
60+
const spy = sinon.spy(), invalidSpy = sinon.spy();
61+
const form = mount(
62+
<Form onValidSubmit={spy} onInvalidSubmit={invalidSpy}>
63+
<ValidatedInput name='file' type='file' label='Attachments' multiple/>
64+
</Form>
65+
);
66+
67+
form.simulate('submit');
68+
69+
expect(spy.calledOnce).to.equal(true);
70+
expect(invalidSpy.called).to.equal(false);
71+
});
72+
});

gulpfile.js

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ var config = {
4545
devtool: 'source-map'
4646
},
4747
babel: {
48-
optional: ['runtime']
4948
}
5049
};
5150

package.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,34 @@
4242
"devDependencies": {
4343
"babel": "^6.5.2",
4444
"babel-jest": "^10.0.1",
45-
"babel-preset-es2015": "^6.6.0",
45+
"babel-plugin-transform-object-rest-spread": "^6.8.0",
46+
"babel-preset-es2015": "^6.9.0",
4647
"babel-preset-jest": "^10.0.1",
47-
"babel-preset-react": "^6.5.0",
48-
"browserify": "10.x",
49-
"gulp": "3.9.0",
50-
"gulp-babel": "^5.1.0",
48+
"babel-preset-react": "^6.11.1",
49+
"browserify": "^13.0.1",
50+
"chai": "^3.5.0",
51+
"enzyme": "^2.3.0",
52+
"gulp": "^3.9.0",
53+
"gulp-babel": "^6.1.2",
5154
"gulp-rename": "^1.2.2",
5255
"gulp-sourcemaps": "^1.5.2",
5356
"gulp-uglify": "^1.2.0",
5457
"gulp-util": "^3.0.6",
5558
"jest": "^0.1.40",
5659
"jest-cli": "^0.10.0",
5760
"react-addons-test-utils": "^0.14.8",
61+
"sinon": "^1.17.4",
5862
"vinyl-buffer": "^1.0.0",
5963
"vinyl-source-stream": "^1.1.0",
6064
"webpack": "^1.10.5"
6165
},
6266
"dependencies": {
63-
"babel-runtime": "^5.1.10",
67+
"babel-runtime": "^6.9.2",
68+
"browserify": "^13.0.1",
6469
"classnames": "^2.0.0",
70+
"gulp-babel": "^6.1.2",
71+
"react-addons-create-fragment": "^0.14.7",
6572
"react-bootstrap": ">=0.23.0",
66-
"validator": "^3.41.3",
67-
"react-addons-create-fragment": "^0.14.7"
73+
"validator": "^3.41.3"
6874
}
6975
}

src/Form.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import ReactDOM from 'react-dom';
23
import InputContainer from './InputContainer';
34
import ValidatedInput from './ValidatedInput';
45
import RadioGroup from './RadioGroup';
@@ -200,7 +201,7 @@ export default class Form extends InputContainer {
200201

201202
if (typeof this.props.validateOne === 'function') {
202203
result = this.props.validateOne(iptName, value, context, result);
203-
}
204+
}
204205
// if result is !== true, it is considered an error
205206
// it can be either bool or string error
206207
if (result !== true) {
@@ -296,13 +297,13 @@ export default class Form extends InputContainer {
296297
}
297298

298299
let value;
299-
300+
let dom = ReactDOM.findDOMNode(input.refs.control);
300301
if (input.props.type === 'checkbox') {
301-
value = input.getChecked();
302+
value = dom.checked;
302303
} else if (input.props.type === 'file') {
303-
value = input.getInputDOMNode().files;
304+
value = dom.files;
304305
} else {
305-
value = input.getValue();
306+
value = dom.value;
306307
}
307308

308309
return value;

src/ValidatedInput.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import React from 'react';
2-
import { Input } from 'react-bootstrap';
2+
import { FormControl } from 'react-bootstrap';
33

4-
export default class ValidatedInput extends Input {
4+
export default class ValidatedInput extends React.Component {
55
constructor(props) {
66
super(props);
77

8-
if (!props._registerInput || !props._unregisterInput) {
8+
const {validationEvent, validate, errorHelp, _registerInput, _unregisterInput, ...inputProps} = props;
9+
this._registerInput = _registerInput;
10+
this._unregisterInput = _unregisterInput;
11+
this.inputProps = inputProps;
12+
if (!this._registerInput || !this._unregisterInput) {
913
throw new Error('Input must be placed inside the Form component');
1014
}
1115
}
1216

1317
componentWillMount() {
14-
if (Input.prototype.componentWillMount) {
15-
super.componentWillMount();
16-
}
17-
18-
this.props._registerInput(this);
18+
this._registerInput(this);
1919
}
2020

2121
componentWillUnmount() {
22-
if (Input.prototype.componentWillUnmount) {
23-
super.componentWillUnmount();
24-
}
22+
this._unregisterInput(this);
23+
}
2524

26-
this.props._unregisterInput(this);
25+
render() {
26+
return <FormControl ref='control' {...this.inputProps}>{this.props.children}</FormControl>;
2727
}
2828
}
2929

30-
ValidatedInput.propTypes = Object.assign({}, Input.propTypes, {
30+
ValidatedInput.propTypes = {
3131
name : React.PropTypes.string.isRequired,
3232
validationEvent: React.PropTypes.oneOf([
3333
'', 'onChange', 'onBlur', 'onFocus'
@@ -40,8 +40,8 @@ ValidatedInput.propTypes = Object.assign({}, Input.propTypes, {
4040
React.PropTypes.string,
4141
React.PropTypes.object
4242
])
43-
});
43+
};
4444

45-
ValidatedInput.defaultProps = Object.assign({}, Input.defaultProps, {
45+
ValidatedInput.defaultProps = {
4646
validationEvent: ''
47-
});
47+
};

0 commit comments

Comments
 (0)