1
1
import { Stock } from '../../../../src' ;
2
- import { createDiv } from '../../../utils/dom' ;
2
+ import { createDiv , removeDom } from '../../../utils/dom' ;
3
3
import { kdata } from '../../../data/stock' ;
4
4
5
- // import { DEFAULT_TOOLTIP_OPTIONS } from '../../../../src/plots/stock/constant';
6
- // import { pick } from '../../../../src/utils';
7
-
8
5
describe ( 'Stock tooltip' , ( ) => {
9
- it ( 'tooltip: default options' , ( ) => {
10
- const k = new Stock ( createDiv ( 'default tooltip' ) , {
11
- width : 400 ,
12
- height : 500 ,
13
- data : kdata ,
14
- xField : 'date' ,
15
- yField : [ 'start' , 'end' , 'max' , 'min' ] ,
16
- } ) ;
6
+ const div = createDiv ( ' default tooltip' ) ;
7
+ const k = new Stock ( div , {
8
+ width : 400 ,
9
+ height : 500 ,
10
+ data : kdata ,
11
+ xField : 'date' ,
12
+ yField : [ 'start' , 'end' , 'max' , 'min' ] ,
13
+ } ) ;
17
14
18
- k . render ( ) ;
15
+ k . render ( ) ;
16
+ it ( 'tooltip: default options' , ( ) => {
19
17
// @ts -ignore
20
18
expect ( k . chart . options . tooltip . shared ) . toBe ( true ) ;
21
19
// @ts -ignore
22
20
expect ( k . chart . options . tooltip . showCrosshairs ) . toBe ( true ) ;
23
21
// @ts -ignore
24
- expect ( k . chart . options . tooltip . showTitle ) . toBe ( false ) ;
25
- // @ts -ignore
26
22
expect ( k . chart . options . tooltip . showMarkers ) . toBe ( false ) ;
23
+ } ) ;
27
24
28
- // @ts -ignore
29
- // expect(pick(k.chart.options.tooltip, Object.keys(DEFAULT_TOOLTIP_OPTIONS))).toEqual(DEFAULT_TOOLTIP_OPTIONS);
25
+ it ( 'tooltip: default show fields of yField' , ( ) => {
26
+ const geometry = k . chart . geometries [ 0 ] ;
27
+ const elements = geometry . elements ;
28
+ const bbox = elements [ 0 ] . getBBox ( ) ;
30
29
31
- k . destroy ( ) ;
30
+ // 正常渲染某个元素tooltip
31
+ k . chart . showTooltip ( { x : bbox . minX + bbox . width / 2 , y : bbox . y + bbox . height / 2 } ) ;
32
+
33
+ expect ( div . querySelectorAll ( '.g2-tooltip-list-item' ) . length ) . toBe ( k . options . yField . length ) ;
34
+
35
+ // 设置hide
36
+ k . chart . hideTooltip ( ) ;
32
37
} ) ;
33
38
34
- it ( 'tooltip: options' , ( ) => {
35
- const k = new Stock ( createDiv ( 'tooltip: options' ) , {
36
- width : 400 ,
37
- height : 500 ,
38
- data : kdata ,
39
- xField : 'date' ,
40
- yField : [ 'start' , 'end' , 'max' , 'min' ] ,
39
+ it ( 'tooltip: showTitle' , ( ) => {
40
+ k . update ( {
41
41
tooltip : {
42
42
showTitle : true ,
43
43
title : 'hello world' ,
44
44
} ,
45
45
} ) ;
46
46
47
- k . render ( ) ;
48
-
49
47
// @ts -ignore
50
48
expect ( k . chart . options . tooltip . showTitle ) . toBe ( true ) ;
51
49
// @ts -ignore
@@ -58,17 +56,10 @@ describe('Stock tooltip', () => {
58
56
// @ts -ignore
59
57
expect ( k . chart . options . tooltip ) . toBe ( false ) ;
60
58
expect ( k . chart . getComponents ( ) . find ( ( co ) => co . type === 'tooltip' ) ) . toBe ( undefined ) ;
61
-
62
- k . destroy ( ) ;
63
59
} ) ;
64
60
65
61
it ( 'tooltip: custom itemTpl' , ( ) => {
66
- const k = new Stock ( createDiv ( 'custom itemTpl' ) , {
67
- width : 400 ,
68
- height : 500 ,
69
- data : kdata ,
70
- xField : 'date' ,
71
- yField : [ 'start' , 'end' , 'max' , 'min' ] ,
62
+ k . update ( {
72
63
tooltip : {
73
64
itemTpl :
74
65
'<li class="g2-tooltip-list-item custom-item-tpl" data-index={index} style="margin-bottom:4px;">' +
@@ -79,68 +70,86 @@ describe('Stock tooltip', () => {
79
70
} ,
80
71
} ) ;
81
72
82
- k . render ( ) ;
83
-
84
73
const geometry = k . chart . geometries [ 0 ] ;
85
74
const elements = geometry . elements ;
86
75
const bbox = elements [ elements . length - 1 ] . getBBox ( ) ;
87
76
88
77
// 渲染自定义itemTpl
89
- k . chart . showTooltip ( { x : bbox . maxX , y : bbox . maxY } ) ;
78
+ k . chart . showTooltip ( { x : bbox . minX + bbox . width / 2 , y : bbox . y + bbox . height / 2 } ) ;
90
79
expect ( document . getElementsByClassName ( 'custom-item-tpl' ) [ 0 ] . innerHTML ) . not . toBeNull ( ) ;
91
80
92
- k . destroy ( ) ;
81
+ // 设置hide
82
+ k . chart . hideTooltip ( ) ;
93
83
} ) ;
94
84
95
- it ( 'tooltip: change the configuration && operation' , ( ) => {
96
- const k = new Stock ( createDiv ( 'change the configuration && operation' ) , {
97
- width : 400 ,
98
- height : 500 ,
99
- data : kdata ,
100
- xField : 'date' ,
101
- yField : [ 'start' , 'end' , 'max' , 'min' ] ,
85
+ it ( 'tooltip: fields' , ( ) => {
86
+ k . update ( {
87
+ tooltip : {
88
+ fields : [ 'start' , 'end' , 'max' , 'min' , 'volumn' ] ,
89
+ } ,
102
90
} ) ;
103
91
104
- k . render ( ) ;
92
+ const geometry = k . chart . geometries [ 0 ] ;
93
+ const elements = geometry . elements ;
94
+ const bbox = elements [ 0 ] . getBBox ( ) ;
95
+
96
+ // 正常渲染某个元素tooltip
97
+ k . chart . showTooltip ( { x : bbox . minX + bbox . width / 2 , y : bbox . y + bbox . height / 2 } ) ;
98
+
99
+ expect ( div . querySelectorAll ( '.g2-tooltip-list-item' ) . length ) . toBe ( 5 ) ;
100
+
101
+ // 设置hide
102
+ k . chart . hideTooltip ( ) ;
103
+ } ) ;
104
+
105
+ // fixme https://github.com/antvis/G2/issues/3435
106
+ it . skip ( 'tooltip: fields & formatter' , ( ) => {
107
+ k . update ( {
108
+ tooltip : {
109
+ fields : [ 'start' , 'end' , 'max' , 'min' , 'volumn' ] ,
110
+ formatter : ( ) => ( { name : 'x' , value : 'a' } ) ,
111
+ } ,
112
+ } ) ;
105
113
106
114
const geometry = k . chart . geometries [ 0 ] ;
107
115
const elements = geometry . elements ;
108
116
const bbox = elements [ elements . length - 1 ] . getBBox ( ) ;
109
117
110
118
// 正常渲染某个元素tooltip
111
- k . chart . showTooltip ( { x : bbox . maxX , y : bbox . maxY } ) ;
112
- expect ( document . getElementsByClassName ( 'g2-tooltip-list-item' ) [ 0 ] . innerHTML ) . not . toBeNull ( ) ;
119
+ k . chart . showTooltip ( { x : bbox . minX + bbox . width / 2 , y : bbox . y + bbox . height / 2 } ) ;
120
+ expect ( div . getElementsByClassName ( 'g2-tooltip-list-item' ) . length ) . toBe ( 5 ) ;
113
121
114
122
// 设置hide
123
+ k . chart . hideTooltip ( ) ;
124
+ } ) ;
125
+
126
+ it ( 'tooltip: fields & customContent' , ( ) => {
115
127
k . update ( {
116
- ...k . options ,
117
- tooltip : false ,
128
+ tooltip : {
129
+ fields : [ 'start' , 'end' , 'max' ] ,
130
+ customContent : ( text , items ) =>
131
+ `<div>${ items . map ( ( item , idx ) => `<div class="custom-tooltip-item-content">${ idx } </div>` ) } <div>` ,
132
+ } ,
118
133
} ) ;
119
- // @ts -ignore
120
- expect ( k . chart . options . tooltip ) . toBe ( false ) ;
121
- expect ( k . chart . getComponents ( ) . find ( ( co ) => co . type === 'tooltip' ) ) . toBe ( undefined ) ;
122
134
123
- k . destroy ( ) ;
135
+ const elements = k . chart . geometries [ 0 ] . elements ;
136
+ const bbox = elements [ elements . length - 1 ] . getBBox ( ) ;
137
+
138
+ // 正常渲染某个元素tooltip
139
+ k . chart . showTooltip ( { x : bbox . minX + bbox . width / 2 , y : bbox . y + bbox . height / 2 } ) ;
140
+ expect ( div . getElementsByClassName ( 'custom-tooltip-item-content' ) . length ) . toBe ( 3 ) ;
141
+
142
+ // 设置hide
143
+ k . chart . hideTooltip ( ) ;
124
144
} ) ;
125
145
126
- it ( 'tooltip: custom crosshairs' , ( ) => {
127
- const k = new Stock ( createDiv ( 'custom crosshairs' ) , {
128
- width : 400 ,
129
- height : 500 ,
130
- data : kdata ,
131
- xField : 'date' ,
132
- yField : [ 'start' , 'end' , 'max' , 'min' ] ,
146
+ it ( 'tooltip: custom crosshairs' , ( ) => {
147
+ k . update ( {
133
148
tooltip : {
134
149
crosshairs : {
135
150
type : 'xy' ,
136
151
follow : true ,
137
- text : (
138
- type , // 对应当前 crosshairs 的类型,值为 'x' 或者 'y'
139
- defaultContent , // 对应当前 crosshairs 默认的文本内容
140
- items , // 对应当前 tooltip 内容框中的数据
141
- currentPoint // 对应当前坐标点
142
- ) => {
143
- console . log ( type , defaultContent , items , currentPoint ) ;
152
+ text : ( ) => {
144
153
return {
145
154
content : 'custom text' ,
146
155
} ;
@@ -149,12 +158,14 @@ describe('Stock tooltip', () => {
149
158
} ,
150
159
} ) ;
151
160
152
- k . render ( ) ;
153
-
154
161
// @ts -ignore
155
162
expect ( k . chart . options . tooltip . crosshairs . text ( ) ) . not . toBeUndefined ( ) ;
156
163
// @ts -ignore
157
164
expect ( k . chart . options . tooltip . crosshairs . text ( ) . content ) . toBe ( 'custom text' ) ;
165
+ } ) ;
166
+
167
+ afterAll ( ( ) => {
158
168
k . destroy ( ) ;
169
+ removeDom ( div ) ;
159
170
} ) ;
160
171
} ) ;
0 commit comments