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