This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
default-toolbar.spec.ts
255 lines (225 loc) · 8.71 KB
/
default-toolbar.spec.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { simulate } from 'simulate-event';
import { PromiseDelegate } from '@phosphor/coreutils';
import { Widget } from '@phosphor/widgets';
import { Context } from '@jupyterlab/docregistry';
import { CodeCell, MarkdownCell } from '@jupyterlab/cells';
import {
INotebookModel,
NotebookActions,
NotebookPanel,
ToolbarItems
} from '@jupyterlab/notebook';
import {
createNotebookContext,
signalToPromise,
sleep,
NBTestUtils,
framePromise
} from '@jupyterlab/testutils';
const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
describe('@jupyterlab/notebook', () => {
describe('ToolbarItems', () => {
let context: Context<INotebookModel>;
let panel: NotebookPanel;
beforeEach(async () => {
context = await createNotebookContext();
await context.initialize(true);
panel = NBTestUtils.createNotebookPanel(context);
context.model.fromJSON(NBTestUtils.DEFAULT_CONTENT);
});
afterEach(async () => {
await context.session.shutdown();
context.dispose();
panel.dispose();
});
describe('#createSaveButton()', () => {
it('should save when clicked', async () => {
const button = ToolbarItems.createSaveButton(panel);
Widget.attach(button, document.body);
let promise = signalToPromise(context.fileChanged);
await framePromise();
simulate(button.node.firstChild as HTMLElement, 'mousedown');
await promise;
button.dispose();
});
it("should have the `'jp-SaveIcon'` class", async () => {
const button = ToolbarItems.createSaveButton(panel);
Widget.attach(button, document.body);
await framePromise();
expect(button.node.querySelector('.jp-SaveIcon')).to.exist;
});
});
describe('#createInsertButton()', () => {
it('should insert below when clicked', async () => {
const button = ToolbarItems.createInsertButton(panel);
Widget.attach(button, document.body);
await framePromise();
simulate(button.node.firstChild as HTMLElement, 'mousedown');
expect(panel.content.activeCellIndex).to.equal(1);
expect(panel.content.activeCell).to.be.an.instanceof(CodeCell);
button.dispose();
});
it("should have the `'jp-AddIcon'` class", async () => {
const button = ToolbarItems.createInsertButton(panel);
Widget.attach(button, document.body);
await framePromise();
expect(button.node.querySelector('.jp-AddIcon')).to.exist;
});
});
describe('#createCutButton()', () => {
it('should cut when clicked', async () => {
const button = ToolbarItems.createCutButton(panel);
const count = panel.content.widgets.length;
Widget.attach(button, document.body);
await framePromise();
simulate(button.node.firstChild as HTMLElement, 'mousedown');
expect(panel.content.widgets.length).to.equal(count - 1);
expect(NBTestUtils.clipboard.hasData(JUPYTER_CELL_MIME)).to.equal(true);
button.dispose();
});
it("should have the `'jp-CutIcon'` class", async () => {
const button = ToolbarItems.createCutButton(panel);
Widget.attach(button, document.body);
await framePromise();
expect(button.node.querySelector('.jp-CutIcon')).to.exist;
});
});
describe('#createCopyButton()', () => {
it('should copy when clicked', async () => {
const button = ToolbarItems.createCopyButton(panel);
const count = panel.content.widgets.length;
Widget.attach(button, document.body);
await framePromise();
simulate(button.node.firstChild as HTMLElement, 'mousedown');
expect(panel.content.widgets.length).to.equal(count);
expect(NBTestUtils.clipboard.hasData(JUPYTER_CELL_MIME)).to.equal(true);
button.dispose();
});
it("should have the `'jp-CopyIcon'` class", async () => {
const button = ToolbarItems.createCopyButton(panel);
Widget.attach(button, document.body);
await framePromise();
expect(button.node.querySelector('.jp-CopyIcon')).to.exist;
});
});
describe('#createPasteButton()', () => {
it('should paste when clicked', async () => {
const button = ToolbarItems.createPasteButton(panel);
const count = panel.content.widgets.length;
Widget.attach(button, document.body);
await framePromise();
NotebookActions.copy(panel.content);
simulate(button.node.firstChild as HTMLElement, 'mousedown');
await sleep();
expect(panel.content.widgets.length).to.equal(count + 1);
button.dispose();
});
it("should have the `'jp-PasteIcon'` class", async () => {
const button = ToolbarItems.createPasteButton(panel);
Widget.attach(button, document.body);
await framePromise();
expect(button.node.querySelector('.jp-PasteIcon')).to.exist;
});
});
describe('#createRunButton()', () => {
it('should run and advance when clicked', async () => {
const button = ToolbarItems.createRunButton(panel);
const widget = panel.content;
// Clear and select the first two cells.
const codeCell = widget.widgets[0] as CodeCell;
codeCell.model.outputs.clear();
widget.select(codeCell);
const mdCell = widget.widgets[1] as MarkdownCell;
mdCell.rendered = false;
widget.select(mdCell);
Widget.attach(button, document.body);
await context.ready;
await context.session.ready;
await context.session.kernel.ready;
const p = new PromiseDelegate();
context.session.statusChanged.connect((sender, status) => {
// Find the right status idle message
if (status === 'idle' && codeCell.model.outputs.length > 0) {
expect(mdCell.rendered).to.equal(true);
expect(widget.activeCellIndex).to.equal(2);
button.dispose();
p.resolve(0);
}
});
await framePromise();
simulate(button.node.firstChild as HTMLElement, 'mousedown');
await p.promise;
}).timeout(30000); // Allow for slower CI
it("should have the `'jp-RunIcon'` class", async () => {
const button = ToolbarItems.createRunButton(panel);
Widget.attach(button, document.body);
await framePromise();
expect(button.node.querySelector('.jp-RunIcon')).to.exist;
});
});
describe('#createCellTypeItem()', () => {
it('should track the cell type of the current cell', async () => {
const item = ToolbarItems.createCellTypeItem(panel);
Widget.attach(item, document.body);
await framePromise();
const node = item.node.getElementsByTagName(
'select'
)[0] as HTMLSelectElement;
expect(node.value).to.equal('code');
panel.content.activeCellIndex++;
await framePromise();
expect(node.value).to.equal('markdown');
});
it("should display `'-'` if multiple cell types are selected", async () => {
const item = ToolbarItems.createCellTypeItem(panel);
Widget.attach(item, document.body);
await framePromise();
const node = item.node.getElementsByTagName(
'select'
)[0] as HTMLSelectElement;
expect(node.value).to.equal('code');
panel.content.select(panel.content.widgets[1]);
await framePromise();
expect(node.value).to.equal('-');
});
it('should display the active cell type if multiple cells of the same type are selected', async () => {
const item = ToolbarItems.createCellTypeItem(panel);
Widget.attach(item, document.body);
await framePromise();
const node = item.node.getElementsByTagName(
'select'
)[0] as HTMLSelectElement;
expect(node.value).to.equal('code');
const cell = panel.model.contentFactory.createCodeCell({});
panel.model.cells.insert(1, cell);
panel.content.select(panel.content.widgets[1]);
await framePromise();
expect(node.value).to.equal('code');
});
});
describe('#getDefaultItems()', () => {
it('should return the default items of the panel toolbar', () => {
const names = ToolbarItems.getDefaultItems(panel).map(
item => item.name
);
expect(names).to.deep.equal([
'save',
'insert',
'cut',
'copy',
'paste',
'run',
'interrupt',
'restart',
'cellType',
'spacer',
'kernelName',
'kernelStatus'
]);
});
});
});
});