Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ driver / form / helpers #287

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
})