|
| 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 createTests(scrollbarWidth) { |
| 8 | + describe("dragging", () => { |
| 9 | + let node; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + node = document.createElement("div"); |
| 13 | + document.body.appendChild(node); |
| 14 | + }); |
| 15 | + afterEach(() => { |
| 16 | + unmountComponentAtNode(node); |
| 17 | + document.body.removeChild(node); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("when dragging vertical thumb", function () { |
| 21 | + it("should scroll to respective position", (done) => { |
| 22 | + render(<Scrollbar style={ {width: 100, height: 100} } gridless> |
| 23 | + <div style={ {width: 200, height: 200} }></div> |
| 24 | + </Scrollbar>, |
| 25 | + node, |
| 26 | + function () { |
| 27 | + setTimeout(() => { |
| 28 | + const {content, thumbVertical: thumb} = this; |
| 29 | + const {top: thumbOffset} = thumb.getBoundingClientRect(); |
| 30 | + |
| 31 | + simulant.fire(thumb, 'mousedown', {target: thumb, clientY: thumbOffset, which: 1}); |
| 32 | + simulant.fire(document, 'mousemove', {clientY: thumbOffset + 100}); |
| 33 | + simulant.fire(document, 'mouseup'); |
| 34 | + |
| 35 | + setTimeout(() => { |
| 36 | + expect(content.scrollTop).toBe(100); |
| 37 | + done(); |
| 38 | + }, 50); |
| 39 | + }, 100); |
| 40 | + }); |
| 41 | + }); |
| 42 | + it("should add dragging class to thumb", (done) => { |
| 43 | + render(<Scrollbar style={ {width: 100, height: 100} } gridless> |
| 44 | + <div style={ {width: 200, height: 200} }></div> |
| 45 | + </Scrollbar>, |
| 46 | + node, |
| 47 | + function () { |
| 48 | + setTimeout(() => { |
| 49 | + const {thumbVertical: thumb} = this; |
| 50 | + const {top: thumbOffset} = thumb.getBoundingClientRect(); |
| 51 | + |
| 52 | + simulant.fire(thumb, 'mousedown', {target: thumb, clientY: thumbOffset, which: 1}); |
| 53 | + simulant.fire(document, 'mousemove', {clientY: thumbOffset + 100}); |
| 54 | + |
| 55 | + expect(thumb.classList.contains('dragging')).toBeTruthy(); |
| 56 | + |
| 57 | + simulant.fire(document, 'mouseup'); |
| 58 | + |
| 59 | + expect(thumb.classList.contains('dragging')).toBeFalsy(); |
| 60 | + done(); |
| 61 | + }, 100); |
| 62 | + }); |
| 63 | + }); |
| 64 | + it("should disable selection on the body element", (done) => { |
| 65 | + render(<Scrollbar style={ {width: 100, height: 100} } gridless> |
| 66 | + <div style={ {width: 200, height: 200} }></div> |
| 67 | + </Scrollbar>, |
| 68 | + node, |
| 69 | + function () { |
| 70 | + setTimeout(() => { |
| 71 | + const {thumbVertical: thumb} = this; |
| 72 | + const {top: thumbOffset} = thumb.getBoundingClientRect(); |
| 73 | + |
| 74 | + simulant.fire(thumb, 'mousedown', {target: thumb, clientY: thumbOffset, which: 1}); |
| 75 | + simulant.fire(document, 'mousemove', {clientY: thumbOffset + 100}); |
| 76 | + |
| 77 | + expect(document.body.style.userSelect).toBe("none"); |
| 78 | + |
| 79 | + simulant.fire(document, 'mouseup'); |
| 80 | + |
| 81 | + expect(document.body.style.userSelect).toBe(""); |
| 82 | + done(); |
| 83 | + }, 100); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("when dragging horizontal thumb", function () { |
| 89 | + it("should scroll to respective position", (done) => { |
| 90 | + render(<Scrollbar style={ {width: 100, height: 100} } gridless> |
| 91 | + <div style={ {width: 200, height: 200} }></div> |
| 92 | + </Scrollbar>, |
| 93 | + node, |
| 94 | + function () { |
| 95 | + setTimeout(() => { |
| 96 | + const {content, thumbHorizontal: thumb} = this; |
| 97 | + const {left: thumbOffset} = thumb.getBoundingClientRect(); |
| 98 | + |
| 99 | + simulant.fire(thumb, 'mousedown', {target: thumb, clientX: thumbOffset, which: 1}); |
| 100 | + simulant.fire(document, 'mousemove', {clientX: thumbOffset + 100}); |
| 101 | + simulant.fire(document, 'mouseup'); |
| 102 | + |
| 103 | + setTimeout(() => { |
| 104 | + expect(content.scrollLeft).toBe(100); |
| 105 | + done(); |
| 106 | + }, 50); |
| 107 | + }, 100); |
| 108 | + }); |
| 109 | + }); |
| 110 | + it("should add dragging class to thumb", (done) => { |
| 111 | + render(<Scrollbar style={ {width: 100, height: 100} } gridless> |
| 112 | + <div style={ {width: 200, height: 200} }></div> |
| 113 | + </Scrollbar>, |
| 114 | + node, |
| 115 | + function () { |
| 116 | + setTimeout(() => { |
| 117 | + const {thumbHorizontal: thumb} = this; |
| 118 | + const {left: thumbOffset} = thumb.getBoundingClientRect(); |
| 119 | + |
| 120 | + simulant.fire(thumb, 'mousedown', {target: thumb, clientX: thumbOffset, which: 1}); |
| 121 | + simulant.fire(document, 'mousemove', {clientX: thumbOffset + 100}); |
| 122 | + |
| 123 | + expect(thumb.classList.contains('dragging')).toBeTruthy(); |
| 124 | + |
| 125 | + simulant.fire(document, 'mouseup'); |
| 126 | + |
| 127 | + expect(thumb.classList.contains('dragging')).toBeFalsy(); |
| 128 | + done(); |
| 129 | + }, 100); |
| 130 | + }); |
| 131 | + }); |
| 132 | + it("should disable selection on the body element", (done) => { |
| 133 | + render(<Scrollbar style={ {width: 100, height: 100} } gridless> |
| 134 | + <div style={ {width: 200, height: 200} }></div> |
| 135 | + </Scrollbar>, |
| 136 | + node, |
| 137 | + function () { |
| 138 | + setTimeout(() => { |
| 139 | + const {thumbHorizontal: thumb} = this; |
| 140 | + const {left: thumbOffset} = thumb.getBoundingClientRect(); |
| 141 | + |
| 142 | + simulant.fire(thumb, 'mousedown', {target: thumb, clientX: thumbOffset, which: 1}); |
| 143 | + simulant.fire(document, 'mousemove', {clientX: thumbOffset + 100}); |
| 144 | + |
| 145 | + expect(document.body.style.userSelect).toBe("none"); |
| 146 | + |
| 147 | + simulant.fire(document, 'mouseup'); |
| 148 | + |
| 149 | + expect(document.body.style.userSelect).toBe(""); |
| 150 | + done(); |
| 151 | + }, 100); |
| 152 | + }); |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | +} |
0 commit comments