Skip to content

Commit 88a1acb

Browse files
committed
Fixed test errors
1 parent f6cdcaf commit 88a1acb

File tree

7 files changed

+28
-23
lines changed

7 files changed

+28
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ Shows how to create fields and attach them to `<input/>` elements.
102102

103103
The following can be imported from `react-final-form-hooks`.
104104

105-
### `useField`
105+
### `useField(name: string, form: Form, validate?: (value:any) => any, subscription: Object)`
106106

107107
Returns an object similar to [`FieldRenderProps`](https://github.com/final-form/react-final-form#fieldrenderprops).
108108

109-
### `useForm`
109+
### `useForm(onSubmit: (values:Object) => Promise | void, validate: (values:Object) => Object)`
110110

111111
Returns an object similar to [`FormRenderProps`](https://github.com/final-form/react-final-form#formrenderprops).
112112

package-scripts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const rimraf = npsUtils.rimraf
77
module.exports = {
88
scripts: {
99
test: {
10-
default: 'jest --coverage',
10+
default: 'jest --coverage --detectOpenHandles',
1111
watch: 'jest --coverage --watch'
1212
},
1313
size: {
@@ -62,7 +62,7 @@ module.exports = {
6262
validate: {
6363
description:
6464
'This runs several scripts to make sure things look good before committing or on clean install',
65-
default: concurrent.nps('lint', 'build.andTest', 'typescript')
65+
default: concurrent.nps('lint', 'build.andTest', 'typescript', 'test')
6666
}
6767
},
6868
options: {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
],
1313
"scripts": {
1414
"start": "nps",
15-
"test": "nps test",
1615
"doctoc": "doctoc README.md && doctoc docs/faq.md && prettier --write README.md && prettier --write docs/faq.md",
1716
"precommit": "lint-staged && npm start validate",
1817
"prepublish": "npm start validate",
1918
"postinstall": "node ./scripts/postinstall.js || exit 0",
20-
"test": "NODE_ENV=test jest -i --detectOpenHandles"
19+
"test": "NODE_ENV=test nps test"
2120
},
2221
"author": "Erik Rasmussen <[email protected]> (http://github.com/erikras)",
2322
"license": "MIT",

src/useField.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const useField = (name, form, validate, subscription = all) => {
4141
subscription,
4242
options
4343
),
44+
// eslint-disable-next-line react-hooks/exhaustive-deps
4445
[name, form, validate, ...subscriptionToInputs(subscription)]
4546
)
4647
let { blur, change, focus, value, ...meta } = state || {}

src/useField.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('useField()', () => {
1212
name = 'foo'
1313
subscription = { value: true }
1414
container = document.createElement('div')
15+
document.body.appendChild(container)
1516
})
1617
afterEach(() => {
1718
document.body.removeChild(container)
@@ -27,12 +28,13 @@ describe('useField()', () => {
2728
})
2829

2930
it('is called with correct params', () => {
30-
renderHook(() => useField(name, form, subscription))
31+
renderHook(() => useField(name, form, undefined, subscription))
3132

3233
expect(form.registerField).toHaveBeenCalledWith(
3334
name,
3435
expect.any(Function),
35-
subscription
36+
subscription,
37+
undefined
3638
)
3739
})
3840

@@ -42,7 +44,8 @@ describe('useField()', () => {
4244
expect(form.registerField).toHaveBeenCalledWith(
4345
name,
4446
expect.any(Function),
45-
all
47+
all,
48+
undefined
4649
)
4750
})
4851
})

src/useForm.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ const useForm = ({
2222
const form = useMemoOnce(() => createForm(config))
2323
const prevConfig = useRef(config)
2424
const state = useFormState(form, subscription)
25-
const handleSubmit = useCallback(event => {
26-
if (event) {
27-
if (typeof event.preventDefault === 'function') {
28-
event.preventDefault()
25+
const handleSubmit = useCallback(
26+
event => {
27+
if (event) {
28+
if (typeof event.preventDefault === 'function') {
29+
event.preventDefault()
30+
}
31+
if (typeof event.stopPropagation === 'function') {
32+
event.stopPropagation()
33+
}
2934
}
30-
if (typeof event.stopPropagation === 'function') {
31-
event.stopPropagation()
32-
}
33-
}
34-
return form.submit()
35-
}, [])
35+
return form.submit()
36+
},
37+
[form]
38+
)
3639

3740
useEffect(() => {
3841
if (config === prevConfig.current) {

src/useFormState.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ const subscriptionToInputs = subscription =>
1515
const useFormState = (form, subscription = all) => {
1616
const [state, setState] = useState(() => form.getState())
1717

18-
useEffect(() => form.subscribe(setState, subscription), [
19-
form,
20-
...subscriptionToInputs(subscription)
21-
])
18+
const deps = subscriptionToInputs(subscription)
19+
// eslint-disable-next-line react-hooks/exhaustive-deps
20+
useEffect(() => form.subscribe(setState, subscription), [form, ...deps])
2221

2322
return state
2423
}

0 commit comments

Comments
 (0)