|
| 1 | +import {renderHook} from '@testing-library/react-hooks' |
1 | 2 | import {
|
2 | 3 | getItemIndex,
|
3 | 4 | defaultProps,
|
4 | 5 | getInitialValue,
|
5 | 6 | getDefaultValue,
|
| 7 | + useMouseAndTouchTracker, |
6 | 8 | } from '../utils'
|
7 | 9 |
|
8 | 10 | describe('utils', () => {
|
@@ -63,4 +65,77 @@ describe('utils', () => {
|
63 | 65 |
|
64 | 66 | expect(value).toEqual(defaults.bogusValue)
|
65 | 67 | })
|
| 68 | + |
| 69 | + describe('useMouseAndTouchTracker', () => { |
| 70 | + test('renders without error', () => { |
| 71 | + expect(() => { |
| 72 | + renderHook(() => |
| 73 | + useMouseAndTouchTracker(false, [], undefined, jest.fn()), |
| 74 | + ) |
| 75 | + }).not.toThrowError() |
| 76 | + }) |
| 77 | + |
| 78 | + test('adds and removes listeners to environment', () => { |
| 79 | + const environment = { |
| 80 | + addEventListener: jest.fn(), |
| 81 | + removeEventListener: jest.fn(), |
| 82 | + } |
| 83 | + |
| 84 | + const {unmount, result} = renderHook(() => |
| 85 | + useMouseAndTouchTracker(false, [], environment, jest.fn()), |
| 86 | + ) |
| 87 | + |
| 88 | + expect(environment.addEventListener).toHaveBeenCalledTimes(5) |
| 89 | + expect(environment.addEventListener).toHaveBeenCalledWith( |
| 90 | + 'mousedown', |
| 91 | + expect.any(Function), |
| 92 | + ) |
| 93 | + expect(environment.addEventListener).toHaveBeenCalledWith( |
| 94 | + 'mouseup', |
| 95 | + expect.any(Function), |
| 96 | + ) |
| 97 | + expect(environment.addEventListener).toHaveBeenCalledWith( |
| 98 | + 'touchstart', |
| 99 | + expect.any(Function), |
| 100 | + ) |
| 101 | + expect(environment.addEventListener).toHaveBeenCalledWith( |
| 102 | + 'touchmove', |
| 103 | + expect.any(Function), |
| 104 | + ) |
| 105 | + expect(environment.addEventListener).toHaveBeenCalledWith( |
| 106 | + 'touchend', |
| 107 | + expect.any(Function), |
| 108 | + ) |
| 109 | + expect(environment.removeEventListener).not.toHaveBeenCalled() |
| 110 | + |
| 111 | + unmount() |
| 112 | + |
| 113 | + expect(environment.addEventListener).toHaveBeenCalledTimes(5) |
| 114 | + expect(environment.removeEventListener).toHaveBeenCalledTimes(5) |
| 115 | + expect(environment.removeEventListener).toHaveBeenCalledWith( |
| 116 | + 'mousedown', |
| 117 | + expect.any(Function), |
| 118 | + ) |
| 119 | + expect(environment.removeEventListener).toHaveBeenCalledWith( |
| 120 | + 'mouseup', |
| 121 | + expect.any(Function), |
| 122 | + ) |
| 123 | + expect(environment.removeEventListener).toHaveBeenCalledWith( |
| 124 | + 'touchstart', |
| 125 | + expect.any(Function), |
| 126 | + ) |
| 127 | + expect(environment.removeEventListener).toHaveBeenCalledWith( |
| 128 | + 'touchmove', |
| 129 | + expect.any(Function), |
| 130 | + ) |
| 131 | + expect(environment.removeEventListener).toHaveBeenCalledWith( |
| 132 | + 'touchend', |
| 133 | + expect.any(Function), |
| 134 | + ) |
| 135 | + |
| 136 | + expect(result.current).toEqual({ |
| 137 | + current: {isMouseDown: false, isTouchMove: false}, |
| 138 | + }) |
| 139 | + }) |
| 140 | + }) |
66 | 141 | })
|
0 commit comments