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

Commit 2ac059c

Browse files
authored
Merge pull request #1184 from brycesenz/924_form_cache_clearing
924 form cache clearing
2 parents bfed523 + 5b065b1 commit 2ac059c

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

src/components/form-component.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import getValidity from '../utils/get-validity';
1010
import invertValidators from '../utils/invert-validators';
1111
import isValidityInvalid from '../utils/is-validity-invalid';
1212
import isValid from '../form/is-valid';
13-
import getForm from '../utils/get-form';
13+
import getForm, { clearGetFormCacheForModel } from '../utils/get-form';
1414
import getModel from '../utils/get-model';
1515
import getField from '../utils/get-field';
1616
import deepCompareChildren from '../utils/deep-compare-children';
@@ -409,6 +409,7 @@ function createFormClass(s = defaultStrategy) {
409409

410410
function mapStateToProps(state, { model }) {
411411
const modelString = getModel(model, state);
412+
clearGetFormCacheForModel(modelString);
412413
const form = s.getForm(state, modelString);
413414

414415
invariant(form,

src/utils/get-form.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ let formStateKeyCache = {};
7676
export const clearGetFormCache =
7777
() => formStateKeyCache = {}; // eslint-disable-line no-return-assign
7878

79+
export const clearGetFormCacheForModel =
80+
// eslint-disable-next-line
81+
(modelString) => delete formStateKeyCache[modelString];
82+
7983
const getFormStateKeyCached = (() => (state, modelString, s = defaultStrategy) => {
8084
if (formStateKeyCache[modelString]) return formStateKeyCache[modelString];
8185

test/form-component-spec.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,106 @@ Object.keys(testContexts).forEach((testKey) => {
20422042
});
20432043
});
20442044

2045+
2046+
describe('form validation with models in collections', () => {
2047+
it('validates if item in initialState', () => {
2048+
const initialState = getInitialState({ test1: {} });
2049+
2050+
const store = testCreateStore({
2051+
test: modelReducer('tests', initialState),
2052+
testForm: formReducer('tests', initialState),
2053+
});
2054+
2055+
let handleSubmitFailedCalledWith = null;
2056+
2057+
function handleSubmitFailed(val) {
2058+
handleSubmitFailedCalledWith = val;
2059+
}
2060+
2061+
const form = TestUtils.renderIntoDocument(
2062+
<Provider store={store}>
2063+
<Form
2064+
model="tests.test1"
2065+
onSubmitFailed={handleSubmitFailed}
2066+
validators={{
2067+
foo: (val) => val,
2068+
}}
2069+
>
2070+
<Control model=".foo" />
2071+
</Form>
2072+
</Provider>
2073+
);
2074+
2075+
const formNode = TestUtils.findRenderedDOMComponentWithTag(form, 'form');
2076+
2077+
TestUtils.Simulate.submit(formNode);
2078+
2079+
assert.containSubset(handleSubmitFailedCalledWith, {
2080+
$form: {
2081+
model: 'tests.test1',
2082+
valid: false,
2083+
},
2084+
foo: {
2085+
model: 'tests.test1.foo',
2086+
valid: false,
2087+
errors: true,
2088+
},
2089+
});
2090+
2091+
assert.isFalse(store.getState().testForm.test1.$form.pending);
2092+
assert.isTrue(store.getState().testForm.test1.$form.submitFailed);
2093+
});
2094+
2095+
it('validates if item not in initialState', () => {
2096+
const initialState = getInitialState({ test1: {} });
2097+
2098+
const store = testCreateStore({
2099+
test: modelReducer('tests', initialState),
2100+
testForm: formReducer('tests', initialState),
2101+
});
2102+
2103+
let handleSubmitFailedCalledWith = null;
2104+
2105+
function handleSubmitFailed(val) {
2106+
handleSubmitFailedCalledWith = val;
2107+
}
2108+
2109+
const form = TestUtils.renderIntoDocument(
2110+
<Provider store={store}>
2111+
<Form
2112+
model="tests.test2"
2113+
onSubmitFailed={handleSubmitFailed}
2114+
validators={{
2115+
foo: (val) => val,
2116+
}}
2117+
>
2118+
<Control model=".foo" />
2119+
</Form>
2120+
</Provider>
2121+
);
2122+
2123+
const formNode = TestUtils.findRenderedDOMComponentWithTag(form, 'form');
2124+
2125+
TestUtils.Simulate.submit(formNode);
2126+
2127+
assert.containSubset(handleSubmitFailedCalledWith, {
2128+
$form: {
2129+
model: 'tests.test2',
2130+
valid: false,
2131+
},
2132+
foo: {
2133+
model: 'tests.test2.foo',
2134+
valid: false,
2135+
errors: true,
2136+
},
2137+
});
2138+
2139+
assert.isFalse(store.getState().testForm.test2.$form.pending);
2140+
assert.isTrue(store.getState().testForm.test2.$form.submitFailed);
2141+
});
2142+
});
2143+
2144+
20452145
describe('getDispatch() prop', () => {
20462146
it('should provide dispatch to callback', (done) => {
20472147
const initialState = getInitialState({ foo: '' });

0 commit comments

Comments
 (0)