|
| 1 | +import { Scrollbars } from 'react-custom-scrollbars'; |
| 2 | +import React from 'react'; |
| 3 | +import { render, unmountComponentAtNode } from 'react-dom'; |
| 4 | + |
| 5 | +export default function createTests(scrollbarWidth, envScrollbarWidth) { |
| 6 | + describe('Scrollbars', () => { |
| 7 | + let node; |
| 8 | + beforeEach(() => { |
| 9 | + node = document.createElement('div'); |
| 10 | + document.body.appendChild(node); |
| 11 | + }); |
| 12 | + afterEach(() => { |
| 13 | + unmountComponentAtNode(node); |
| 14 | + document.body.removeChild(node); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('when rendering Scrollbars', () => { |
| 18 | + it('hides native scrollbars', done => { |
| 19 | + render(( |
| 20 | + <Scrollbars style={{ width: 100, height: 100 }}> |
| 21 | + <div style={{ width: 200, height: 200 }}/> |
| 22 | + </Scrollbars> |
| 23 | + ), node, function callback() { |
| 24 | + if (scrollbarWidth) { |
| 25 | + const width = `-${scrollbarWidth}px`; |
| 26 | + expect(this.refs.view.style.right).toEqual(width); |
| 27 | + expect(this.refs.view.style.bottom).toEqual(width); |
| 28 | + } else { |
| 29 | + expect(this.refs.view.style.right).toEqual(''); |
| 30 | + expect(this.refs.view.style.bottom).toEqual(''); |
| 31 | + } |
| 32 | + done(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renders view', done => { |
| 37 | + render(( |
| 38 | + <Scrollbars style={{ width: 100, height: 100 }}> |
| 39 | + <div style={{ width: 200, height: 200 }}/> |
| 40 | + </Scrollbars> |
| 41 | + ), node, function callback() { |
| 42 | + expect(this.refs.view).toBeA(Node); |
| 43 | + done(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('renders bars', done => { |
| 48 | + render(( |
| 49 | + <Scrollbars style={{ width: 100, height: 100 }}> |
| 50 | + <div style={{ width: 200, height: 200 }}/> |
| 51 | + </Scrollbars> |
| 52 | + ), node, function callback() { |
| 53 | + expect(this.refs.barHorizontal).toBeA(Node); |
| 54 | + expect(this.refs.barVertical).toBeA(Node); |
| 55 | + done(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders thumbs', done => { |
| 60 | + render(( |
| 61 | + <Scrollbars style={{ width: 100, height: 100 }}> |
| 62 | + <div style={{ width: 200, height: 200 }}/> |
| 63 | + </Scrollbars> |
| 64 | + ), node, function callback() { |
| 65 | + expect(this.refs.thumbHorizontal).toBeA(Node); |
| 66 | + expect(this.refs.thumbVertical).toBeA(Node); |
| 67 | + done(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('renders thumbs with correct size', done => { |
| 72 | + render(( |
| 73 | + <Scrollbars style={{ width: 100, height: 100 }}> |
| 74 | + <div style={{ width: 200, height: 200 }}/> |
| 75 | + </Scrollbars> |
| 76 | + ), node, function callback() { |
| 77 | + setTimeout(() => { |
| 78 | + if (scrollbarWidth) { |
| 79 | + expect(this.refs.thumbVertical.style.height).toEqual('50%'); |
| 80 | + expect(this.refs.thumbHorizontal.style.width).toEqual('50%'); |
| 81 | + } else { |
| 82 | + expect(this.refs.thumbVertical.style.height).toEqual(''); |
| 83 | + expect(this.refs.thumbHorizontal.style.width).toEqual(''); |
| 84 | + } |
| 85 | + done(); |
| 86 | + }, 100); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('when scrolling', () => { |
| 92 | + it('should update thumbs position', done => { |
| 93 | + render(( |
| 94 | + <Scrollbars style={{ width: 100, height: 100 }}> |
| 95 | + <div style={{ width: 200, height: 200 }}/> |
| 96 | + </Scrollbars> |
| 97 | + ), node, function callback() { |
| 98 | + this.scrollTop(50); |
| 99 | + this.scrollLeft(50); |
| 100 | + setTimeout(() => { |
| 101 | + if (scrollbarWidth) { |
| 102 | + expect(this.refs.thumbVertical.style.transform).toEqual('translateY(50%)'); |
| 103 | + expect(this.refs.thumbHorizontal.style.transform).toEqual('translateX(50%)'); |
| 104 | + } else { |
| 105 | + expect(this.refs.thumbVertical.style.transform).toEqual(''); |
| 106 | + expect(this.refs.thumbHorizontal.style.transform).toEqual(''); |
| 107 | + } |
| 108 | + done(); |
| 109 | + }, 100); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('when scrolling y-axis', () => { |
| 114 | + it('should call `onScroll`', done => { |
| 115 | + const spy = createSpy(); |
| 116 | + render(( |
| 117 | + <Scrollbars style={{ width: 100, height: 100 }} onScroll={spy}> |
| 118 | + <div style={{ width: 200, height: 200 }}/> |
| 119 | + </Scrollbars> |
| 120 | + ), node, function callback() { |
| 121 | + this.scrollTop(50); |
| 122 | + setTimeout(() => { |
| 123 | + expect(spy.calls.length).toEqual(1); |
| 124 | + const args = spy.calls[0].arguments; |
| 125 | + const event = args[0]; |
| 126 | + const values = args[1]; |
| 127 | + expect(event).toBeA(Event); |
| 128 | + expect(values).toBeA(Object); |
| 129 | + |
| 130 | + if (scrollbarWidth) { |
| 131 | + expect(values.left).toEqual(0); |
| 132 | + expect(values.top).toEqual(0.5); |
| 133 | + expect(values.scrollLeft).toEqual(0); |
| 134 | + expect(values.scrollTop).toEqual(50); |
| 135 | + expect(values.scrollWidth).toEqual(200); |
| 136 | + expect(values.scrollHeight).toEqual(200); |
| 137 | + expect(values.clientWidth).toEqual(100); |
| 138 | + expect(values.clientHeight).toEqual(100); |
| 139 | + } else { |
| 140 | + expect(values.left).toEqual(0); |
| 141 | + expect(values.top).toEqual(values.scrollTop / (values.scrollHeight - (values.clientHeight))); |
| 142 | + expect(values.scrollLeft).toEqual(0); |
| 143 | + expect(values.scrollTop).toEqual(50); |
| 144 | + expect(values.scrollWidth).toEqual(200); |
| 145 | + expect(values.scrollHeight).toEqual(200); |
| 146 | + expect(values.clientWidth).toEqual(100 - envScrollbarWidth); |
| 147 | + expect(values.clientHeight).toEqual(100 - envScrollbarWidth); |
| 148 | + } |
| 149 | + done(); |
| 150 | + }, 100); |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('when scrolling x-axis', () => { |
| 156 | + it('should call `onScroll`', done => { |
| 157 | + const spy = createSpy(); |
| 158 | + render(( |
| 159 | + <Scrollbars style={{ width: 100, height: 100 }} onScroll={spy}> |
| 160 | + <div style={{ width: 200, height: 200 }}/> |
| 161 | + </Scrollbars> |
| 162 | + ), node, function callback() { |
| 163 | + this.scrollLeft(50); |
| 164 | + setTimeout(() => { |
| 165 | + expect(spy.calls.length).toEqual(1); |
| 166 | + const args = spy.calls[0].arguments; |
| 167 | + const event = args[0]; |
| 168 | + const values = args[1]; |
| 169 | + expect(event).toBeA(Event); |
| 170 | + expect(values).toBeA(Object); |
| 171 | + |
| 172 | + if (scrollbarWidth) { |
| 173 | + expect(values.left).toEqual(0.5); |
| 174 | + expect(values.top).toEqual(0); |
| 175 | + expect(values.scrollLeft).toEqual(50); |
| 176 | + expect(values.scrollTop).toEqual(0); |
| 177 | + expect(values.scrollWidth).toEqual(200); |
| 178 | + expect(values.scrollHeight).toEqual(200); |
| 179 | + expect(values.clientWidth).toEqual(100); |
| 180 | + expect(values.clientHeight).toEqual(100); |
| 181 | + } else { |
| 182 | + expect(values.left).toEqual(values.scrollLeft / (values.scrollWidth - (values.clientWidth))); |
| 183 | + expect(values.top).toEqual(0); |
| 184 | + expect(values.scrollLeft).toEqual(50); |
| 185 | + expect(values.scrollTop).toEqual(0); |
| 186 | + expect(values.scrollWidth).toEqual(200); |
| 187 | + expect(values.scrollHeight).toEqual(200); |
| 188 | + expect(values.clientWidth).toEqual(100 - envScrollbarWidth); |
| 189 | + expect(values.clientHeight).toEqual(100 - envScrollbarWidth); |
| 190 | + } |
| 191 | + done(); |
| 192 | + }, 100); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe('when resizing window', () => { |
| 199 | + it('should update scrollbars', done => { |
| 200 | + render(( |
| 201 | + <Scrollbars style={{ width: 100, height: 100 }}> |
| 202 | + <div style={{ width: 200, height: 200 }}/> |
| 203 | + </Scrollbars> |
| 204 | + ), node, function callback() { |
| 205 | + setTimeout(() => { |
| 206 | + const spy = spyOn(this, 'update'); |
| 207 | + window.dispatchEvent(new Event('resize')); |
| 208 | + expect(spy.calls.length).toEqual(1); |
| 209 | + done(); |
| 210 | + }, 100); |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | + }); |
| 215 | +} |
0 commit comments