forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnativeEditorTestHelpers.tsx
More file actions
154 lines (139 loc) · 5.3 KB
/
nativeEditorTestHelpers.tsx
File metadata and controls
154 lines (139 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
import { ReactWrapper } from 'enzyme';
import * as React from 'react';
import { Uri } from 'vscode';
import { InteractiveWindowMessages } from '../../client/datascience/interactive-common/interactiveWindowTypes';
import { IJupyterExecution, INotebookEditor, INotebookEditorProvider } from '../../client/datascience/types';
import { CursorPos } from '../../datascience-ui/interactive-common/mainState';
import { NativeCell } from '../../datascience-ui/native-editor/nativeCell';
import { ImageButton } from '../../datascience-ui/react-common/imageButton';
import { DataScienceIocContainer } from './dataScienceIocContainer';
import {
addMockData,
getCellResults,
getNativeFocusedEditor,
injectCode,
mountWebView,
simulateKey,
waitForMessage
} from './testHelpers';
// tslint:disable: no-any
async function getOrCreateNativeEditor(ioc: DataScienceIocContainer, uri?: Uri): Promise<INotebookEditor> {
const notebookEditorProvider = ioc.get<INotebookEditorProvider>(INotebookEditorProvider);
let editor: INotebookEditor | undefined;
const messageWaiter = waitForMessage(ioc, InteractiveWindowMessages.LoadAllCellsComplete);
if (uri) {
editor = await notebookEditorProvider.open(uri);
} else {
editor = await notebookEditorProvider.createNew();
}
if (editor) {
await messageWaiter;
}
return editor;
}
export async function createNewEditor(ioc: DataScienceIocContainer): Promise<INotebookEditor> {
return getOrCreateNativeEditor(ioc);
}
export async function openEditor(
ioc: DataScienceIocContainer,
contents: string,
filePath: string = '/usr/home/test.ipynb'
): Promise<INotebookEditor> {
const uri = Uri.file(filePath);
ioc.setFileContents(uri, contents);
return getOrCreateNativeEditor(ioc, uri);
}
// tslint:disable-next-line: no-any
export function getNativeCellResults(
ioc: DataScienceIocContainer,
wrapper: ReactWrapper<any, Readonly<{}>, React.Component>,
updater: () => Promise<void>,
renderPromiseGenerator?: () => Promise<void>
): Promise<ReactWrapper<any, Readonly<{}>, React.Component>> {
return getCellResults(ioc, wrapper, 'NativeCell', updater, renderPromiseGenerator);
}
// tslint:disable-next-line:no-any
export function runMountedTest(
name: string,
testFunc: (wrapper: ReactWrapper<any, Readonly<{}>, React.Component>, context: Mocha.Context) => Promise<void>,
getIOC: () => DataScienceIocContainer
) {
test(name, async function () {
const ioc = getIOC();
const wrapper = await setupWebview(ioc);
if (wrapper) {
// tslint:disable-next-line: no-invalid-this
await testFunc(wrapper, this);
} else {
// tslint:disable-next-line:no-console
console.log(`${name} skipped, no Jupyter installed.`);
}
});
}
export function mountNativeWebView(ioc: DataScienceIocContainer): ReactWrapper<any, Readonly<{}>, React.Component> {
return mountWebView(ioc, 'native');
}
export async function setupWebview(ioc: DataScienceIocContainer) {
const jupyterExecution = ioc.get<IJupyterExecution>(IJupyterExecution);
if (await jupyterExecution.isNotebookSupported()) {
addMockData(ioc, 'a=1\na', 1);
return mountNativeWebView(ioc);
}
}
export function focusCell(
ioc: DataScienceIocContainer,
wrapper: ReactWrapper<any, Readonly<{}>, React.Component>,
index: number
): Promise<void> {
const cell = wrapper.find(NativeCell).at(index);
if (cell) {
const vm = cell.props().cellVM;
if (!vm.focused) {
const focusChange = waitForMessage(ioc, InteractiveWindowMessages.FocusedCellEditor);
cell.props().focusCell(vm.cell.id, CursorPos.Current);
return focusChange;
}
}
return Promise.resolve();
}
// tslint:disable-next-line: no-any
export async function addCell(
wrapper: ReactWrapper<any, Readonly<{}>, React.Component>,
ioc: DataScienceIocContainer,
code: string,
submit: boolean = true
): Promise<void> {
// First get the main toolbar. We'll use this to add a cell.
const toolbar = wrapper.find('#main-panel-toolbar');
assert.ok(toolbar, 'Cannot find the main panel toolbar during adding a cell');
const ImageButtons = toolbar.find(ImageButton);
assert.equal(ImageButtons.length, 10, 'Toolbar buttons not found');
const addButton = ImageButtons.at(5);
let update = waitForMessage(ioc, InteractiveWindowMessages.FocusedCellEditor);
addButton.simulate('click');
await update;
let textArea: HTMLTextAreaElement | null;
if (code) {
// Type in the code
const editorEnzyme = getNativeFocusedEditor(wrapper);
textArea = injectCode(editorEnzyme, code);
}
if (submit) {
// Then run the cell (use ctrl+enter so we don't add another cell)
update = waitForMessage(ioc, InteractiveWindowMessages.ExecutionRendered);
simulateKey(textArea!, 'Enter', false, true);
return update;
}
}
export function closeNotebook(
editor: INotebookEditor,
wrapper: ReactWrapper<any, Readonly<{}>, React.Component>
): Promise<void> {
const promise = editor.dispose();
wrapper.unmount();
return promise;
}