Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 5cf2044

Browse files
committed
Providing NativeForm (as View) for React Native + unit tests
1 parent b286239 commit 5cf2044

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

src/components/form-component.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ class Form extends Component {
142142
}
143143

144144
Form.propTypes = {
145-
component: PropTypes.oneOfType([
146-
PropTypes.func,
147-
PropTypes.string,
148-
]),
145+
component: PropTypes.any,
149146
validators: PropTypes.object,
150147
errors: PropTypes.object,
151148
validateOn: PropTypes.oneOf([

src/native/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import React from 'react';
2+
3+
import Form from '../components/form-component';
14
import { createFieldClass } from '../components/field-component';
25

36
const View = process.env.NODE_ENV !== 'test'
@@ -44,4 +47,18 @@ const NativeField = createFieldClass({
4447
component: View,
4548
});
4649

47-
export { NativeField as Field };
50+
class NativeForm extends React.Component {
51+
render() {
52+
return (
53+
<Form
54+
component={View}
55+
{...this.props}
56+
/>
57+
);
58+
}
59+
}
60+
61+
export {
62+
NativeField as Field,
63+
NativeForm as Form,
64+
};

test/custom-field-component-spec.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { applyMiddleware, combineReducers, createStore } from 'redux';
66
import { Provider } from 'react-redux';
77
import thunk from 'redux-thunk';
88

9-
import { Field as NativeField } from '../src/native';
9+
import {
10+
Field as NativeField,
11+
Form as NativeForm,
12+
} from '../src/native';
1013
import { controls, createFieldClass, formReducer, modelReducer, Field } from '../src';
1114

1215
describe('controls props mapping', () => {
@@ -190,7 +193,7 @@ describe('React Native <Field /> components', () => {
190193
assert.ok(NativeField);
191194
});
192195

193-
it('should map native components', () => {
196+
it('should map the native field component', () => {
194197
// Placeholder div, for now
195198
class TextField extends Component {
196199
render() {
@@ -200,4 +203,22 @@ describe('React Native <Field /> components', () => {
200203

201204
assert.ok(<NativeField model="foo.bar"><TextField /></NativeField>);
202205
});
206+
207+
it('should render a Form component as a View', () => {
208+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
209+
testForm: formReducer('test'),
210+
test: modelReducer('test', { foo: 'bar' }),
211+
}));
212+
213+
const form = TestUtils.renderIntoDocument(
214+
<Provider store={store}>
215+
<NativeForm model="test" />
216+
</Provider>
217+
);
218+
219+
// Placeholder div, for now
220+
const formElement = TestUtils.findRenderedDOMComponentWithTag(form, 'div');
221+
222+
assert.ok(formElement);
223+
});
203224
});

test/errors-component-spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,36 @@ describe('<Errors />', () => {
521521
assert.equal(components[1].innerHTML, 'bar error');
522522
});
523523
});
524+
525+
xdescribe('deep model paths', () => {
526+
it('should work with deep model paths', () => {
527+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
528+
forms: combineReducers({
529+
testForm: formReducer('test', {}),
530+
test: modelReducer('test'),
531+
}),
532+
}));
533+
534+
// assert.doesNotThrow(() => {
535+
const form = TestUtils.renderIntoDocument(
536+
<Provider store={store}>
537+
<form>
538+
<Errors model="forms.test.foo"
539+
messages={{
540+
required: 'This field is required',
541+
}}
542+
/>
543+
<Field model="forms.test.foo"
544+
validators={{
545+
required: (v) => v && v.length,
546+
}}
547+
>
548+
<input type="text" />
549+
</Field>
550+
</form>
551+
</Provider>
552+
);
553+
// });
554+
});
555+
});
524556
});

0 commit comments

Comments
 (0)