@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
258 lines • 11.5 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
// *****************************************************************************
// Copyright (C) 2026 Safi Seid-Ahmad, K2view and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { enableJSDOM } from '../test/jsdom.js';
// The widget module transitively imports `@lumino/widgets`, which touches `document` at load time.
let disableJSDOM = enableJSDOM();
import { expect } from 'chai';
import { flushSync } from 'react-dom';
import { createRoot } from 'react-dom/client';
import { CompositeTreeNode, TreeNode } from './tree.js';
import { defaultTreeProps, TreeWidget } from './tree-widget.js';
disableJSDOM();
class CountingView extends TreeWidget.View {
constructor() {
super(...arguments);
this.scrollCount = 0;
this.scrollIntoViewIfNeeded = () => {
if (this.list && this.props.scrollToRow !== undefined) {
this.scrollCount++;
}
};
}
/** Reports the list height the way Virtuoso does once it has measured the list. */
measure(height, renderedHeight = height) {
this.setRenderedHeight(renderedHeight);
this.onTotalListHeightChanged(height);
}
setRenderedHeight(scrollHeight) {
this.scroller = { scrollHeight };
}
}
class TestTreeWidget extends TreeWidget {
constructor() {
super(...arguments);
this.scrollToSelectedCount = 0;
}
update() { }
scrollToSelected() {
this.scrollToSelectedCount++;
}
get RowRenderer() {
return this.ScrollingRowRenderer;
}
get currentScrollToRow() {
return this.scrollToRow;
}
get currentScrollToRowRequestId() {
return this.scrollToRowRequestId;
}
requestScrollToFocus() {
this.updateScrollToRow();
}
doUpdateRowsNow() {
this.doUpdateRows();
}
roundTripThroughStorage(node) {
return this.inflateFromStorage(JSON.parse(JSON.stringify(this.deflateForStorage(node))));
}
}
function createTreeRoot(...children) {
const root = { id: 'root', name: 'root', parent: undefined, visible: false, children: [] };
return CompositeTreeNode.addChildren(root, children);
}
function leaf(id) {
return { id, name: id, parent: undefined, visible: true };
}
describe('TreeWidget', () => {
before(() => disableJSDOM = enableJSDOM());
after(() => disableJSDOM());
describe('scroll requests', () => {
let widget;
let model;
beforeEach(() => {
model = { root: undefined };
widget = new TestTreeWidget(defaultTreeProps, model, {});
Object.assign(widget, {
focusService: { get focusedNode() { return widget.focusedNode; } },
scheduleUpdateScrollToRow: () => widget.requestScrollToFocus()
});
});
afterEach(() => widget.dispose());
it('scrolls to the focused row when a scroll is requested', () => {
const target = leaf('target');
model.root = createTreeRoot(leaf('a'), leaf('b'), target);
widget.doUpdateRowsNow();
widget.focusedNode = target;
const requestId = widget.currentScrollToRowRequestId;
widget.requestScrollToFocus();
expect(widget.currentScrollToRow).to.equal(2);
expect(widget.currentScrollToRowRequestId).to.not.equal(requestId);
});
it('does not scroll again when the rows update after the request was served', () => {
const target = leaf('target');
model.root = createTreeRoot(leaf('a'), target);
widget.doUpdateRowsNow();
widget.focusedNode = target;
widget.requestScrollToFocus();
const requestId = widget.currentScrollToRowRequestId;
// A file created above the focused node shifts its row.
model.root = createTreeRoot(leaf('a'), leaf('new'), target);
widget.doUpdateRowsNow();
expect(widget.currentScrollToRow).to.equal(1);
expect(widget.currentScrollToRowRequestId).to.equal(requestId);
});
it('keeps a request pending until the rows contain the focused node', () => {
const target = leaf('target');
const folder = { id: 'folder', name: 'folder', parent: undefined, visible: true, expanded: false, children: [] };
CompositeTreeNode.addChild(folder, target);
model.root = createTreeRoot(leaf('a'), folder);
widget.doUpdateRowsNow();
widget.focusedNode = target;
widget.requestScrollToFocus();
expect(widget.currentScrollToRow).to.be.undefined;
const requestId = widget.currentScrollToRowRequestId;
// The reveal expands the collapsed parent.
folder.expanded = true;
widget.doUpdateRowsNow();
expect(widget.currentScrollToRow).to.equal(2);
expect(widget.currentScrollToRowRequestId).to.not.equal(requestId);
// Once served, a later rows update does not scroll again.
const servedRequestId = widget.currentScrollToRowRequestId;
widget.doUpdateRowsNow();
expect(widget.currentScrollToRowRequestId).to.equal(servedRequestId);
});
});
describe('non-virtualized rows', () => {
let container;
let root;
let widget;
const render = (scrollToRowRequestId) => flushSync(() => root.render(_jsx(widget.RowRenderer, { rows: [], scrollToRowRequestId: scrollToRowRequestId })));
beforeEach(() => {
widget = new TestTreeWidget({ ...defaultTreeProps, virtualized: false }, { root: undefined }, {});
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
});
afterEach(() => {
flushSync(() => root.unmount());
document.body.removeChild(container);
widget.dispose();
});
it('scrolls to the selection once per scroll request, not on every render', () => {
render(1);
const scrollsAfterMount = widget.scrollToSelectedCount;
render(1);
render(1);
expect(widget.scrollToSelectedCount).to.equal(scrollsAfterMount);
render(2);
expect(widget.scrollToSelectedCount).to.equal(scrollsAfterMount + 1);
});
});
describe('storage', () => {
it('restores the root and its children as tree nodes', () => {
const widget = new TestTreeWidget(defaultTreeProps, {}, {});
try {
const restored = widget.roundTripThroughStorage(createTreeRoot(leaf('a')));
expect(TreeNode.is(restored)).to.be.true;
expect(restored.parent).to.be.undefined;
expect(TreeNode.is(restored.children[0])).to.be.true;
expect(restored.children[0].parent).to.equal(restored);
}
finally {
widget.dispose();
}
});
});
describe('View', () => {
let container;
let root;
let view;
let frames;
let requestAnimationFrame;
const rows = [0, 1, 2].map(index => ({ index, depth: 0, node: leaf(`node-${index}`) }));
const render = (props) => flushSync(() => root.render(_jsx(CountingView, { ref: instance => { view = instance ?? undefined; }, width: 100, height: 100, rows: rows, renderNodeRow: row => _jsx("div", { children: row.node.id }), ...props })));
const runFrames = () => frames.splice(0).forEach(frame => frame(0));
/** Mount and measure the list, and serve the mount-time request. */
const mount = (props) => {
render(props);
view.measure(300);
runFrames();
};
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
frames = [];
requestAnimationFrame = window.requestAnimationFrame;
window.requestAnimationFrame = frame => frames.push(frame);
});
afterEach(() => {
window.requestAnimationFrame = requestAnimationFrame;
flushSync(() => root.unmount());
document.body.removeChild(container);
});
it('holds a scroll request made before the list is measured until the list is rendered at its height', () => {
render({ scrollToRow: 2, scrollToRowRequestId: 1 });
expect(view.scrollCount).to.equal(0);
view.measure(300, 100);
runFrames();
expect(view.scrollCount).to.equal(0);
view.setRenderedHeight(300);
runFrames();
expect(view.scrollCount).to.equal(1);
runFrames();
expect(view.scrollCount).to.equal(1);
});
it('waits for the latest list height when the first reported height shrinks', () => {
render({ scrollToRow: 2, scrollToRowRequestId: 1 });
view.measure(300, 100);
view.measure(200, 200);
runFrames();
expect(view.scrollCount).to.equal(1);
});
it('serves a held scroll request when the list is never rendered at its height', () => {
render({ scrollToRow: 2, scrollToRowRequestId: 1 });
view.measure(300, 100);
for (let frame = 0; frame < 30; frame++) {
runFrames();
expect(view.scrollCount).to.equal(0);
}
runFrames();
expect(view.scrollCount).to.equal(1);
});
it('does not scroll again when re-rendered without a new scroll request', () => {
mount({ scrollToRow: 2, scrollToRowRequestId: 1 });
const scrollsAfterMount = view.scrollCount;
render({ scrollToRow: 2, scrollToRowRequestId: 1 });
render({ scrollToRow: 2, scrollToRowRequestId: 1 });
expect(view.scrollCount).to.equal(scrollsAfterMount);
});
it('scrolls again when a new scroll request arrives for the same row', () => {
mount({ scrollToRow: 2, scrollToRowRequestId: 1 });
const scrollsAfterMount = view.scrollCount;
render({ scrollToRow: 2, scrollToRowRequestId: 2 });
expect(view.scrollCount).to.equal(scrollsAfterMount + 1);
});
it('scrolls when the row to scroll to changes', () => {
mount({ scrollToRow: 1 });
const scrollsAfterMount = view.scrollCount;
render({ scrollToRow: 2 });
expect(view.scrollCount).to.equal(scrollsAfterMount + 1);
});
});
});
//# sourceMappingURL=tree-widget.spec.js.map