|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title></title> |
| 5 | + <link href="/assets/index.css" rel="stylesheet" type="text/css" /> |
| 6 | + <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> |
| 7 | + <script src="/react-scroll-to-bottom.development.js"></script> |
| 8 | + <script src="/test-harness.js"></script> |
| 9 | + <script src="/assets/page-object-model.js"></script> |
| 10 | + </head> |
| 11 | + <body> |
| 12 | + <div id="app"></div> |
| 13 | + </body> |
| 14 | + <script type="text/babel" data-presets="react"> |
| 15 | + 'use strict'; |
| 16 | + |
| 17 | + const RunFunction = ({ fn }) => { |
| 18 | + fn && fn(); |
| 19 | + |
| 20 | + return false; |
| 21 | + }; |
| 22 | + |
| 23 | + async function render(paragraphs, fn) { |
| 24 | + let called; |
| 25 | + const once = |
| 26 | + fn && |
| 27 | + (() => { |
| 28 | + !called && fn(); |
| 29 | + called = 1; |
| 30 | + }); |
| 31 | + |
| 32 | + await new Promise(resolve => |
| 33 | + ReactDOM.render( |
| 34 | + <div className="react-scroll-to-bottom"> |
| 35 | + {/* Set checkInterval to 1 to increase the probability of race condition. */} |
| 36 | + <ReactScrollToBottom.Composer checkInterval={1}> |
| 37 | + <ReactScrollToBottom.Panel className="scrollable"> |
| 38 | + {paragraphs.map((paragraph, index) => ( |
| 39 | + <div key={index}> |
| 40 | + {index + 1}: {paragraph} |
| 41 | + </div> |
| 42 | + ))} |
| 43 | + </ReactScrollToBottom.Panel> |
| 44 | + <RunFunction fn={once} /> |
| 45 | + </ReactScrollToBottom.Composer> |
| 46 | + </div>, |
| 47 | + document.getElementById('app'), |
| 48 | + resolve |
| 49 | + ) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + run(async function () { |
| 54 | + let paragraphs = pageObjects.paragraphs; |
| 55 | + |
| 56 | + for (let i = 0; i < 20; i++) { |
| 57 | + // Preparation: scrollable should stay at the bottom before start. |
| 58 | + await render(paragraphs, () => ReactScrollToBottom.useScrollTo()('100%', { behavior: 'auto' })); |
| 59 | + await pageObjects.scrollStabilizedAtBottom(); |
| 60 | + |
| 61 | + const scrollable = document.getElementsByClassName('scrollable')[0]; |
| 62 | + |
| 63 | + // Call scrollTo() and wait until the first "scroll" event. |
| 64 | + const scrolling = new Promise(resolve => scrollable.addEventListener('scroll', resolve, { once: true })); |
| 65 | + |
| 66 | + // Alternate between discrete and smooth scrolling. |
| 67 | + scrollable.scrollTo({ behavior: i % 2 ? 'auto' : 'smooth', top: 0 }); |
| 68 | + |
| 69 | + // "scroll" event dispatched after we call scrollTo(), it should be safe to append a new element. |
| 70 | + // Withou the "scroll" event, the component can't guarantee if it is safe to append, due to technical difficulties. |
| 71 | + await scrolling; |
| 72 | + |
| 73 | + // Add a new paragraph. |
| 74 | + paragraphs = [...paragraphs, pageObjects.paragraphs[~~(Math.random() * pageObjects.paragraphs.length)]]; |
| 75 | + |
| 76 | + await render(paragraphs); |
| 77 | + |
| 78 | + // After a new paragraph is added, it should continue to scroll to the top. |
| 79 | + await pageObjects.scrollStabilizedAtTop(); |
| 80 | + } |
| 81 | + }); |
| 82 | + </script> |
| 83 | +</html> |
0 commit comments