|
| 1 | +import expect from "expect"; |
| 2 | +import React from "react"; |
| 3 | +import {render, unmountComponentAtNode} from "react-dom"; |
| 4 | +import Scrollbar from "react-scrollbars-custom"; |
| 5 | +import simulant from "simulant"; |
| 6 | + |
| 7 | +export default function performTests() { |
| 8 | + describe("thumb interaction", () => { |
| 9 | + let node; |
| 10 | + beforeEach(() => { |
| 11 | + node = document.createElement("div"); |
| 12 | + document.body.appendChild(node); |
| 13 | + }); |
| 14 | + afterEach(() => { |
| 15 | + unmountComponentAtNode(node); |
| 16 | + document.body.removeChild(node); |
| 17 | + }); |
| 18 | + |
| 19 | + it("thumb drag should scroll to respective position", done => { |
| 20 | + render( |
| 21 | + <Scrollbar |
| 22 | + trackClickBehavior="step" |
| 23 | + style={{width: 100, height: 100, position: "relative", padding: 0, margin: 0}} |
| 24 | + wrapperProps={{style: {padding: 0, margin: 0}}}> |
| 25 | + <div style={{width: 1000, height: 1000, padding: 0, margin: 0}} /> |
| 26 | + </Scrollbar>, |
| 27 | + node, |
| 28 | + function() { |
| 29 | + setTimeout(() => { |
| 30 | + const { |
| 31 | + top: trackXTop, |
| 32 | + height: trackXHeight, |
| 33 | + left: trackXLeft, |
| 34 | + width: trackXWidth, |
| 35 | + } = this.trackXEl.getBoundingClientRect(); |
| 36 | + const { |
| 37 | + top: trackYTop, |
| 38 | + height: trackYHeight, |
| 39 | + left: trackYLeft, |
| 40 | + width: trackYWidth, |
| 41 | + } = this.trackYEl.getBoundingClientRect(); |
| 42 | + const { |
| 43 | + top: thumbXTop, |
| 44 | + height: thumbXHeight, |
| 45 | + left: thumbXLeft, |
| 46 | + width: thumbXWidth, |
| 47 | + } = this.thumbXEl.getBoundingClientRect(); |
| 48 | + const { |
| 49 | + top: thumbYTop, |
| 50 | + height: thumbYHeight, |
| 51 | + left: thumbYLeft, |
| 52 | + width: thumbYWidth, |
| 53 | + } = this.thumbYEl.getBoundingClientRect(); |
| 54 | + |
| 55 | + simulant.fire(this.thumbYEl, "mousedown", { |
| 56 | + which: 1, |
| 57 | + clientY: thumbYTop + thumbYHeight / 2, |
| 58 | + clientX: thumbYLeft + thumbYWidth / 2, |
| 59 | + }); |
| 60 | + simulant.fire(document, "mousemove", { |
| 61 | + which: 1, |
| 62 | + clientY: trackYTop + trackYHeight / 2, |
| 63 | + clientX: trackYLeft + trackYWidth / 2, |
| 64 | + }); |
| 65 | + simulant.fire(document, "mouseup", { |
| 66 | + which: 1, |
| 67 | + clientY: trackYTop + trackYHeight / 2, |
| 68 | + clientX: trackYLeft + trackYWidth / 2, |
| 69 | + }); |
| 70 | + |
| 71 | + setTimeout(() => { |
| 72 | + expect(this.contentEl.scrollTop).toBe( |
| 73 | + this.contentEl.scrollHeight / 2 - this.contentEl.clientHeight / 2 |
| 74 | + ); |
| 75 | + |
| 76 | + simulant.fire(this.thumbXEl, "mousedown", { |
| 77 | + which: 1, |
| 78 | + clientY: thumbXTop + thumbXHeight / 2, |
| 79 | + clientX: thumbXLeft + thumbXWidth / 2, |
| 80 | + }); |
| 81 | + simulant.fire(document, "mousemove", { |
| 82 | + which: 1, |
| 83 | + clientY: trackXTop + trackXHeight / 2, |
| 84 | + clientX: trackXLeft + trackXWidth / 2, |
| 85 | + }); |
| 86 | + simulant.fire(document, "mouseup", { |
| 87 | + which: 1, |
| 88 | + clientY: trackXTop + trackXHeight / 2, |
| 89 | + clientX: trackXLeft + trackXWidth / 2, |
| 90 | + }); |
| 91 | + |
| 92 | + setTimeout(() => { |
| 93 | + expect(this.contentEl.scrollLeft).toBe( |
| 94 | + this.contentEl.scrollWidth / 2 - this.contentEl.clientWidth / 2 |
| 95 | + ); |
| 96 | + |
| 97 | + done(); |
| 98 | + }, 20); |
| 99 | + }, 20); |
| 100 | + }, 20); |
| 101 | + } |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | +} |
0 commit comments