@@ -19,30 +19,33 @@ const renderer = new CanvasRenderer({
19
19
enableDirtyRectangleRendering : true ,
20
20
} ) ;
21
21
22
+ const div = createDiv ( ) ;
23
+
24
+ // @ts -ignore
25
+ const canvas = new Canvas ( {
26
+ container : div ,
27
+ width : 300 ,
28
+ height : 300 ,
29
+ renderer,
30
+ } ) ;
31
+
32
+ const height = 100 ;
33
+ const thumbLen = 30 ;
34
+ const scrollbar = new Scrollbar ( {
35
+ attrs : {
36
+ height,
37
+ thumbLen,
38
+ x : 100 ,
39
+ y : 5 ,
40
+ value : 0.5 ,
41
+ width : 20 ,
42
+ } ,
43
+ } ) ;
44
+
45
+ canvas . appendChild ( scrollbar ) ;
46
+
22
47
describe ( 'scrollbar' , ( ) => {
23
48
test ( 'basic' , async ( ) => {
24
- const div = createDiv ( ) ;
25
-
26
- // @ts -ignore
27
- const canvas = new Canvas ( {
28
- container : div ,
29
- width : 300 ,
30
- height : 300 ,
31
- renderer,
32
- } ) ;
33
-
34
- const height = 100 ;
35
- const thumbLen = 30 ;
36
- const scrollbar = new Scrollbar ( {
37
- attrs : {
38
- height,
39
- thumbLen,
40
- x : 100 ,
41
- y : 5 ,
42
- value : 0.5 ,
43
- width : 20 ,
44
- } ,
45
- } ) ;
46
49
expect ( scrollbar . getValue ( ) ) . toBe ( 0.5 ) ;
47
50
48
51
const { padding } = scrollbar . attributes ;
@@ -52,128 +55,111 @@ describe('scrollbar', () => {
52
55
let value = 0.2 ;
53
56
scrollbar . setValue ( value ) ;
54
57
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
55
- expect ( scrollbar . lastChild . attributes . y ) . toBeCloseTo (
58
+
59
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'y' ) ) . toBeCloseTo (
56
60
top + ( height - verticalPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
57
61
1
58
62
) ;
59
63
60
64
value = 0.1 ;
61
65
scrollbar . setValue ( value ) ;
62
66
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
63
- expect ( scrollbar . lastChild . attributes . y ) . toBeCloseTo (
67
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'y' ) ) . toBeCloseTo (
64
68
top + ( height - verticalPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
65
69
1
66
70
) ;
67
71
68
72
value = 0.9 ;
69
73
scrollbar . setValue ( value ) ;
70
74
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
71
- expect ( scrollbar . lastChild . attributes . y ) . toBeCloseTo (
75
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'y' ) ) . toBeCloseTo (
72
76
top + ( height - verticalPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
73
77
1
74
78
) ;
75
79
76
80
value = 10 ;
77
81
scrollbar . setValue ( value ) ;
78
82
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
79
- expect ( scrollbar . lastChild . attributes . y ) . toBeCloseTo (
83
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'y' ) ) . toBeCloseTo (
80
84
top + ( height - verticalPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
81
85
1
82
86
) ;
83
87
84
88
value = - 10086 ;
85
89
scrollbar . setValue ( value ) ;
86
90
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
87
- expect ( scrollbar . lastChild . attributes . y ) . toBeCloseTo (
91
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'y' ) ) . toBeCloseTo (
88
92
top + ( height - verticalPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
89
93
1
90
94
) ;
91
-
92
- canvas . appendChild ( scrollbar ) ;
93
-
94
- // scrollbar.destroy();
95
- // canvas.destroy();
96
95
} ) ;
97
96
98
97
test ( 'horizontal' , async ( ) => {
99
- const div = createDiv ( ) ;
100
-
101
- // @ts -ignore
102
- const canvas = new Canvas ( {
103
- container : div ,
104
- width : 300 ,
105
- height : 300 ,
106
- renderer,
107
- } ) ;
108
-
109
98
const width = 100 ;
110
99
const thumbLen = 30 ;
111
-
112
- const scrollbar = new Scrollbar ( {
113
- attrs : {
114
- width,
115
- thumbLen,
116
- x : 10 ,
117
- y : 50 ,
118
- orient : 'horizontal' ,
119
- height : 20 ,
120
- value : 0.5 ,
121
- } ,
100
+ scrollbar . update ( {
101
+ width,
102
+ thumbLen,
103
+ x : 10 ,
104
+ y : 50 ,
105
+ orient : 'horizontal' ,
106
+ height : 20 ,
107
+ value : 0.5 ,
122
108
} ) ;
123
109
124
110
expect ( scrollbar . getValue ( ) ) . toBe ( 0.5 ) ;
125
111
126
112
const { padding } = scrollbar . attributes ;
127
113
const [ , right , , left ] = padding ;
128
-
129
114
const horizonPadding = left + right ;
130
115
131
116
let value = 0.2 ;
132
117
scrollbar . setValue ( value ) ;
133
118
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
134
- expect ( scrollbar . lastChild . attributes . x ) . toBeCloseTo (
119
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'x' ) ) . toBeCloseTo (
135
120
left + ( width - horizonPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
136
121
1
137
122
) ;
138
123
139
124
value = 0.1 ;
140
125
scrollbar . setValue ( value ) ;
141
126
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
142
- expect ( scrollbar . lastChild . attributes . x ) . toBeCloseTo (
127
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'x' ) ) . toBeCloseTo (
143
128
left + ( width - horizonPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
144
129
1
145
130
) ;
146
131
147
132
value = 0.9 ;
148
133
scrollbar . setValue ( value ) ;
149
134
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
150
- expect ( scrollbar . lastChild . attributes . x ) . toBeCloseTo (
135
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'x' ) ) . toBeCloseTo (
151
136
left + ( width - horizonPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
152
137
1
153
138
) ;
154
139
155
140
value = 10 ;
156
141
scrollbar . setValue ( value ) ;
157
142
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
158
- expect ( scrollbar . lastChild . attributes . x ) . toBeCloseTo (
143
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'x' ) ) . toBeCloseTo (
159
144
left + ( width - horizonPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
160
145
1
161
146
) ;
162
147
163
148
value = - 10086 ;
164
149
scrollbar . setValue ( value ) ;
165
150
expect ( scrollbar . getValue ( ) ) . toBe ( value ) ;
166
- expect ( scrollbar . lastChild . attributes . x ) . toBeCloseTo (
151
+ expect ( scrollbar . getElementsByName ( 'thumb' ) [ 0 ] . attr ( 'x' ) ) . toBeCloseTo (
167
152
left + ( width - horizonPadding - thumbLen ) * clamp ( value , 0 , 1 ) ,
168
153
1
169
154
) ;
170
155
171
156
scrollbar . addEventListener ( 'valuechange' , ( e ) => {
172
- console . log ( 'scroll: ' , e ) ;
157
+ e ;
173
158
} ) ;
174
159
175
160
canvas . appendChild ( scrollbar ) ;
176
- // scrollbar.destroy();
177
- // canvas.destroy();
178
161
} ) ;
179
162
} ) ;
163
+
164
+ // scrollbar.destroy();
165
+ // canvas.destroy();
0 commit comments