forked from chartjs/chartjs-plugin-annotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
129 lines (106 loc) · 3.79 KB
/
events.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
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
export const getCenterPoint = (chart) => window.getAnnotationElements(chart)[0].getCenterPoint();
export const eventPoint0 = {
x: 0,
y: 0
};
export function testEvents(options) {
describe(`events on ${options.type}`, function() {
const chartConfig = {
type: 'scatter',
options: {
animation: false,
scales: {
x: {
display: false,
min: 0,
max: 10
},
y: {
display: false,
min: 0,
max: 10
}
},
plugins: {
legend: false,
annotation: {
}
}
},
};
const pluginOpts = chartConfig.options.plugins.annotation;
[pluginOpts, options].forEach(function(targetOptions) {
it('should detect enter and leave events', function(done) {
const enterSpy = jasmine.createSpy('enter');
const leaveSpy = jasmine.createSpy('leave');
targetOptions.enter = enterSpy;
targetOptions.leave = leaveSpy;
pluginOpts.annotations = [options];
const chart = window.acquireChart(chartConfig);
window.triggerMouseEvent(chart, 'mousemove', getCenterPoint(chart));
window.afterEvent(chart, 'mousemove', function() {
expect(enterSpy.calls.count()).toBe(1);
window.triggerMouseEvent(chart, 'mousemove', eventPoint0);
window.afterEvent(chart, 'mousemove', function() {
expect(leaveSpy.calls.count()).toBe(1);
delete targetOptions.enter;
delete targetOptions.leave;
done();
});
});
});
it('should detect enter and leave (by mouseout) events', function(done) {
const enterSpy = jasmine.createSpy('enter');
const leaveSpy = jasmine.createSpy('leave');
targetOptions.enter = enterSpy;
targetOptions.leave = leaveSpy;
pluginOpts.annotations = [options];
const chart = window.acquireChart(chartConfig);
window.triggerMouseEvent(chart, 'mousemove', getCenterPoint(chart));
window.afterEvent(chart, 'mousemove', function() {
expect(enterSpy.calls.count()).toBe(1);
window.triggerMouseEvent(chart, 'mouseout', eventPoint0);
window.afterEvent(chart, 'mouseout', function() {
expect(leaveSpy.calls.count()).toBe(1);
delete targetOptions.enter;
delete targetOptions.leave;
done();
});
});
});
it('shuold detect click event', function(done) {
const clickSpy = jasmine.createSpy('click');
targetOptions.click = clickSpy;
pluginOpts.annotations = [options];
const chart = window.acquireChart(chartConfig);
window.afterEvent(chart, 'click', function() {
expect(clickSpy.calls.count()).toBe(1);
delete targetOptions.click;
done();
});
window.triggerMouseEvent(chart, 'click', getCenterPoint(chart));
});
it('should detect dbl click event', function(done) {
const dblClickSpy = jasmine.createSpy('dblclick');
targetOptions.dblclick = dblClickSpy;
pluginOpts.dblClickSpeed = 1000;
pluginOpts.annotations = [options];
const chart = window.acquireChart(chartConfig);
const eventPoint = getCenterPoint(chart);
let dblClick = false;
window.afterEvent(chart, 'click', function() {
if (!dblClick) {
dblClick = true;
window.triggerMouseEvent(chart, 'click', eventPoint);
} else {
expect(dblClickSpy.calls.count()).toBe(1);
delete targetOptions.dblclick;
delete pluginOpts.dblClickSpeed;
done();
}
});
window.triggerMouseEvent(chart, 'click', eventPoint);
});
});
});
}