Skip to content

Commit 7a7d03a

Browse files
jpraskerikras
authored andcommitted
Feature/add tests (#40)
* Add react-hooks-testing-library as a dependency * Add test scripts * Create tests for useField * Create tests for useFormState * Create tests for useForm
1 parent 8682a3f commit 7a7d03a

7 files changed

+474
-5
lines changed

.babelrc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
],
1212
"@babel/preset-react"
1313
],
14-
"env": {
15-
"test": {
16-
"plugins": ["@babel/plugin-transform-react-jsx-source", "istanbul"]
17-
}
18-
},
14+
1915
"plugins": [
2016
"@babel/plugin-syntax-dynamic-import",
2117
"@babel/plugin-syntax-import-meta",

package-lock.json

Lines changed: 141 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-scripts.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const rimraf = npsUtils.rimraf
66

77
module.exports = {
88
scripts: {
9+
test: {
10+
default: 'jest --coverage',
11+
watch: 'jest --coverage --watch'
12+
},
913
size: {
1014
description: 'check the size of the bundle',
1115
script: 'bundlesize'

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"scripts": {
1414
"start": "nps",
15+
"test": "nps test",
1516
"doctoc": "doctoc README.md && doctoc docs/faq.md && prettier --write README.md && prettier --write docs/faq.md",
1617
"precommit": "lint-staged && npm start validate",
1718
"prepublish": "npm start validate",
@@ -69,6 +70,7 @@
6970
"prop-types": "^15.6.2",
7071
"react": "^16.8.1",
7172
"react-dom": "^16.8.1",
73+
"react-hooks-testing-library": "^0.4.0",
7274
"rollup": "^1.1.2",
7375
"rollup-plugin-babel": "^4.0.1",
7476
"rollup-plugin-commonjs": "^9.2.0",

src/useField.test.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { renderHook, cleanup } from 'react-hooks-testing-library'
2+
import useField, { all } from './useField'
3+
4+
describe('useField()', () => {
5+
let form, name, subscription
6+
7+
beforeEach(() => {
8+
name = 'foo'
9+
subscription = { value: true }
10+
})
11+
afterEach(cleanup)
12+
13+
describe("form hook parameter's registerField", () => {
14+
beforeEach(() => {
15+
form = {
16+
registerField: jest.fn()
17+
}
18+
})
19+
20+
it('is called with correct params', () => {
21+
renderHook(() => useField(name, form, subscription))
22+
23+
expect(form.registerField).toHaveBeenCalledWith(
24+
name,
25+
expect.any(Function),
26+
subscription
27+
)
28+
})
29+
30+
it('receives all subscriptions by default', () => {
31+
renderHook(() => useField(name, form))
32+
33+
expect(form.registerField).toHaveBeenCalledWith(
34+
name,
35+
expect.any(Function),
36+
all
37+
)
38+
})
39+
})
40+
41+
describe('field input props return object', () => {
42+
let value, blur, change, focus
43+
44+
const setupHook = () => {
45+
const { result } = renderHook(() => useField(name, form, subscription))
46+
const { input } = result.current
47+
48+
return input
49+
}
50+
51+
beforeEach(() => {
52+
value = 'bar'
53+
blur = jest.fn()
54+
change = jest.fn()
55+
focus = jest.fn()
56+
57+
form = {
58+
registerField: jest.fn((name, callback, subscription) =>
59+
callback({ blur, change, focus, value })
60+
)
61+
}
62+
})
63+
64+
it('has the correct name', () => {
65+
const { name: returnName } = setupHook()
66+
67+
expect(returnName).toBe(name)
68+
})
69+
70+
it('has the correct value', () => {
71+
const { value: returnValue } = setupHook()
72+
73+
expect(returnValue).toBe(value)
74+
})
75+
76+
describe('onBlur()', () => {
77+
it('calls the correct event handler', () => {
78+
const { onBlur } = setupHook()
79+
80+
onBlur()
81+
82+
expect(blur).toHaveBeenCalled()
83+
})
84+
})
85+
86+
describe('onChange()', () => {
87+
describe('when event is not an usual input event', () => {
88+
const event = { foo: 'bar' }
89+
90+
it('calls the provided handler with event object', () => {
91+
const { onChange } = setupHook()
92+
93+
onChange(event)
94+
95+
expect(change).toHaveBeenCalledWith(event)
96+
})
97+
})
98+
99+
describe('when event has a value prop', () => {
100+
const event = { target: { value: 'foo' } }
101+
102+
it('calls provided handler with value', () => {
103+
const { onChange } = setupHook()
104+
105+
onChange(event)
106+
107+
expect(change).toHaveBeenLastCalledWith(event.target.value)
108+
})
109+
})
110+
111+
describe('when event has a checked prop', () => {
112+
const event = { target: { type: 'radio', checked: false } }
113+
114+
it('calls provided handler with value', () => {
115+
const { onChange } = setupHook()
116+
117+
onChange(event)
118+
119+
expect(change).toHaveBeenLastCalledWith(event.target.checked)
120+
})
121+
})
122+
})
123+
124+
describe('onFocus()', () => {
125+
it('calls the correct event handler', () => {
126+
const { onFocus } = setupHook()
127+
128+
onFocus()
129+
130+
expect(focus).toHaveBeenCalled()
131+
})
132+
})
133+
})
134+
135+
describe('field meta return object', () => {
136+
let meta
137+
138+
beforeEach(() => {
139+
meta = { name: 'foo', bar: 'bar', biz: 'biz' }
140+
141+
form = {
142+
registerField: jest.fn((name, callback, subscription) =>
143+
callback({ ...meta })
144+
)
145+
}
146+
})
147+
148+
it('has the correct values', () => {
149+
const { result } = renderHook(() => useField(name, form, subscription))
150+
const { meta: returnMeta } = result.current
151+
152+
delete meta.name
153+
154+
expect(returnMeta).toEqual(meta)
155+
})
156+
})
157+
})

0 commit comments

Comments
 (0)