extract createTestNotebookEditor to testing outside of with-util

This commit is contained in:
Johannes Rieken 2021-03-31 16:49:21 +02:00
parent 1372233695
commit e715199ccc
No known key found for this signature in database
GPG key ID: 96634B5AF12F8798
2 changed files with 19 additions and 4 deletions

View file

@ -367,6 +367,7 @@ export interface INotebookEditor extends ICommonNotebookEditor {
readonly onDidChangeKernel: Event<void>;
readonly onDidChangeActiveCell: Event<void>;
isDisposed: boolean;
dispose(): void;
getId(): string;
getDomNode(): HTMLElement;

View file

@ -135,8 +135,8 @@ export function setupInstantiationService() {
return instantiationService;
}
export async function withTestNotebook<R = any>(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][], callback: (editor: IActiveNotebookEditor, accessor: TestInstantiationService) => Promise<R> | R): Promise<R> {
const instantiationService = setupInstantiationService();
function _createTestNotebookEditor(instantiationService: TestInstantiationService, cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][]): IActiveNotebookEditor {
const viewType = 'notebook';
const notebook = instantiationService.createInstance(NotebookTextModel, viewType, URI.parse('test'), cells.map(cell => {
return {
@ -157,6 +157,9 @@ export async function withTestNotebook<R = any>(cells: [source: string, lang: st
const listViewInfoAccessor = new ListViewInfoAccessor(cellList);
const notebookEditor: IActiveNotebookEditor = new class extends mock<IActiveNotebookEditor>() {
dispose() {
viewModel.dispose();
}
onDidChangeModel: Event<NotebookTextModel | undefined> = new Emitter<NotebookTextModel | undefined>().event;
get viewModel() { return viewModel; }
hasModel(): this is IActiveNotebookEditor {
@ -184,11 +187,22 @@ export async function withTestNotebook<R = any>(cells: [source: string, lang: st
}
};
return notebookEditor;
}
export function createTestNotebookEditor(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][]): IActiveNotebookEditor {
return _createTestNotebookEditor(setupInstantiationService(), cells);
}
export async function withTestNotebook<R = any>(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][], callback: (editor: IActiveNotebookEditor, accessor: TestInstantiationService) => Promise<R> | R): Promise<R> {
const instantiationService = setupInstantiationService();
const notebookEditor = _createTestNotebookEditor(instantiationService, cells);
const res = await callback(notebookEditor, instantiationService);
if (res instanceof Promise) {
res.finally(() => viewModel.dispose());
res.finally(() => notebookEditor.dispose());
} else {
viewModel.dispose();
notebookEditor.dispose();
}
return res;
}