@pixui-dev/pxw 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/pxw.js +217 -0
- package/bin/wpbuild.js +10 -0
- package/config/default.conf +21 -0
- package/config/devops.js +358 -0
- package/config/h5es.js +10 -0
- package/config/index.html +181 -0
- package/config/pfbs.js +207 -0
- package/config/testhelp.js +0 -0
- package/config/util.js +140 -0
- package/config/webpack.js +291 -0
- package/lib/assets/border.png +0 -0
- package/lib/assets/check.html +62 -0
- package/lib/assets/pixui.png +0 -0
- package/lib/assets/preact.js +4354 -0
- package/lib/assets/preact.png +0 -0
- package/lib/check/main.less +63 -0
- package/lib/check/main.tsx +41 -0
- package/lib/check/tool.js +3 -0
- package/lib/check/util.tsx +110 -0
- package/lib/grpc-web/dist/ChunkParser.js +117 -0
- package/lib/grpc-web/dist/ChunkParser.js.map +1 -0
- package/lib/grpc-web/dist/Code.js +58 -0
- package/lib/grpc-web/dist/Code.js.map +1 -0
- package/lib/grpc-web/dist/client.js +299 -0
- package/lib/grpc-web/dist/client.js.map +1 -0
- package/lib/grpc-web/dist/debug.js +16 -0
- package/lib/grpc-web/dist/debug.js.map +1 -0
- package/lib/grpc-web/dist/detach.js +7 -0
- package/lib/grpc-web/dist/detach.js.map +1 -0
- package/lib/grpc-web/dist/index.js +29 -0
- package/lib/grpc-web/dist/index.js.map +1 -0
- package/lib/grpc-web/dist/invoke.js +32 -0
- package/lib/grpc-web/dist/invoke.js.map +1 -0
- package/lib/grpc-web/dist/message.js +3 -0
- package/lib/grpc-web/dist/message.js.map +1 -0
- package/lib/grpc-web/dist/metadata.js +5 -0
- package/lib/grpc-web/dist/metadata.js.map +1 -0
- package/lib/grpc-web/dist/service.js +3 -0
- package/lib/grpc-web/dist/service.js.map +1 -0
- package/lib/grpc-web/dist/transports/Transport.js +15 -0
- package/lib/grpc-web/dist/transports/Transport.js.map +1 -0
- package/lib/grpc-web/dist/transports/http/fetch.js +117 -0
- package/lib/grpc-web/dist/transports/http/fetch.js.map +1 -0
- package/lib/grpc-web/dist/transports/http/http.js +15 -0
- package/lib/grpc-web/dist/transports/http/http.js.map +1 -0
- package/lib/grpc-web/dist/transports/http/xhr.js +136 -0
- package/lib/grpc-web/dist/transports/http/xhr.js.map +1 -0
- package/lib/grpc-web/dist/transports/http/xhrUtil.js +36 -0
- package/lib/grpc-web/dist/transports/http/xhrUtil.js.map +1 -0
- package/lib/grpc-web/dist/transports/websocket/websocket.js +95 -0
- package/lib/grpc-web/dist/transports/websocket/websocket.js.map +1 -0
- package/lib/grpc-web/dist/typings/ChunkParser.d.ts +17 -0
- package/lib/grpc-web/dist/typings/Code.d.ts +20 -0
- package/lib/grpc-web/dist/typings/client.d.ts +25 -0
- package/lib/grpc-web/dist/typings/debug.d.ts +1 -0
- package/lib/grpc-web/dist/typings/detach.d.ts +1 -0
- package/lib/grpc-web/dist/typings/index.d.ts +45 -0
- package/lib/grpc-web/dist/typings/invoke.d.ts +20 -0
- package/lib/grpc-web/dist/typings/message.d.ts +8 -0
- package/lib/grpc-web/dist/typings/metadata.d.ts +2 -0
- package/lib/grpc-web/dist/typings/service.d.ts +16 -0
- package/lib/grpc-web/dist/typings/transports/Transport.d.ts +22 -0
- package/lib/grpc-web/dist/typings/transports/http/fetch.d.ts +6 -0
- package/lib/grpc-web/dist/typings/transports/http/http.d.ts +5 -0
- package/lib/grpc-web/dist/typings/transports/http/xhr.d.ts +27 -0
- package/lib/grpc-web/dist/typings/transports/http/xhrUtil.d.ts +3 -0
- package/lib/grpc-web/dist/typings/transports/websocket/websocket.d.ts +2 -0
- package/lib/grpc-web/dist/typings/unary.d.ts +23 -0
- package/lib/grpc-web/dist/typings/util.d.ts +2 -0
- package/lib/grpc-web/dist/unary.js +44 -0
- package/lib/grpc-web/dist/unary.js.map +1 -0
- package/lib/grpc-web/dist/util.js +11 -0
- package/lib/grpc-web/dist/util.js.map +1 -0
- package/lib/grpcTransport/PixHttp2Transport.ts +107 -0
- package/lib/grpcTransport/PixLuaTransport.ts +82 -0
- package/lib/h5es-types/v1.9.2/h5es.d.ts +1698 -0
- package/lib/h5es-types/v3.5.0/h5es.d.ts +1788 -0
- package/lib/pi_component/tinyList/tinyList.js +483 -0
- package/lib/pi_component/tinyList/tinyList.tsx +517 -0
- package/lib/pika-svelte/compiler.js +29829 -0
- package/lib/pika-svelte/easing/index.js +158 -0
- package/lib/pika-svelte/index.js +72 -0
- package/lib/pika-svelte/internal/index.js +1926 -0
- package/lib/pika-svelte/motion/index.js +210 -0
- package/lib/pika-svelte/package.json +145 -0
- package/lib/pika-svelte/register.js +57 -0
- package/lib/pika-svelte/rollup.config.js +126 -0
- package/lib/pika-svelte/store/index.js +123 -0
- package/lib/pika-svelte/transition/index.js +185 -0
- package/lib/preact-router.js +395 -0
- package/lib/preact.js +4355 -0
- package/lib/preact.tq.js +4385 -0
- package/lib/react-window/src/FixedSizeGrid.js +172 -0
- package/lib/react-window/src/FixedSizeList.js +91 -0
- package/lib/react-window/src/VariableSizeGrid.js +329 -0
- package/lib/react-window/src/VariableSizeList.js +231 -0
- package/lib/react-window/src/__tests__/FixedSizeGrid.js +942 -0
- package/lib/react-window/src/__tests__/FixedSizeList.js +749 -0
- package/lib/react-window/src/__tests__/VariableSizeGrid.js +598 -0
- package/lib/react-window/src/__tests__/VariableSizeList.js +345 -0
- package/lib/react-window/src/__tests__/__snapshots__/FixedSizeGrid.js.snap +912 -0
- package/lib/react-window/src/__tests__/__snapshots__/FixedSizeList.js.snap +568 -0
- package/lib/react-window/src/__tests__/__snapshots__/VariableSizeGrid.js.snap +542 -0
- package/lib/react-window/src/__tests__/__snapshots__/VariableSizeList.js.snap +331 -0
- package/lib/react-window/src/__tests__/areEqual.js +28 -0
- package/lib/react-window/src/__tests__/shouldComponentUpdate.js +32 -0
- package/lib/react-window/src/areEqual.js +13 -0
- package/lib/react-window/src/createGridComponent.js +657 -0
- package/lib/react-window/src/createListComponent.js +574 -0
- package/lib/react-window/src/domHelpers.js +69 -0
- package/lib/react-window/src/index.js +9 -0
- package/lib/react-window/src/shallowDiffers.js +17 -0
- package/lib/react-window/src/shouldComponentUpdate.js +11 -0
- package/lib/react-window/src/test.js.flow +382 -0
- package/lib/react-window/src/timer.js +36 -0
- package/lib/svelte-tab/Tab.svelte +31 -0
- package/lib/svelte-tab/TabList.svelte +10 -0
- package/lib/svelte-tab/TabPanel.svelte +13 -0
- package/lib/svelte-tab/Tabs.svelte +68 -0
- package/lib/svelte-tab/tabs.js +4 -0
- package/lib/types/css.d.ts +7476 -0
- package/lib/types/dom.ts +17 -0
- package/lib/types/ext.d.ts +81 -0
- package/lib/types/internal.d.ts +94 -0
- package/lib/types/jsx.d.ts +309 -0
- package/lib/types/preact.d.ts +340 -0
- package/package.json +111 -0
- package/tsconfig.json +30 -0
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
import React, { createRef, PureComponent } from 'react';
|
|
2
|
+
import { render } from 'react-dom';
|
|
3
|
+
import { Simulate } from 'react-dom/test-utils';
|
|
4
|
+
import ReactTestRenderer from 'react-test-renderer';
|
|
5
|
+
import { VariableSizeGrid } from '..';
|
|
6
|
+
import * as domHelpers from '../domHelpers';
|
|
7
|
+
|
|
8
|
+
const simulateScroll = (instance, { scrollLeft, scrollTop }) => {
|
|
9
|
+
instance._outerRef.scrollLeft = scrollLeft;
|
|
10
|
+
instance._outerRef.scrollTop = scrollTop;
|
|
11
|
+
Simulate.scroll(instance._outerRef);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const findScrollContainer = (rendered) => rendered.root.children[0].children[0];
|
|
15
|
+
|
|
16
|
+
describe('VariableSizeGrid', () => {
|
|
17
|
+
let columnWidth, defaultProps, getScrollbarSize, itemRenderer, onItemsRendered, rowHeight;
|
|
18
|
+
|
|
19
|
+
// Use PureComponent to test memoization.
|
|
20
|
+
// Pass through to itemRenderer mock for easier test assertions.
|
|
21
|
+
class PureItemRenderer extends PureComponent {
|
|
22
|
+
render() {
|
|
23
|
+
return itemRenderer(this.props);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const findItemRendererCall = (rowIndex: number, columnIndex: number) => {
|
|
28
|
+
const found = itemRenderer.mock.calls.find(([params]) => params.rowIndex === rowIndex && params.columnIndex === columnIndex);
|
|
29
|
+
return found.length === 1 ? found[0] : null;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
jest.useFakeTimers();
|
|
34
|
+
|
|
35
|
+
// JSdom does not do actual layout and so doesn't return meaningful values here.
|
|
36
|
+
// For the purposes of our tests though, we can mock out semi-meaningful values.
|
|
37
|
+
// This mock is required for e.g. "onScroll" tests to work properly.
|
|
38
|
+
Object.defineProperties(HTMLElement.prototype, {
|
|
39
|
+
clientWidth: {
|
|
40
|
+
configurable: true,
|
|
41
|
+
get: function () {
|
|
42
|
+
return parseInt(this.style.width, 10) || 0;
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
clientHeight: {
|
|
46
|
+
configurable: true,
|
|
47
|
+
get: function () {
|
|
48
|
+
return parseInt(this.style.height, 10) || 0;
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
scrollHeight: {
|
|
52
|
+
configurable: true,
|
|
53
|
+
get: () => Number.MAX_SAFE_INTEGER,
|
|
54
|
+
},
|
|
55
|
+
scrollWidth: {
|
|
56
|
+
configurable: true,
|
|
57
|
+
get: () => Number.MAX_SAFE_INTEGER,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Mock the DOM helper util for testing purposes.
|
|
62
|
+
getScrollbarSize = domHelpers.getScrollbarSize = jest.fn(() => 0);
|
|
63
|
+
|
|
64
|
+
itemRenderer = jest.fn(({ style, ...rest }) => <div style={style}>{JSON.stringify(rest, null, 2)}</div>);
|
|
65
|
+
onItemsRendered = jest.fn();
|
|
66
|
+
columnWidth = jest.fn((index) => 50 + index);
|
|
67
|
+
rowHeight = jest.fn((index) => 25 + index);
|
|
68
|
+
defaultProps = {
|
|
69
|
+
children: PureItemRenderer,
|
|
70
|
+
columnCount: 10,
|
|
71
|
+
columnWidth,
|
|
72
|
+
height: 100,
|
|
73
|
+
onItemsRendered,
|
|
74
|
+
rowCount: 20,
|
|
75
|
+
rowHeight,
|
|
76
|
+
width: 200,
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Much of the shared Grid functionality is already tested by VariableSizeGrid tests.
|
|
81
|
+
// This test covers functionality that is unique to VariableSizeGrid.
|
|
82
|
+
|
|
83
|
+
it('should render an empty grid', () => {
|
|
84
|
+
ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} columnCount={0} rowCount={0} />);
|
|
85
|
+
ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} columnCount={0} />);
|
|
86
|
+
ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} rowCount={0} />);
|
|
87
|
+
expect(itemRenderer).not.toHaveBeenCalled();
|
|
88
|
+
expect(columnWidth).not.toHaveBeenCalled();
|
|
89
|
+
expect(rowHeight).not.toHaveBeenCalled();
|
|
90
|
+
expect(onItemsRendered).not.toHaveBeenCalled();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('changing item size does not impact the rendered items', () => {
|
|
94
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} />);
|
|
95
|
+
itemRenderer.mockClear();
|
|
96
|
+
rendered.update(<VariableSizeGrid {...defaultProps} columnWidth={(index) => 100} rowHeight={(index) => 50} />);
|
|
97
|
+
expect(itemRenderer).not.toHaveBeenCalled();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('estimatedColumnWidth and estimatedRowHeight', () => {
|
|
101
|
+
it('should estimate an initial scrollable size based on this value', () => {
|
|
102
|
+
const columnWidth = jest.fn(() => 50);
|
|
103
|
+
const rowHeight = jest.fn(() => 25);
|
|
104
|
+
const rendered = ReactTestRenderer.create(
|
|
105
|
+
<VariableSizeGrid
|
|
106
|
+
{...defaultProps}
|
|
107
|
+
columnCount={50}
|
|
108
|
+
columnWidth={columnWidth}
|
|
109
|
+
estimatedColumnWidth={200}
|
|
110
|
+
estimatedRowHeight={100}
|
|
111
|
+
overscanColumnCount={0}
|
|
112
|
+
overscanRowCount={0}
|
|
113
|
+
rowCount={50}
|
|
114
|
+
rowHeight={rowHeight}
|
|
115
|
+
/>,
|
|
116
|
+
);
|
|
117
|
+
// We'll render 5 columns and 5 rows initially (250px wide by 125px tall).
|
|
118
|
+
// The remaining 45 columns and 45 rows will be estimated (9,000px wide by 4,500px tall).
|
|
119
|
+
expect(columnWidth).toHaveBeenCalledTimes(5);
|
|
120
|
+
expect(rowHeight).toHaveBeenCalledTimes(5);
|
|
121
|
+
const scrollContainer = findScrollContainer(rendered);
|
|
122
|
+
expect(scrollContainer.props.style.width).toEqual(9250);
|
|
123
|
+
expect(scrollContainer.props.style.height).toEqual(4625);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('should udpate the scrollable size as more items are measured', () => {
|
|
127
|
+
const columnWidth = jest.fn(() => 50);
|
|
128
|
+
const rowHeight = jest.fn(() => 25);
|
|
129
|
+
const rendered = ReactTestRenderer.create(
|
|
130
|
+
<VariableSizeGrid
|
|
131
|
+
{...defaultProps}
|
|
132
|
+
columnCount={50}
|
|
133
|
+
columnWidth={columnWidth}
|
|
134
|
+
estimatedColumnWidth={200}
|
|
135
|
+
estimatedRowHeight={100}
|
|
136
|
+
overscanColumnCount={0}
|
|
137
|
+
overscanRowCount={0}
|
|
138
|
+
rowCount={50}
|
|
139
|
+
rowHeight={rowHeight}
|
|
140
|
+
/>,
|
|
141
|
+
);
|
|
142
|
+
rendered.getInstance().scrollToItem({ columnIndex: 13, rowIndex: 23 });
|
|
143
|
+
// At this point we have measured 15 columns and 25 rows (750px wide by 625px tall).
|
|
144
|
+
// The remaining 35 columns and 25 rows will be estimated (7,000px wide by 2,500px tall).
|
|
145
|
+
expect(columnWidth).toHaveBeenCalledTimes(15);
|
|
146
|
+
expect(rowHeight).toHaveBeenCalledTimes(25);
|
|
147
|
+
const scrollContainer = findScrollContainer(rendered);
|
|
148
|
+
expect(scrollContainer.props.style.width).toEqual(7750);
|
|
149
|
+
expect(scrollContainer.props.style.height).toEqual(3125);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('scrollToItem method', () => {
|
|
154
|
+
it('should not set invalid offsets when the list contains few items', () => {
|
|
155
|
+
const onScroll = jest.fn();
|
|
156
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} columnCount={1} rowCount={2} onScroll={onScroll} />);
|
|
157
|
+
expect(onItemsRendered).toMatchSnapshot();
|
|
158
|
+
onItemsRendered.mockClear();
|
|
159
|
+
rendered.getInstance().scrollToItem(0);
|
|
160
|
+
expect(onItemsRendered).not.toHaveBeenCalled();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should scroll to the correct item for align = "auto"', () => {
|
|
164
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} />);
|
|
165
|
+
// Scroll down enough to show item 10 at the bottom.
|
|
166
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 5, align: 'auto' });
|
|
167
|
+
// No need to scroll again; item 9 is already visible.
|
|
168
|
+
// Overscan indices will change though, since direction changes.
|
|
169
|
+
rendered.getInstance().scrollToItem({ columnIndex: 4, rowIndex: 4, align: 'auto' });
|
|
170
|
+
// Scroll up enough to show item 2 at the top.
|
|
171
|
+
rendered.getInstance().scrollToItem({ columnIndex: 2, rowIndex: 2, align: 'auto' });
|
|
172
|
+
// Scroll down to row 10, without changing scrollLeft
|
|
173
|
+
rendered.getInstance().scrollToItem({ rowIndex: 10, align: 'auto' });
|
|
174
|
+
// Scroll left to column 0, without changing scrollTop
|
|
175
|
+
rendered.getInstance().scrollToItem({ columnIndex: 0, align: 'auto' });
|
|
176
|
+
expect(onItemsRendered.mock.calls).toMatchSnapshot();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('scroll with align = "auto" should work with partially-visible items', () => {
|
|
180
|
+
const rendered = ReactTestRenderer.create(
|
|
181
|
+
// Create list where items don't fit exactly into container.
|
|
182
|
+
// The container has space for 3 1/3 items.
|
|
183
|
+
<VariableSizeGrid {...defaultProps} columnCount={100} columnWidth={() => 70} rowCount={100} rowHeight={() => 30} />,
|
|
184
|
+
);
|
|
185
|
+
// Scroll down enough to show row 10 at the bottom a nd column 10 at the right.
|
|
186
|
+
// Should show 4 rows: 3 full and one partial at the beginning
|
|
187
|
+
// Should show 3 columns: 2 full and one partial at the beginning
|
|
188
|
+
rendered.getInstance().scrollToItem({ columnIndex: 10, rowIndex: 10, align: 'auto' });
|
|
189
|
+
// No need to scroll again; row and column 9 are already visible.
|
|
190
|
+
// Because there's no scrolling, it won't call onItemsRendered.
|
|
191
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 9, align: 'auto' });
|
|
192
|
+
// Scroll to near the end. row 96 and column 97 will be partly visible.
|
|
193
|
+
rendered.getInstance().scrollToItem({ columnIndex: 99, rowIndex: 99, align: 'auto' });
|
|
194
|
+
// Scroll back to row 91 and column 97.
|
|
195
|
+
// This will cause row 99 and column 99 to be partly viisble
|
|
196
|
+
// Even though a scroll happened, none of the items rendered have changed.
|
|
197
|
+
rendered.getInstance().scrollToItem({ columnIndex: 97, rowIndex: 96, align: 'auto' });
|
|
198
|
+
// Scroll forward again. Because row and column #99 were already partly visible,
|
|
199
|
+
// all props of the onItemsRendered will be the same.
|
|
200
|
+
rendered.getInstance().scrollToItem({ columnIndex: 99, rowIndex: 99, align: 'auto' });
|
|
201
|
+
// Scroll to the second row and column.
|
|
202
|
+
// This should leave row 4 and column 3 partly visible.
|
|
203
|
+
rendered.getInstance().scrollToItem({ columnIndex: 1, rowIndex: 1, align: 'auto' });
|
|
204
|
+
// Scroll to the first row and column.
|
|
205
|
+
// This should leave row 3 and column 2 partly visible.
|
|
206
|
+
rendered.getInstance().scrollToItem({ columnIndex: 0, rowIndex: 0, align: 'auto' });
|
|
207
|
+
expect(onItemsRendered.mock.calls).toMatchSnapshot();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('should scroll to the correct item for align = "auto" at the bottom of the grid', () => {
|
|
211
|
+
getScrollbarSize.mockImplementation(() => 20);
|
|
212
|
+
|
|
213
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} />);
|
|
214
|
+
onItemsRendered.mockClear();
|
|
215
|
+
|
|
216
|
+
// Scroll down to the last row in the list.
|
|
217
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 19, align: 'auto' });
|
|
218
|
+
|
|
219
|
+
expect(onItemsRendered).toHaveBeenCalledTimes(1);
|
|
220
|
+
expect(onItemsRendered).toHaveBeenLastCalledWith(
|
|
221
|
+
expect.objectContaining({
|
|
222
|
+
visibleRowStartIndex: 18,
|
|
223
|
+
visibleRowStopIndex: 19,
|
|
224
|
+
}),
|
|
225
|
+
);
|
|
226
|
+
// Repeat the previous scrollToItem call.
|
|
227
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 19, align: 'auto' });
|
|
228
|
+
|
|
229
|
+
// Shouldn't have been called again
|
|
230
|
+
expect(onItemsRendered).toHaveBeenCalledTimes(1);
|
|
231
|
+
expect(onItemsRendered).toHaveBeenLastCalledWith(
|
|
232
|
+
expect.objectContaining({
|
|
233
|
+
visibleRowStartIndex: 18,
|
|
234
|
+
visibleRowStopIndex: 19,
|
|
235
|
+
}),
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('should scroll to the correct item for align = "auto" at the right hand side of the grid', () => {
|
|
240
|
+
getScrollbarSize.mockImplementation(() => 20);
|
|
241
|
+
|
|
242
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} width={120} />);
|
|
243
|
+
onItemsRendered.mockClear();
|
|
244
|
+
|
|
245
|
+
// Scroll scross to the last row in the list.
|
|
246
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 10, align: 'auto' });
|
|
247
|
+
|
|
248
|
+
expect(onItemsRendered).toHaveBeenCalledTimes(1);
|
|
249
|
+
expect(onItemsRendered).toHaveBeenLastCalledWith(
|
|
250
|
+
expect.objectContaining({
|
|
251
|
+
visibleColumnStartIndex: 8,
|
|
252
|
+
visibleColumnStopIndex: 9,
|
|
253
|
+
}),
|
|
254
|
+
);
|
|
255
|
+
// Repeat the previous scrollToItem call.
|
|
256
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 10, align: 'auto' });
|
|
257
|
+
|
|
258
|
+
// Shouldn't have been called again
|
|
259
|
+
expect(onItemsRendered).toHaveBeenCalledTimes(1);
|
|
260
|
+
expect(onItemsRendered).toHaveBeenLastCalledWith(
|
|
261
|
+
expect.objectContaining({
|
|
262
|
+
visibleColumnStartIndex: 8,
|
|
263
|
+
visibleColumnStopIndex: 9,
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('should scroll to the correct item for align = "start"', () => {
|
|
269
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} />);
|
|
270
|
+
// Scroll down enough to show item 10 at the top.
|
|
271
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 5, align: 'start' });
|
|
272
|
+
// Scroll back up so that item 9 is at the top.
|
|
273
|
+
// Overscroll direction wil change too.
|
|
274
|
+
rendered.getInstance().scrollToItem({ columnIndex: 4, rowIndex: 4, align: 'start' });
|
|
275
|
+
// Item 99 can't align at the top because there aren't enough items.
|
|
276
|
+
// Scroll down as far as possible though.
|
|
277
|
+
// Overscroll direction wil change again.
|
|
278
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 19, align: 'start' });
|
|
279
|
+
// Scroll up to row 10, without changing scrollLeft
|
|
280
|
+
rendered.getInstance().scrollToItem({ rowIndex: 10, align: 'start' });
|
|
281
|
+
// Scroll left to column 0, without changing scrollTop
|
|
282
|
+
rendered.getInstance().scrollToItem({ columnIndex: 0, align: 'start' });
|
|
283
|
+
expect(onItemsRendered.mock.calls).toMatchSnapshot();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should scroll to the correct item for align = "end"', () => {
|
|
287
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} />);
|
|
288
|
+
// Scroll down enough to show item 10 at the bottom.
|
|
289
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 5, align: 'end' });
|
|
290
|
+
// Scroll back up so that item 9 is at the bottom.
|
|
291
|
+
// Overscroll direction wil change too.
|
|
292
|
+
rendered.getInstance().scrollToItem({ columnIndex: 4, rowIndex: 4, align: 'end' });
|
|
293
|
+
// Item 1 can't align at the bottom because it's too close to the beginning.
|
|
294
|
+
// Scroll up as far as possible though.
|
|
295
|
+
// Overscroll direction wil change again.
|
|
296
|
+
rendered.getInstance().scrollToItem({ columnIndex: 1, rowIndex: 1, align: 'end' });
|
|
297
|
+
// Scroll down to row 10, without changing scrollLeft
|
|
298
|
+
rendered.getInstance().scrollToItem({ rowIndex: 10, align: 'end' });
|
|
299
|
+
// Scroll right to column 9, without changing scrollTop
|
|
300
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, align: 'end' });
|
|
301
|
+
expect(onItemsRendered.mock.calls).toMatchSnapshot();
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('should scroll to the correct item for align = "center"', () => {
|
|
305
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} />);
|
|
306
|
+
// Scroll down enough to show item 10 in the middle.
|
|
307
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 5, align: 'center' });
|
|
308
|
+
// Scroll back up so that item 9 is in the middle.
|
|
309
|
+
// Overscroll direction wil change too.
|
|
310
|
+
rendered.getInstance().scrollToItem({ columnIndex: 4, rowIndex: 4, align: 'center' });
|
|
311
|
+
// Item 1 can't align in the middle because it's too close to the beginning.
|
|
312
|
+
// Scroll up as far as possible though.
|
|
313
|
+
// Overscroll direction wil change again.
|
|
314
|
+
rendered.getInstance().scrollToItem({ columnIndex: 1, rowIndex: 1, align: 'center' });
|
|
315
|
+
// Item 99 can't align in the middle because it's too close to the end.
|
|
316
|
+
// Scroll down as far as possible though.
|
|
317
|
+
// Overscroll direction wil change again.
|
|
318
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 19, align: 'center' });
|
|
319
|
+
// Scroll up to row 10, without changing scrollLeft
|
|
320
|
+
rendered.getInstance().scrollToItem({ rowIndex: 10, align: 'center' });
|
|
321
|
+
// Scroll left to column 3, without changing scrollTop
|
|
322
|
+
rendered.getInstance().scrollToItem({ columnIndex: 3, align: 'center' });
|
|
323
|
+
expect(onItemsRendered.mock.calls).toMatchSnapshot();
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('should scroll to the correct item for align = "smart"', () => {
|
|
327
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} />);
|
|
328
|
+
|
|
329
|
+
// Scroll down enough to show item 10 at the center.
|
|
330
|
+
// It was further than one screen away, so it gets centered.
|
|
331
|
+
rendered.getInstance().scrollToItem({ columnIndex: 10, rowIndex: 10, align: 'smart' });
|
|
332
|
+
// No need to scroll again; item 9 is already visible.
|
|
333
|
+
// Overscan indices will change though, since direction changes.
|
|
334
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 9, align: 'smart' });
|
|
335
|
+
// Scroll up enough to show item 2 as close to the center as we can.
|
|
336
|
+
rendered.getInstance().scrollToItem({ columnIndex: 2, rowIndex: 2, align: 'smart' });
|
|
337
|
+
// Scroll down to row 10, without changing scrollLeft
|
|
338
|
+
rendered.getInstance().scrollToItem({ rowIndex: 10, align: 'smart' });
|
|
339
|
+
// Scroll left to column 0, without changing scrollTop
|
|
340
|
+
rendered.getInstance().scrollToItem({ columnIndex: 0, align: 'smart' });
|
|
341
|
+
|
|
342
|
+
// Scrolling within a distance of a single screen from viewport
|
|
343
|
+
// should have the 'auto' behavior of scrolling as little as possible.
|
|
344
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 5, align: 'smart' });
|
|
345
|
+
rendered.getInstance().scrollToItem({ columnIndex: 10, rowIndex: 10, align: 'smart' });
|
|
346
|
+
expect(onItemsRendered.mock.calls).toMatchSnapshot();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('should account for scrollbar size', () => {
|
|
350
|
+
const onScroll = jest.fn();
|
|
351
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} onScroll={onScroll} />);
|
|
352
|
+
|
|
353
|
+
onScroll.mockClear();
|
|
354
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 10, align: 'end' });
|
|
355
|
+
|
|
356
|
+
// With hidden scrollbars (size === 0) we would expect...
|
|
357
|
+
expect(onScroll).toHaveBeenCalledWith({
|
|
358
|
+
horizontalScrollDirection: 'forward',
|
|
359
|
+
scrollLeft: 115,
|
|
360
|
+
scrollTop: 230,
|
|
361
|
+
scrollUpdateWasRequested: true,
|
|
362
|
+
verticalScrollDirection: 'forward',
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
getScrollbarSize.mockImplementation(() => 20);
|
|
366
|
+
|
|
367
|
+
onScroll.mockClear();
|
|
368
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 10, align: 'end' });
|
|
369
|
+
|
|
370
|
+
// With scrollbars of size 20 we would expect those values ot increase by 20px
|
|
371
|
+
expect(onScroll).toHaveBeenCalledWith({
|
|
372
|
+
horizontalScrollDirection: 'forward',
|
|
373
|
+
scrollLeft: 135,
|
|
374
|
+
scrollTop: 250,
|
|
375
|
+
scrollUpdateWasRequested: true,
|
|
376
|
+
verticalScrollDirection: 'forward',
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it('should not account for scrollbar size when no scrollbar is visible for a particular direction', () => {
|
|
381
|
+
getScrollbarSize.mockImplementation(() => 20);
|
|
382
|
+
|
|
383
|
+
const onScroll = jest.fn();
|
|
384
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} columnCount={1} onScroll={onScroll} />);
|
|
385
|
+
|
|
386
|
+
onScroll.mockClear();
|
|
387
|
+
rendered.getInstance().scrollToItem({ columnIndex: 0, rowIndex: 10, align: 'end' });
|
|
388
|
+
|
|
389
|
+
// Since there aren't enough columns to require horizontal scrolling,
|
|
390
|
+
// the additional 20px for the scrollbar should not be taken into consideration.
|
|
391
|
+
expect(onScroll).toHaveBeenCalledWith({
|
|
392
|
+
horizontalScrollDirection: 'backward',
|
|
393
|
+
scrollLeft: 0,
|
|
394
|
+
scrollTop: 230,
|
|
395
|
+
scrollUpdateWasRequested: true,
|
|
396
|
+
verticalScrollDirection: 'forward',
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
rendered.update(<VariableSizeGrid {...defaultProps} rowCount={1} onScroll={onScroll} />);
|
|
400
|
+
|
|
401
|
+
onScroll.mockClear();
|
|
402
|
+
rendered.getInstance().scrollToItem({ columnIndex: 5, rowIndex: 0, align: 'end' });
|
|
403
|
+
|
|
404
|
+
// Since there aren't enough rows to require vertical scrolling,
|
|
405
|
+
// the additional 20px for the scrollbar should not be taken into consideration.
|
|
406
|
+
expect(onScroll).toHaveBeenCalledWith({
|
|
407
|
+
horizontalScrollDirection: 'forward',
|
|
408
|
+
scrollLeft: 115,
|
|
409
|
+
scrollTop: 0,
|
|
410
|
+
scrollUpdateWasRequested: true,
|
|
411
|
+
verticalScrollDirection: 'backward',
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
describe('resetAfterIndex method', () => {
|
|
417
|
+
it('should recalculate the estimated total size', () => {
|
|
418
|
+
const columnWidth = jest.fn(() => 75);
|
|
419
|
+
const rowHeight = jest.fn(() => 35);
|
|
420
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} columnWidth={(index) => 50} rowHeight={(index) => 25} />);
|
|
421
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 19 });
|
|
422
|
+
// We've measured every item initially.
|
|
423
|
+
const scrollContainer = findScrollContainer(rendered);
|
|
424
|
+
expect(scrollContainer.props.style.height).toEqual(500);
|
|
425
|
+
expect(scrollContainer.props.style.width).toEqual(500);
|
|
426
|
+
// Supplying new item sizes alone should not impact anything.
|
|
427
|
+
rendered.update(<VariableSizeGrid {...defaultProps} columnWidth={columnWidth} rowHeight={rowHeight} />);
|
|
428
|
+
expect(scrollContainer.props.style.height).toEqual(500);
|
|
429
|
+
expect(scrollContainer.props.style.width).toEqual(500);
|
|
430
|
+
// Reset styles after index 75,
|
|
431
|
+
// And verify that the new estimated total takes this into account.
|
|
432
|
+
// This means 5 columns at 50px each and 5 at 75px each (625),
|
|
433
|
+
// And 15 rows at 25px each and 5 at 35px each (550px).
|
|
434
|
+
rendered.getInstance().resetAfterIndices({ columnIndex: 5, rowIndex: 15 });
|
|
435
|
+
rendered.getInstance().scrollToItem({ columnIndex: 9, rowIndex: 19 });
|
|
436
|
+
expect(columnWidth).toHaveBeenCalledTimes(5);
|
|
437
|
+
expect(rowHeight).toHaveBeenCalledTimes(5);
|
|
438
|
+
expect(scrollContainer.props.style.height).toEqual(550);
|
|
439
|
+
expect(scrollContainer.props.style.width).toEqual(625);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it('should delay the recalculation of the estimated total size if shouldForceUpdate is false', () => {
|
|
443
|
+
const rendered = ReactTestRenderer.create(
|
|
444
|
+
<VariableSizeGrid
|
|
445
|
+
{...defaultProps}
|
|
446
|
+
estimatedColumnWidth={30}
|
|
447
|
+
estimatedRowHeight={30}
|
|
448
|
+
overscanColumnCount={1}
|
|
449
|
+
overscanRowCount={1}
|
|
450
|
+
columnWidth={(index) => 50}
|
|
451
|
+
rowHeight={(index) => 25}
|
|
452
|
+
/>,
|
|
453
|
+
);
|
|
454
|
+
const scrollContainer = findScrollContainer(rendered);
|
|
455
|
+
// The estimated total height should be (100 + 25 * 1 + 30 * 15)px = 575px.
|
|
456
|
+
// The estimated total width should be (200 + 50 * 1 + 30 * 5)px = 400px.
|
|
457
|
+
expect(scrollContainer.props.style.height).toEqual(575);
|
|
458
|
+
expect(scrollContainer.props.style.width).toEqual(400);
|
|
459
|
+
// Supplying new item sizes alone should not impact anything.
|
|
460
|
+
// Although the grid get re-rendered by passing inline functions,
|
|
461
|
+
// but it still use the cached metrics to calculate the estimated size.
|
|
462
|
+
rendered.update(
|
|
463
|
+
<VariableSizeGrid
|
|
464
|
+
{...defaultProps}
|
|
465
|
+
estimatedColumnWidth={30}
|
|
466
|
+
estimatedRowHeight={30}
|
|
467
|
+
overscanColumnCount={1}
|
|
468
|
+
overscanRowCount={1}
|
|
469
|
+
columnWidth={(index) => 40}
|
|
470
|
+
rowHeight={(index) => 20}
|
|
471
|
+
/>,
|
|
472
|
+
);
|
|
473
|
+
expect(scrollContainer.props.style.height).toEqual(575);
|
|
474
|
+
expect(scrollContainer.props.style.width).toEqual(400);
|
|
475
|
+
// Reset calculation cache but don't re-render the grid,
|
|
476
|
+
// the estimated total size should stay the same.
|
|
477
|
+
rendered.getInstance().resetAfterIndices({
|
|
478
|
+
columnIndex: 0,
|
|
479
|
+
rowIndex: 0,
|
|
480
|
+
shouldForceUpdate: false,
|
|
481
|
+
});
|
|
482
|
+
expect(scrollContainer.props.style.height).toEqual(575);
|
|
483
|
+
expect(scrollContainer.props.style.width).toEqual(400);
|
|
484
|
+
// Pass inline function to make the grid re-render.
|
|
485
|
+
rendered.update(
|
|
486
|
+
<VariableSizeGrid
|
|
487
|
+
{...defaultProps}
|
|
488
|
+
estimatedColumnWidth={30}
|
|
489
|
+
estimatedRowHeight={30}
|
|
490
|
+
overscanColumnCount={1}
|
|
491
|
+
overscanRowCount={1}
|
|
492
|
+
columnWidth={(index) => 40}
|
|
493
|
+
rowHeight={(index) => 20}
|
|
494
|
+
/>,
|
|
495
|
+
);
|
|
496
|
+
// The estimated total height should be (100 + 20 * 1 + 30 * 14)px = 540px.
|
|
497
|
+
// The estimated total width should be (200 + 40 * 1 + 30 * 4)px = 360px.
|
|
498
|
+
expect(scrollContainer.props.style.height).toEqual(540);
|
|
499
|
+
expect(scrollContainer.props.style.width).toEqual(360);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it('should re-render items after the specified index with updated styles', () => {
|
|
503
|
+
const columnWidth = jest.fn(() => 75);
|
|
504
|
+
const rowHeight = jest.fn(() => 35);
|
|
505
|
+
const rendered = ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} columnWidth={(index) => 50} rowHeight={(index) => 25} />);
|
|
506
|
+
// We've rendered 5 columns and 5 rows initially.
|
|
507
|
+
expect(itemRenderer).toHaveBeenCalledTimes(25);
|
|
508
|
+
expect(findItemRendererCall(3, 3).style.height).toBe(25);
|
|
509
|
+
expect(findItemRendererCall(3, 3).style.width).toBe(50);
|
|
510
|
+
// Supplying new item sizes alone should not impact anything.
|
|
511
|
+
rendered.update(<VariableSizeGrid {...defaultProps} columnCount={5} columnWidth={columnWidth} rowCount={5} rowHeight={rowHeight} />);
|
|
512
|
+
// Reset styles for columns and rows 4 and 5.
|
|
513
|
+
// And verify that the affected rows are re-rendered with new styles.
|
|
514
|
+
itemRenderer.mockClear();
|
|
515
|
+
rendered.getInstance().resetAfterIndices({ columnIndex: 3, rowIndex: 3 });
|
|
516
|
+
expect(itemRenderer).toHaveBeenCalledTimes(25);
|
|
517
|
+
expect(findItemRendererCall(3, 3).style.height).toBe(35);
|
|
518
|
+
expect(findItemRendererCall(3, 3).style.width).toBe(75);
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
describe('props validation', () => {
|
|
523
|
+
beforeEach(() => spyOn(console, 'error'));
|
|
524
|
+
|
|
525
|
+
it('should fail if non-function columnWidth is provided', () => {
|
|
526
|
+
expect(() => ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} columnWidth={123} />)).toThrow(
|
|
527
|
+
'An invalid "columnWidth" prop has been specified. ' + 'Value should be a function. "number" was specified.',
|
|
528
|
+
);
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it('should fail if non-function rowHeight is provided', () => {
|
|
532
|
+
expect(() => ReactTestRenderer.create(<VariableSizeGrid {...defaultProps} rowHeight={123} />)).toThrow(
|
|
533
|
+
'An invalid "rowHeight" prop has been specified. ' + 'Value should be a function. "number" was specified.',
|
|
534
|
+
);
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
describe('onScroll', () => {
|
|
539
|
+
it('scrolling should report partial items correctly in onItemsRendered', () => {
|
|
540
|
+
// Use ReactDOM renderer so the container ref works correctly.
|
|
541
|
+
const instance = render(
|
|
542
|
+
<VariableSizeGrid {...defaultProps} columnCount={100} columnWidth={() => 100} initialScrollLeft={20} initialScrollTop={10} rowCount={100} rowHeight={() => 25} />,
|
|
543
|
+
document.createElement('div'),
|
|
544
|
+
);
|
|
545
|
+
// grid 200w x 100h
|
|
546
|
+
// columnWidth: 100, rowHeight: 25,
|
|
547
|
+
// columnCount: 100, rowCount: 100
|
|
548
|
+
// Scroll 2 items fwd, but thanks to the initialScrollOffset, we should
|
|
549
|
+
// still be showing partials on both ends.
|
|
550
|
+
instance.scrollTo({ scrollLeft: 150, scrollTop: 40 });
|
|
551
|
+
// Scroll a little fwd to cause partials to be hidden
|
|
552
|
+
instance.scrollTo({ scrollLeft: 200, scrollTop: 50 });
|
|
553
|
+
// Scroll backwards to show partials again
|
|
554
|
+
instance.scrollTo({ scrollLeft: 150, scrollTop: 40 });
|
|
555
|
+
// Scroll near the end so that the last item is shown
|
|
556
|
+
// as a partial.
|
|
557
|
+
instance.scrollTo({
|
|
558
|
+
scrollLeft: 98 * 100 - 5,
|
|
559
|
+
scrollTop: 96 * 25 - 5,
|
|
560
|
+
});
|
|
561
|
+
// Scroll to the end. No partials.
|
|
562
|
+
instance.scrollTo({
|
|
563
|
+
scrollLeft: 98 * 100,
|
|
564
|
+
scrollTop: 96 * 25,
|
|
565
|
+
});
|
|
566
|
+
// Verify that backwards scrolling near the end works OK.
|
|
567
|
+
instance.scrollTo({
|
|
568
|
+
scrollLeft: 98 * 100 - 5,
|
|
569
|
+
scrollTop: 96 * 25 - 5,
|
|
570
|
+
});
|
|
571
|
+
expect(onItemsRendered.mock.calls).toMatchSnapshot();
|
|
572
|
+
});
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
// https://github.com/bvaughn/react-window/pull/138
|
|
576
|
+
it('should descrease scroll size when itemCount decreases', () => {
|
|
577
|
+
const innerRef = createRef();
|
|
578
|
+
const gridRef = createRef();
|
|
579
|
+
|
|
580
|
+
class Wrapper extends PureComponent {
|
|
581
|
+
state = { columnCount: 100, rowCount: 200 };
|
|
582
|
+
render() {
|
|
583
|
+
return <VariableSizeGrid {...defaultProps} columnCount={this.state.columnCount} innerRef={innerRef} ref={gridRef} rowCount={this.state.rowCount} />;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// Use ReactDOM renderer so "scroll" events work correctly.
|
|
588
|
+
const instance = render(<Wrapper />, document.createElement('div'));
|
|
589
|
+
|
|
590
|
+
// Simulate scrolling past several rows.
|
|
591
|
+
simulateScroll(gridRef.current, { scrollLeft: 3000, scrollTop: 4000 });
|
|
592
|
+
|
|
593
|
+
// Decrease itemCount a lot and verify the scroll height is descreased as well.
|
|
594
|
+
instance.setState({ columnCount: 2, rowCount: 4 });
|
|
595
|
+
expect(innerRef.current.style.height).toEqual('106px');
|
|
596
|
+
expect(innerRef.current.style.width).toEqual('101px');
|
|
597
|
+
});
|
|
598
|
+
});
|