-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
utils.spec.tsx
147 lines (112 loc) · 3.85 KB
/
utils.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
142
143
144
145
146
147
import {
createContainerWithHTML,
triggerEvent,
validateNodeTree,
} from 'inferno-utils';
describe('Utils', () => {
describe('createContainerWithHTML', () => {
it('should create a container with the passed in HTML', () => {
const container = createContainerWithHTML('<h1>hello!</h1>');
expect(container.innerHTML).toBe('<h1>hello!</h1>');
expect(container.tagName).toBe('DIV');
});
});
describe('validateNodeTree', () => {
it('should return true if called with falsy arguments', () => {
expect(validateNodeTree(false)).toBe(true);
expect(validateNodeTree(null)).toBe(true);
expect(validateNodeTree(undefined)).toBe(true);
});
it('should return true if called with a string', () => {
expect(validateNodeTree('<div><h1>test</h1></div>')).toBe(true);
});
it('should return true if called with a number', () => {
expect(validateNodeTree(4)).toBe(true);
});
});
describe('triggerEvent', () => {
let expectedEventType = '';
const element = {
dispatchEvent() {},
};
let spyDispatch;
let spyCreateMouseEvent;
let triggerName;
let spyEvent;
beforeEach(function () {
spyDispatch = spyOn(element, 'dispatchEvent');
spyCreateMouseEvent = spyOn(document, 'createEvent').and.callFake(
(ev) => {
expect(ev).toBe(expectedEventType);
spyEvent = {
initEvent: (eventType, _canBubble, cancelable) => {
expect(eventType).toBe(triggerName);
// expect(canBubble).toBe(true);
expect(cancelable).toBe(true);
},
};
return spyEvent;
},
);
});
afterEach(function () {
spyDispatch.calls.reset();
spyCreateMouseEvent.calls.reset();
});
it('should trigger event on click', () => {
expectedEventType = 'MouseEvents';
triggerName = 'click';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should trigger event on dblclick', () => {
expectedEventType = 'MouseEvents';
triggerName = 'dblclick';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should trigger event on mousedown', () => {
expectedEventType = 'MouseEvents';
triggerName = 'mousedown';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should trigger event on mouseup', () => {
expectedEventType = 'MouseEvents';
triggerName = 'mouseup';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should trigger event on focus', () => {
expectedEventType = 'HTMLEvents';
triggerName = 'focus';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should trigger event on change', () => {
expectedEventType = 'HTMLEvents';
triggerName = 'change';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should trigger event on blur', () => {
expectedEventType = 'HTMLEvents';
triggerName = 'blur';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should trigger event on select', () => {
expectedEventType = 'HTMLEvents';
triggerName = 'select';
triggerEvent(triggerName, element);
expect(spyDispatch.calls.argsFor(0)[0]).toBe(spyEvent);
});
it('should throw an error on unknown event', () => {
expectedEventType = 'HTMLEvents';
triggerName = 'blah';
expect(
triggerEvent.bind(triggerEvent, triggerName, element),
).toThrowError(Error);
});
});
});