notebookViewModel.test.ts 3.5 KB
Newer Older
R
rebornix 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
R
rebornix 已提交
10
import { NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
11
import { CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
12
import { TestNotebook, withTestNotebook, TestCell } from 'vs/workbench/contrib/notebook/test/testNotebookEditor';
R
rebornix 已提交
13 14
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
R
rebornix 已提交
15 16 17

suite('NotebookViewModel', () => {
	const instantiationService = new TestInstantiationService();
R
rebornix 已提交
18 19
	const blukEditService = instantiationService.get(IBulkEditService);
	const undoRedoService = instantiationService.get(IUndoRedoService);
R
rebornix 已提交
20 21

	test('ctor', function () {
22
		const notebook = new TestNotebook(0, 'notebook', URI.parse('test'));
R
rebornix 已提交
23
		const model = new NotebookEditorModel(notebook);
R
rebornix 已提交
24
		const viewModel = new NotebookViewModel('notebook', model, instantiationService, blukEditService, undoRedoService);
R
rebornix 已提交
25 26 27 28
		assert.equal(viewModel.viewType, 'notebook');
	});

	test('insert/delete', function () {
29 30
		withTestNotebook(
			instantiationService,
R
rebornix 已提交
31 32
			blukEditService,
			undoRedoService,
R
rebornix 已提交
33
			[
R
rebornix 已提交
34 35
				[['var a = 1;'], 'javascript', CellKind.Code, []],
				[['var b = 2;'], 'javascript', CellKind.Code, []]
R
rebornix 已提交
36
			],
37
			(editor, viewModel) => {
R
rebornix 已提交
38
				const cell = viewModel.insertCell(1, new TestCell(viewModel.viewType, 0, ['var c = 3;'], 'javascript', CellKind.Code, []), true);
R
rebornix 已提交
39 40 41 42
				assert.equal(viewModel.viewCells.length, 3);
				assert.equal(viewModel.notebookDocument.cells.length, 3);
				assert.equal(viewModel.getViewCellIndex(cell), 1);

R
rebornix 已提交
43
				viewModel.deleteCell(1, true);
R
rebornix 已提交
44 45 46 47 48 49 50 51
				assert.equal(viewModel.viewCells.length, 2);
				assert.equal(viewModel.notebookDocument.cells.length, 2);
				assert.equal(viewModel.getViewCellIndex(cell), -1);
			}
		);
	});

	test('index', function () {
52 53
		withTestNotebook(
			instantiationService,
R
rebornix 已提交
54 55
			blukEditService,
			undoRedoService,
R
rebornix 已提交
56
			[
R
rebornix 已提交
57 58
				[['var a = 1;'], 'javascript', CellKind.Code, []],
				[['var b = 2;'], 'javascript', CellKind.Code, []]
R
rebornix 已提交
59
			],
60
			(editor, viewModel) => {
R
rebornix 已提交
61 62 63 64
				const firstViewCell = viewModel.viewCells[0];
				const lastViewCell = viewModel.viewCells[viewModel.viewCells.length - 1];

				const insertIndex = viewModel.getViewCellIndex(firstViewCell) + 1;
R
rebornix 已提交
65
				const cell = viewModel.insertCell(insertIndex, new TestCell(viewModel.viewType, 3, ['var c = 3;'], 'javascript', CellKind.Code, []), true);
R
rebornix 已提交
66 67

				const addedCellIndex = viewModel.getViewCellIndex(cell);
R
rebornix 已提交
68
				viewModel.deleteCell(addedCellIndex, true);
R
rebornix 已提交
69 70

				const secondInsertIndex = viewModel.getViewCellIndex(lastViewCell) + 1;
R
rebornix 已提交
71
				const cell2 = viewModel.insertCell(secondInsertIndex, new TestCell(viewModel.viewType, 4, ['var d = 4;'], 'javascript', CellKind.Code, []), true);
R
rebornix 已提交
72 73 74 75 76 77 78 79

				assert.equal(viewModel.viewCells.length, 3);
				assert.equal(viewModel.notebookDocument.cells.length, 3);
				assert.equal(viewModel.getViewCellIndex(cell2), 2);
			}
		);
	});
});