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

Commit 773faea

Browse files
committed
Adding tracking support for <Field>, <Form>, and <Errors> + unit tests
1 parent f4ce0aa commit 773faea

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

src/components/errors-component.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,16 @@ Errors.defaultProps = {
130130
};
131131

132132
function selector(state, { model }) {
133-
const fieldValue = getFieldFromState(state, model);
133+
const modelString = typeof model === 'function'
134+
? model(state)
135+
: model;
136+
137+
const fieldValue = getFieldFromState(state, modelString);
134138

135139
return {
136140
...state,
137-
modelValue: _get(state, model),
141+
model: modelString,
142+
modelValue: _get(state, modelString),
138143
fieldValue,
139144
};
140145
}

src/components/field-component.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const {
1515
} = actions;
1616

1717
function selector(state, { model }) {
18-
const stringModel = typeof model === 'function'
18+
const modelString = typeof model === 'function'
1919
? model(state)
2020
: model;
2121

2222
return {
23-
model: stringModel,
24-
modelValue: _get(state, stringModel),
23+
model: modelString,
24+
modelValue: _get(state, modelString),
2525
};
2626
}
2727

src/components/form-component.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,14 @@ Form.defaultProps = {
140140
};
141141

142142
function selector(state, { model }) {
143+
const modelString = typeof model === 'function'
144+
? model(state)
145+
: model;
146+
143147
return {
144148
...state,
145-
modelValue: _get(state, model),
146-
formValue: getForm(state, model),
149+
modelValue: _get(state, modelString),
150+
formValue: getForm(state, modelString),
147151
};
148152
}
149153

src/utils/track.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function track(model, predicate) {
66
const [
77
parentModelPath,
88
childModelPath = '',
9-
] = model.split('[]');
9+
] = model.split(/\[\]\.?/);
1010
const parentValue = get(state, parentModelPath);
1111

1212
return [

test/tracking-spec.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Provider } from 'react-redux';
55
import thunk from 'redux-thunk';
66
import TestUtils from 'react-addons-test-utils';
77

8-
import { Field, formReducer, modelReducer, track } from '../src';
8+
import { Field, Errors, formReducer, modelReducer, track } from '../src';
99

1010
const state = {
1111
deep: {
@@ -72,3 +72,39 @@ describe('track() with <Field model="...">', () => {
7272
{ id: 2, value: 'testing' });
7373
});
7474
});
75+
76+
describe('track() with <Errors model="...">', () => {
77+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
78+
testForm: formReducer('test'),
79+
test: modelReducer('test', state),
80+
}));
81+
82+
const tracker = track('test.deep.deeper[].value', { id: 2 });
83+
84+
const form = TestUtils.renderIntoDocument(
85+
<Provider store={store}>
86+
<form>
87+
<Field model={tracker}
88+
errors={{
89+
foo: () => 'foo error',
90+
bar: () => 'bar error',
91+
}}
92+
>
93+
<input type="text" />
94+
</Field>
95+
<Errors model={tracker} />
96+
</form>
97+
</Provider>
98+
);
99+
100+
const input = TestUtils.findRenderedDOMComponentWithTag(form, 'input');
101+
102+
it('should successfully show errors for the proper model', () => {
103+
input.value = 'testing';
104+
TestUtils.Simulate.change(input);
105+
106+
const errors = TestUtils.scryRenderedDOMComponentsWithTag(form, 'span');
107+
108+
assert.lengthOf(errors, 2);
109+
});
110+
});

0 commit comments

Comments
 (0)