Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookCellLayoutManager.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as assert from 'assert';
6
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
7
import { ICellViewModel } from '../../browser/notebookBrowser.js';
8
import { NotebookCellLayoutManager } from '../../browser/notebookCellLayoutManager.js';
9
10
suite('NotebookCellLayoutManager', () => {
11
12
const store = ensureNoDisposablesAreLeakedInTestSuite();
13
14
const mockCellViewModel = () => {
15
return { handle: 'cell1' } as unknown as ICellViewModel;
16
};
17
18
class MockList {
19
private _height = new Map();
20
getViewIndex(cell: ICellViewModel) { return this.cells.indexOf(cell) < 0 ? undefined : this.cells.indexOf(cell); }
21
elementHeight(cell: ICellViewModel) { return this._height.get(cell) ?? 100; }
22
inRenderingTransaction = false;
23
updateElementHeight2(cell: ICellViewModel, height: number) { this._height.set(cell, height); }
24
getViewIndexCalled = false;
25
cells: ICellViewModel[] = [];
26
}
27
class MockLoggingService { debug() { } }
28
class MockNotebookWidget {
29
viewModel = { hasCell: (cell: ICellViewModel) => true, getCellIndex: () => 0 };
30
hasEditorFocus() { return true; }
31
getAbsoluteTopOfElement() { return 0; }
32
getLength() { return 1; }
33
visibleRanges = [{ start: 0 }];
34
getDomNode(): HTMLElement {
35
return {
36
style: {
37
height: '100px'
38
}
39
} as HTMLElement;
40
}
41
}
42
43
test('should update cell height', async () => {
44
const cell = mockCellViewModel();
45
const cell2 = mockCellViewModel();
46
const list = new MockList();
47
list.cells.push(cell);
48
list.cells.push(cell2);
49
const widget = new MockNotebookWidget();
50
const mgr = store.add(new NotebookCellLayoutManager(widget as any, list as any, new MockLoggingService() as any));
51
mgr.layoutNotebookCell(cell, 200);
52
mgr.layoutNotebookCell(cell2, 200);
53
assert.strictEqual(list.elementHeight(cell), 200);
54
assert.strictEqual(list.elementHeight(cell2), 200);
55
});
56
57
test('should schedule updates if already in a rendering transaction', async () => {
58
const cell = mockCellViewModel();
59
const cell2 = mockCellViewModel();
60
const list = new MockList();
61
list.inRenderingTransaction = true;
62
list.cells.push(cell);
63
list.cells.push(cell2);
64
const widget = new MockNotebookWidget();
65
const mgr = store.add(new NotebookCellLayoutManager(widget as any, list as any, new MockLoggingService() as any));
66
67
const promise = mgr.layoutNotebookCell(cell, 200);
68
mgr.layoutNotebookCell(cell2, 200);
69
assert.strictEqual(list.elementHeight(cell), 100);
70
assert.strictEqual(list.elementHeight(cell2), 100);
71
list.inRenderingTransaction = false;
72
73
await promise;
74
75
assert.strictEqual(list.elementHeight(cell), 200);
76
assert.strictEqual(list.elementHeight(cell2), 200);
77
});
78
79
test('should not update if cell is hidden', async () => {
80
const cell = mockCellViewModel();
81
const list = new MockList();
82
const widget = new MockNotebookWidget();
83
const mgr = store.add(new NotebookCellLayoutManager(widget as any, list as any, new MockLoggingService() as any));
84
await mgr.layoutNotebookCell(cell, 200);
85
assert.strictEqual(list.elementHeight(cell), 100);
86
});
87
88
test('should not update if height is unchanged', async () => {
89
const cell = mockCellViewModel();
90
const list = new MockList();
91
list.cells.push(cell);
92
const widget = new MockNotebookWidget();
93
const mgr = store.add(new NotebookCellLayoutManager(widget as any, list as any, new MockLoggingService() as any));
94
await mgr.layoutNotebookCell(cell, 100);
95
assert.strictEqual(list.elementHeight(cell), 100);
96
});
97
});
98
99