-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.spec.tsx
141 lines (124 loc) · 3.71 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { cloneElement } from "react";
import renderer from "react-test-renderer";
import useHover, { UseHoverOptions } from "./index";
type Hook = ReturnType<typeof useHover>;
const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
interface TestComponentProps {
hookOptions?: UseHoverOptions;
onRender: (hookReturn: ReturnType<typeof useHover>) => null;
}
function TestComponent(props: TestComponentProps) {
const hookReturn = useHover(props.hookOptions);
return props.onRender(hookReturn);
}
function createTestWrapper(options?: UseHoverOptions) {
let hook: Hook | undefined;
const render = {
observers: new Set<Function>(),
dispatch: () =>
render.observers.forEach(fn => {
fn();
render.observers.delete(fn);
}),
subscribe: (fn: Function) => render.observers.add(fn)
};
const element = (
<TestComponent
hookOptions={options}
onRender={hookReturn => {
hook = hookReturn;
render.dispatch();
return null;
}}
/>
);
const component = renderer.create(element);
const assert = async (assertion: (hook: Hook) => void) => {
return new Promise(resolve => {
render.subscribe(() => {
assertion(hook!);
resolve();
});
// Ensure new render actually happens
component.update(cloneElement(element, { _: Date.now() }));
});
};
const flush = () => new Promise(render.subscribe);
const mouseEnter = () => hook![1].onMouseEnter!({} as any);
const mouseLeave = () => hook![1].onMouseLeave!({} as any);
return {
initialIsHovering: hook![0],
mouseEnter,
mouseLeave,
component,
assert,
flush
};
}
test("isHovering is initially false", async () => {
const { initialIsHovering } = createTestWrapper();
expect(initialIsHovering).toBe(false);
});
test("isHovering turns true after mouse enter", async () => {
const { mouseEnter, assert } = createTestWrapper({
mouseEnterDelayMS: 0
});
mouseEnter();
await wait(0);
await assert(hook => expect(hook[0]).toBe(true));
});
test("isHovering turns false after mouse leave", async () => {
const { mouseEnter, mouseLeave, assert, flush } = createTestWrapper({
mouseEnterDelayMS: 0
});
mouseEnter();
await flush();
mouseLeave();
await flush();
await assert(hook => expect(hook[0]).toBe(false));
});
test("mouseEnterDelayMS delays isHovering turning true", async () => {
const { mouseEnter, assert } = createTestWrapper({
mouseEnterDelayMS: 100
});
mouseEnter();
await assert(hook => expect(hook[0]).toBe(false));
await wait(100);
await assert(hook => expect(hook[0]).toBe(true));
});
test("mousing out cancels delayed mouse enter", async () => {
const { mouseEnter, mouseLeave, assert } = createTestWrapper({
mouseEnterDelayMS: 100
});
mouseEnter();
mouseLeave();
await assert(hook => expect(hook[0]).toBe(false));
await wait(300);
await assert(hook => expect(hook[0]).toBe(false));
});
test("mouseLeaveDelayMS delays isHovering turning false", async () => {
const { mouseEnter, mouseLeave, assert, flush } = createTestWrapper({
mouseLeaveDelayMS: 100
});
jest.useRealTimers();
mouseEnter();
await flush();
mouseLeave();
await wait(0);
await assert(hook => expect(hook[0]).toBe(true));
await wait(100);
await assert(hook => expect(hook[0]).toBe(false));
});
test("mousing in cancels delayed mouse leave", async () => {
const { mouseEnter, mouseLeave, assert, flush } = createTestWrapper({
mouseEnterDelayMS: 0,
mouseLeaveDelayMS: 100
});
mouseEnter();
await flush();
mouseLeave();
await wait(0);
await assert(hook => expect(hook[0]).toBe(true));
await wait(100);
await assert(hook => expect(hook[0]).toBe(false));
});