Skip to content

Commit

Permalink
✨ driver / form / helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif authored Jan 14, 2019
1 parent 2d7812e commit a95adee
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/drivers/form/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ const mapStore = inject((store, { formName, field }, drivers) => {

export default mapStore(Component)
```

## Helpers
- `drivers.form.getUpdatedValues(action)` will return an _object_ of all updated pair (field name -> field value) for the given action.
- `drivers.form.getUpdatedEntries(action)` will return an _array_ of all updated pair (field name -> field value) for the given action.
- `drivers.form.getUpdatedFieldNames(action)` will return an _array_ of all updated field names for the given action.
33 changes: 33 additions & 0 deletions packages/drivers/form/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,39 @@ Object {
}
`;

exports[`drivers/form bulk mode should returns updated field entries 1`] = `
Object {
"entries": Array [
Array [
"first",
true,
],
Array [
"second",
"ok",
],
],
}
`;

exports[`drivers/form bulk mode should returns updated field names 1`] = `
Object {
"fieldNames": Array [
"first",
"second",
],
}
`;

exports[`drivers/form bulk mode should returns updated field names and values 1`] = `
Object {
"fieldValues": Object {
"first": true,
"second": "ok",
},
}
`;

exports[`drivers/form bulk mode should set errors 1`] = `
Object {
"state": Object {
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/form/dist/index.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/drivers/form/dist/index.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions packages/drivers/form/misc/test-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,66 @@ export default (driver) => {
state,
}).toMatchSnapshot()
})

it('should returns updated field names', () => {
// run implementation
let fieldNames
runReaction()((action, store, { form }) => {
fieldNames = form.getUpdatedFieldNames({
payload: {
'@@form-name': 'myForm',
'@@form-fields': ['first', 'second'],
first: true,
second: 'ok',
},
})
})

// assert
expect({
fieldNames,
}).toMatchSnapshot()
})

it('should returns updated field names and values', () => {
// run implementation
let fieldValues
runReaction()((action, store, { form }) => {
fieldValues = form.getUpdatedValues({
payload: {
'@@form-name': 'myForm',
'@@form-fields': ['first', 'second'],
first: true,
second: 'ok',
},
})
})

// assert
expect({
fieldValues,
}).toMatchSnapshot()
})

it('should returns updated field entries', () => {
// run implementation
let entries
runReaction()((action, store, { form }) => {
entries = form.getUpdatedEntries({
payload: {
'@@form-name': 'myForm',
'@@form-fields': ['first', 'second'],
first: true,
second: 'ok',
},
})
})

// assert
expect({
entries,
}).toMatchSnapshot()
})
})
})
}
33 changes: 33 additions & 0 deletions packages/drivers/form/src/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,39 @@ Object {
}
`;

exports[`drivers/form bulk mode should returns updated field entries 1`] = `
Object {
"entries": Array [
Array [
"first",
true,
],
Array [
"second",
"ok",
],
],
}
`;

exports[`drivers/form bulk mode should returns updated field names 1`] = `
Object {
"fieldNames": Array [
"first",
"second",
],
}
`;

exports[`drivers/form bulk mode should returns updated field names and values 1`] = `
Object {
"fieldValues": Object {
"first": true,
"second": "ok",
},
}
`;

exports[`drivers/form bulk mode should set errors 1`] = `
Object {
"state": Object {
Expand Down
2 changes: 2 additions & 0 deletions packages/drivers/form/src/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import actions from './actions'
import bulkActions from './bulk.actions'
import selectors from './selectors'
import utils from './utils'
import helpers from './helpers'

export default ({
path = 'form',
Expand Down Expand Up @@ -36,6 +37,7 @@ export default ({
}),
bulkActions({ keyName, keyFields })(state),
utils(state),
helpers({ keyName, keyFields }),
)
},
}
Expand Down
21 changes: 21 additions & 0 deletions packages/drivers/form/src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const getUpdatedValues = ({ keyName, keyFields }) => (action) => {
const values = { ...action.payload }
delete values[keyName]
delete values[keyFields]

return values
}

const getUpdatedFieldNames = ({ keyFields }) => action => (
action.payload[keyFields]
)

const getUpdatedEntries = keys => action => (
Object.entries(getUpdatedValues(keys)(action))
)

export default keys => ({
getUpdatedFieldNames: getUpdatedFieldNames(keys),
getUpdatedValues: getUpdatedValues(keys),
getUpdatedEntries: getUpdatedEntries(keys),
})

0 comments on commit a95adee

Please sign in to comment.