Skip to content

Commit

Permalink
v4.1.0. fixes #370
Browse files Browse the repository at this point in the history
  • Loading branch information
erikras committed Jan 2, 2016
1 parent 2242bb4 commit 09f2186
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-form",
"version": "4.0.7",
"version": "4.1.0",
"description": "A higher order component decorator for forms using Redux and React",
"main": "./lib/index.js",
"jsnext:main": "./src/index.js",
Expand Down
105 changes: 101 additions & 4 deletions src/__tests__/createReduxForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ describe('createReduxForm', () => {
});
});

it('should initialize an array field without blowing away existing value', () => {
it('should initialize an array field, blowing away existing value', () => {
const store = makeStore();
const form = 'testForm';
const Decorated = reduxForm({
Expand Down Expand Up @@ -1110,9 +1110,9 @@ describe('createReduxForm', () => {
expectField({
field: stub.props.fields.children,
name: 'children',
value: [1, 2],
value: [3, 4],
valid: true,
dirty: true,
dirty: false,
error: undefined,
touched: false,
visited: false
Expand All @@ -1121,7 +1121,7 @@ describe('createReduxForm', () => {
expect(store.getState().form.testForm.children)
.toEqual({
initial: [3, 4],
value: [1, 2]
value: [3, 4]
});
// reset form to newly initialized values
stub.props.resetForm();
Expand All @@ -1137,4 +1137,101 @@ describe('createReduxForm', () => {
visited: false
});
});

it('should only initialize on mount once', () => {
const store = makeStore();
const form = 'testForm';
const Decorated = reduxForm({
form,
fields: ['name']
})(Form);
const dom = TestUtils.renderIntoDocument(
<Provider store={store}>
<Decorated initialValues={{name: 'Bob'}}/>
</Provider>
);
const stub = TestUtils.findRenderedComponentWithType(dom, Form);

// check value
expectField({
field: stub.props.fields.name,
name: 'name',
value: 'Bob',
valid: true,
dirty: false,
error: undefined,
touched: false,
visited: false
});
// check state
expect(store.getState().form.testForm.name)
.toEqual({
initial: 'Bob',
value: 'Bob'
});
// set value
stub.props.fields.name.onChange('Dan');
// check value
expectField({
field: stub.props.fields.name,
name: 'name',
value: 'Dan',
valid: true,
dirty: true,
error: undefined,
touched: false,
visited: false
});
// check state
expect(store.getState().form.testForm.name)
.toEqual({
initial: 'Bob',
value: 'Dan'
});

// should NOT dispatch INITIALIZE this time
const dom2 = TestUtils.renderIntoDocument(
<Provider store={store}>
<Decorated initialValues={{name: 'Bob'}}/>
</Provider>
);
const stub2 = TestUtils.findRenderedComponentWithType(dom2, Form);
// check that value is unchanged
expectField({
field: stub2.props.fields.name,
name: 'name',
value: 'Dan',
valid: true,
dirty: true,
error: undefined,
touched: false,
visited: false
});
// check state
expect(store.getState().form.testForm.name)
.toEqual({
initial: 'Bob',
value: 'Dan'
});

// manually initialize new values
stub.props.initializeForm({name: 'Tom'});
// check value
expectField({
field: stub2.props.fields.name,
name: 'name',
value: 'Tom',
valid: true,
dirty: false,
error: undefined,
touched: false,
visited: false
});
// check state
expect(store.getState().form.testForm.name)
.toEqual({
initial: 'Tom',
value: 'Tom'
});
});
});
7 changes: 5 additions & 2 deletions src/__tests__/initializeState.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,14 @@ describe('initializeState', () => {
expect(isFieldValue(result.bar)).toBe(true);
});

it('should initialize array values to state, not changing existing values', () => {
it('should initialize array values to state, changing existing values', () => {
const result = initializeState({
animals: ['cat', 'dog', 'rat'],
bar: []
}, ['animals', 'bar'], {
animals: {
value: ['hog', 'pig', 'doe']
},
bar: {
value: [{deeper: 42}]
}
Expand All @@ -179,7 +182,7 @@ describe('initializeState', () => {
},
bar: {
initial: [],
value: [{deeper: 42}]
value: []
}
});
expect(isFieldValue(result.animals)).toBe(true);
Expand Down

0 comments on commit 09f2186

Please sign in to comment.