-
Notifications
You must be signed in to change notification settings - Fork 2k
/
useDrag.spec.tsx
54 lines (49 loc) · 1.3 KB
/
useDrag.spec.tsx
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
import { useDrag } from '../useDrag'
import { wrapWithBackend } from 'react-dnd-test-utils'
import { render } from '@testing-library/react'
describe('The useDrag hook', () => {
it('throws if rendered outside of a React-DnD tree', () => {
function Component() {
const [, drag] = useDrag({
type: 'box',
})
return <div ref={drag} />
}
const err = console.error
try {
const errorMock = jest.fn()
console.error = errorMock
expect(() => render(<Component />)).toThrow(/Expected drag drop context/)
} finally {
console.error = err
}
})
it('throws if type is null', () => {
function Component() {
const [, drag] = useDrag({
type: null,
})
return <div ref={drag} />
}
const Wrapped = wrapWithBackend(Component)
const err = console.error
try {
const errorMock = jest.fn()
console.error = errorMock
expect(() => render(<Wrapped />)).toThrow(/spec.type must be defined/)
} finally {
console.error = err
}
})
it('can be used inside of a React-DnD context', async () => {
const Wrapped = wrapWithBackend(function Component() {
const [, drag] = useDrag({
type: 'box',
})
return <div ref={drag} role="root" />
})
const result = render(<Wrapped />)
const root = await result.findByRole('root')
expect(root.draggable).toBeTruthy()
})
})