-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathannotations_test.js
89 lines (65 loc) · 2.9 KB
/
annotations_test.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
var Annotations = require('@src/components/annotations');
var Plotly = require('@lib/index');
var Plots = require('@src/plots/plots');
var Lib = require('@src/lib');
var Dates = require('@src/lib/dates');
var d3 = require('d3');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
describe('Test annotations', function() {
'use strict';
describe('supplyLayoutDefaults', function() {
it('should default to pixel for axref/ayref', function() {
var annotationDefaults = {};
annotationDefaults._has = Plots._hasPlotType.bind(annotationDefaults);
Annotations.supplyLayoutDefaults({ annotations: [{ showarrow: true, arrowhead: 2}] }, annotationDefaults);
expect(annotationDefaults.annotations[0].axref).toEqual('pixel');
expect(annotationDefaults.annotations[0].ayref).toEqual('pixel');
});
it('should convert ax/ay date coordinates to milliseconds if tail is in axis terms and axis is a date', function() {
var annotationOut = { xaxis: { type: 'date', range: ['2000-01-01', '2016-01-01'] }};
annotationOut._has = Plots._hasPlotType.bind(annotationOut);
var annotationIn = {
annotations: [{ showarrow: true, axref: 'x', ayref: 'y', x: '2008-07-01', ax: '2004-07-01', y: 0, ay: 50}]
};
Annotations.supplyLayoutDefaults(annotationIn, annotationOut);
expect(annotationIn.annotations[0].ax).toEqual(Dates.dateTime2ms('2004-07-01'));
});
});
});
describe('annotations relayout', function() {
'use strict';
var mock = require('@mocks/annotations.json');
var len = mock.layout.annotations.length;
var gd;
beforeEach(function(done) {
gd = createGraphDiv();
var mockData = Lib.extendDeep([], mock.data),
mockLayout = Lib.extendDeep({}, mock.layout);
Plotly.plot(gd, mockData, mockLayout).then(done);
});
afterEach(destroyGraphDiv);
function countAnnotations() {
return d3.selectAll('g.annotation').size();
}
it('should be able to add /remove annotations', function(done) {
expect(countAnnotations()).toEqual(len);
var ann = { text: '' };
Plotly.relayout(gd, 'annotations[' + len + ']', ann).then(function() {
expect(countAnnotations()).toEqual(len + 1);
return Plotly.relayout(gd, 'annotations[' + 0 + ']', 'remove');
})
.then(function() {
expect(countAnnotations()).toEqual(len);
return Plotly.relayout(gd, 'annotations[' + 0 + ']', null);
})
.then(function() {
expect(countAnnotations()).toEqual(len - 1);
return Plotly.relayout(gd, { annotations: [] });
})
.then(function() {
expect(countAnnotations()).toEqual(0);
done();
});
});
});