@@ -11,22 +11,117 @@ test('test scrollparent behaviour', async ({ page }) => {
1111 await page . evaluate ( ( ) => {
1212 var top = document . scrollingElement || document . documentElement ;
1313
14- function equal ( a , b , msg ) {
14+ let counter = 0 ;
15+ function equal ( a , b ) {
16+ counter += 1 ;
1517 if ( a !== b ) {
16- throw new Error ( msg || "Page Error" ) ;
18+ throw new Error ( "Failed #" + counter ) ;
1719 }
1820 }
1921
2022 function byId ( id ) {
2123 return document . getElementById ( id ) ;
2224 }
2325
24- equal ( Scrollparent ( byId ( "test1" ) ) . className , "scroll" ) ;
26+ equal ( Scrollparent ( byId ( "test1" ) ) , byId ( "test1-target" ) ) ;
2527
2628 equal ( Scrollparent ( byId ( "test2" ) ) , top ) ;
2729 equal ( Scrollparent ( byId ( "test3" ) ) , top ) ;
2830
29- equal ( Scrollparent ( byId ( "test4" ) ) . className , "scroll-y" ) ;
30- equal ( Scrollparent ( byId ( "test5" ) ) . className , "scroll-x" ) ;
31+ equal ( Scrollparent ( byId ( "test4" ) ) , byId ( "test4-target" ) ) ;
32+ equal ( Scrollparent ( byId ( "test5" ) ) , byId ( "test5-target" ) ) ;
33+ equal ( Scrollparent ( byId ( "test6" ) ) , byId ( "test6-target" ) ) ;
34+ equal ( Scrollparent ( byId ( "test7" ) ) , byId ( "test7-target" ) ) ;
35+
36+ equal ( Scrollparent ( byId ( "test8" ) ) , top ) ;
37+ equal ( Scrollparent ( byId ( "test9" ) ) , top ) ;
38+
39+ equal ( Scrollparent ( byId ( "test10" ) ) , byId ( "test10-target" ) ) ;
40+
41+ equal ( typeof Scrollparent ( "test1" ) , typeof undefined ) ;
3142 } ) ;
3243} ) ;
44+
45+ test ( 'benchmark scrollparent' , async ( { page } ) => {
46+ const benchFile = resolve ( __dirname , 'bench.html' ) ;
47+
48+ await page . goto ( `file://${ benchFile } ` ) ;
49+
50+ await expect ( page ) . toHaveTitle ( 'scrollparent.js benchmark page' ) ;
51+
52+ page . on ( "console" , ( msg ) => {
53+ console . log ( msg ) ;
54+ } ) ;
55+
56+ await page . evaluate ( ( ) => {
57+ function byId ( id ) {
58+ return document . getElementById ( id ) ;
59+ }
60+
61+ function bench ( name , n , fn ) {
62+ const start = performance . now ( ) ;
63+ for ( let i = 0 ; i < n ; i ++ ) {
64+ fn ( i )
65+ }
66+ console . log ( `${ name } took ${ performance . now ( ) - start } ms` ) ;
67+ }
68+
69+ const node1 = byId ( "bench1" ) ;
70+ bench ( "bench1" , 1000 , ( ) => {
71+ Scrollparent ( node1 ) ;
72+ } ) ;
73+
74+ const node2 = byId ( "bench2" ) ;
75+ bench ( "bench2" , 1000 , ( ) => {
76+ Scrollparent ( node2 ) ;
77+ } ) ;
78+
79+ const nodes1 = [ ] ;
80+ for ( let i = 0 ; i < 1000 ; i ++ ) {
81+ const root = document . createElement ( "div" ) ;
82+ document . body . appendChild ( root ) ;
83+ root . style . overflow = i % 2 === 0 ? "scroll" : "hidden" ;
84+
85+ let current = root ;
86+ for ( let j = 0 ; j < i ; j ++ ) {
87+ const child = document . createElement ( "div" ) ;
88+ current . appendChild ( child ) ;
89+ current = child
90+ }
91+
92+ nodes1 . push ( current ) ;
93+ }
94+
95+ // force style recompute
96+ getComputedStyle ( nodes1 [ nodes1 . length - 1 ] , null ) . getPropertyValue ( "overflow" ) ;
97+
98+ bench ( "benchN1" , nodes1 . length , ( i ) => {
99+ Scrollparent ( nodes1 [ i ] ) ;
100+ } ) ;
101+
102+ const nodes2 = [ ] ;
103+ for ( let i = 0 ; i < 1000 ; i ++ ) {
104+ const root = document . createElement ( "div" ) ;
105+ document . body . appendChild ( root ) ;
106+
107+ let current = root ;
108+ for ( let j = 0 ; j < i ; j ++ ) {
109+ const child = document . createElement ( "div" ) ;
110+ if ( j === Math . floor ( i / 2 ) ) {
111+ child . style . overflow = "scroll" ;
112+ }
113+
114+ current . appendChild ( child ) ;
115+ current = child
116+ }
117+
118+ nodes2 . push ( current ) ;
119+ }
120+
121+ getComputedStyle ( nodes2 [ nodes2 . length - 1 ] , null ) . getPropertyValue ( "overflow" ) ;
122+
123+ bench ( "benchN2" , nodes2 . length , ( i ) => {
124+ Scrollparent ( nodes2 [ i ] ) ;
125+ } ) ;
126+ } ) ;
127+ } ) ;
0 commit comments