-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathOnBlur.test.js
58 lines (53 loc) · 1.56 KB
/
OnBlur.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React from 'react'
import { render, fireEvent, cleanup } from '@testing-library/react'
import { Form, Field } from 'react-final-form'
import OnBlur from './OnBlur'
const onSubmitMock = () => {}
describe('OnBlur', () => {
afterEach(cleanup)
it('should not call listener on first render', () => {
const spy = jest.fn()
render(
<Form onSubmit={onSubmitMock}>
{() => <OnBlur name="foo">{spy}</OnBlur>}
</Form>
)
expect(spy).not.toHaveBeenCalled()
})
it('should not call listener on focus', () => {
const spy = jest.fn()
const { getByTestId } = render(
<Form onSubmit={onSubmitMock}>
{() => (
<form>
<Field name="name" component="input" data-testid="name" />
<OnBlur name="name">{spy}</OnBlur>
</form>
)}
</Form>
)
expect(spy).not.toHaveBeenCalled()
fireEvent.focus(getByTestId('name'))
expect(spy).not.toHaveBeenCalled()
})
it('should call listener on blur', () => {
const spy = jest.fn()
const { getByTestId } = render(
<Form onSubmit={onSubmitMock}>
{() => (
<form>
<Field name="name" component="input" data-testid="name" />
<OnBlur name="name">{spy}</OnBlur>
</form>
)}
</Form>
)
expect(spy).not.toHaveBeenCalled()
fireEvent.focus(getByTestId('name'))
expect(spy).not.toHaveBeenCalled()
fireEvent.blur(getByTestId('name'))
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith()
})
})