/
actions.spec.ts
1474 lines (1297 loc) · 54.3 KB
/
actions.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { ClientSession } from '@jupyterlab/apputils';
import { each } from '@phosphor/algorithm';
import { CodeCell, MarkdownCell, RawCell } from '@jupyterlab/cells';
import { NotebookModel } from '@jupyterlab/notebook';
import { NotebookActions } from '@jupyterlab/notebook';
import { Notebook } from '@jupyterlab/notebook';
import {
acceptDialog,
createClientSession,
dismissDialog,
sleep,
NBTestUtils
} from '@jupyterlab/testutils';
import { JSONObject, JSONArray } from '@phosphor/coreutils';
const ERROR_INPUT = 'a = foo';
const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
describe('@jupyterlab/notebook', () => {
const rendermime = NBTestUtils.defaultRenderMime();
describe('NotebookActions', () => {
let widget: Notebook;
let session: ClientSession;
let ipySession: ClientSession;
before(async () => {
session = await createClientSession();
ipySession = await createClientSession({
kernelPreference: { name: 'ipython' }
});
await Promise.all([ipySession.initialize(), session.initialize()]);
await Promise.all([ipySession.kernel.ready, session.kernel.ready]);
});
beforeEach(() => {
widget = new Notebook({
rendermime,
contentFactory: NBTestUtils.createNotebookFactory(),
mimeTypeService: NBTestUtils.mimeTypeService
});
const model = new NotebookModel();
model.fromJSON(NBTestUtils.DEFAULT_CONTENT);
widget.model = model;
widget.activeCellIndex = 0;
});
afterEach(() => {
widget.dispose();
NBTestUtils.clipboard.clear();
});
after(() => {
return Promise.all([session.shutdown(), ipySession.shutdown()]);
});
describe('#executed', () => {
it('should emit when Markdown and code cells are run', async () => {
const cell = widget.activeCell as CodeCell;
const next = widget.widgets[1] as MarkdownCell;
let emitted = 0;
widget.select(next);
cell.model.outputs.clear();
next.rendered = false;
NotebookActions.executed.connect(() => {
emitted += 1;
});
await NotebookActions.run(widget, session);
expect(emitted).to.equal(2);
expect(next.rendered).to.equal(true);
});
});
describe('#splitCell({})', () => {
it('should split the active cell into two cells', () => {
const cell = widget.activeCell;
const source = 'thisisasamplestringwithnospaces';
cell.model.value.text = source;
const index = widget.activeCellIndex;
const editor = cell.editor;
editor.setCursorPosition(editor.getPositionAt(10));
NotebookActions.splitCell(widget);
const cells = widget.model.cells;
const newSource =
cells.get(index).value.text + cells.get(index + 1).value.text;
expect(newSource).to.equal(source);
});
it('should preserve leading white space in the second cell', () => {
const cell = widget.activeCell;
const source = 'this\n\n is a test';
cell.model.value.text = source;
const editor = cell.editor;
editor.setCursorPosition(editor.getPositionAt(4));
NotebookActions.splitCell(widget);
expect(widget.activeCell.model.value.text).to.equal(' is a test');
});
it('should clear the existing selection', () => {
each(widget.widgets, child => {
widget.select(child);
});
NotebookActions.splitCell(widget);
for (let i = 0; i < widget.widgets.length; i++) {
if (i === widget.activeCellIndex) {
continue;
}
expect(widget.isSelected(widget.widgets[i])).to.equal(false);
}
});
it('should activate the second cell', () => {
NotebookActions.splitCell(widget);
expect(widget.activeCellIndex).to.equal(1);
});
it('should preserve the types of each cell', () => {
NotebookActions.changeCellType(widget, 'markdown');
NotebookActions.splitCell(widget);
expect(widget.activeCell).to.be.an.instanceof(MarkdownCell);
const prev = widget.widgets[0];
expect(prev).to.be.an.instanceof(MarkdownCell);
});
it('should create two empty cells if there is no content', () => {
widget.activeCell.model.value.text = '';
NotebookActions.splitCell(widget);
expect(widget.activeCell.model.value.text).to.equal('');
const prev = widget.widgets[0];
expect(prev.model.value.text).to.equal('');
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.splitCell(widget);
expect(widget.activeCell).to.be.undefined;
});
it('should preserve the widget mode', () => {
NotebookActions.splitCell(widget);
expect(widget.mode).to.equal('command');
widget.mode = 'edit';
NotebookActions.splitCell(widget);
expect(widget.mode).to.equal('edit');
});
it('should be undo-able', () => {
const source = widget.activeCell.model.value.text;
const count = widget.widgets.length;
NotebookActions.splitCell(widget);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
const cell = widget.widgets[0];
expect(cell.model.value.text).to.equal(source);
});
});
describe('#mergeCells', () => {
it('should merge the selected cells', () => {
let source = widget.activeCell.model.value.text + '\n\n';
let next = widget.widgets[1];
widget.select(next);
source += next.model.value.text + '\n\n';
next = widget.widgets[2];
widget.select(next);
source += next.model.value.text;
const count = widget.widgets.length;
NotebookActions.mergeCells(widget);
expect(widget.widgets.length).to.equal(count - 2);
expect(widget.activeCell.model.value.text).to.equal(source);
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.mergeCells(widget);
expect(widget.activeCell).to.be.undefined;
});
it('should select the next cell if there is only one cell selected', () => {
let source = widget.activeCell.model.value.text + '\n\n';
const next = widget.widgets[1];
source += next.model.value.text;
NotebookActions.mergeCells(widget);
expect(widget.activeCell.model.value.text).to.equal(source);
});
it('should clear the outputs of a code cell', () => {
NotebookActions.mergeCells(widget);
const cell = widget.activeCell as CodeCell;
expect(cell.model.outputs.length).to.equal(0);
});
it('should preserve the widget mode', () => {
widget.mode = 'edit';
NotebookActions.mergeCells(widget);
expect(widget.mode).to.equal('edit');
widget.mode = 'command';
NotebookActions.mergeCells(widget);
expect(widget.mode).to.equal('command');
});
it('should be undo-able', () => {
const source = widget.activeCell.model.value.text;
const count = widget.widgets.length;
NotebookActions.mergeCells(widget);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
const cell = widget.widgets[0];
expect(cell.model.value.text).to.equal(source);
});
it('should unrender a markdown cell', () => {
NotebookActions.changeCellType(widget, 'markdown');
let cell = widget.activeCell as MarkdownCell;
cell.rendered = true;
NotebookActions.mergeCells(widget);
cell = widget.activeCell as MarkdownCell;
expect(cell.rendered).to.equal(false);
expect(widget.mode).to.equal('command');
});
it('should preserve the cell type of the active cell', () => {
NotebookActions.changeCellType(widget, 'raw');
NotebookActions.mergeCells(widget);
expect(widget.activeCell).to.be.an.instanceof(RawCell);
expect(widget.mode).to.equal('command');
});
});
describe('#deleteCells()', () => {
it('should delete the selected cells', () => {
const next = widget.widgets[1];
widget.select(next);
const count = widget.widgets.length;
NotebookActions.deleteCells(widget);
expect(widget.widgets.length).to.equal(count - 2);
});
it('should increment deletedCells model when cells deleted', () => {
let next = widget.widgets[1];
widget.select(next);
let count = widget.model.deletedCells.length;
NotebookActions.deleteCells(widget);
expect(widget.model.deletedCells.length).to.equal(count + 2);
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.deleteCells(widget);
expect(widget.activeCell).to.be.undefined;
});
it('should switch to command mode', () => {
widget.mode = 'edit';
NotebookActions.deleteCells(widget);
expect(widget.mode).to.equal('command');
});
it('should activate the cell after the last selected cell', () => {
widget.activeCellIndex = 4;
const prev = widget.widgets[2];
widget.select(prev);
NotebookActions.deleteCells(widget);
expect(widget.activeCellIndex).to.equal(3);
});
it('should select the previous cell if the last cell is deleted', () => {
widget.select(widget.widgets[widget.widgets.length - 1]);
NotebookActions.deleteCells(widget);
expect(widget.activeCellIndex).to.equal(widget.widgets.length - 1);
});
it('should add a code cell if all cells are deleted', async () => {
for (let i = 0; i < widget.widgets.length; i++) {
widget.select(widget.widgets[i]);
}
NotebookActions.deleteCells(widget);
await sleep();
expect(widget.widgets.length).to.equal(1);
expect(widget.activeCell).to.be.an.instanceof(CodeCell);
});
it('should be undo-able', () => {
const next = widget.widgets[1];
widget.select(next);
const source = widget.activeCell.model.value.text;
const count = widget.widgets.length;
NotebookActions.deleteCells(widget);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
const cell = widget.widgets[0];
expect(cell.model.value.text).to.equal(source);
});
it('should be undo-able if all the cells are deleted', () => {
for (let i = 0; i < widget.widgets.length; i++) {
widget.select(widget.widgets[i]);
}
const count = widget.widgets.length;
const source = widget.widgets[1].model.value.text;
NotebookActions.deleteCells(widget);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
expect(widget.widgets[1].model.value.text).to.equal(source);
});
});
describe('#insertAbove()', () => {
it('should insert a code cell above the active cell', () => {
const count = widget.widgets.length;
NotebookActions.insertAbove(widget);
expect(widget.activeCellIndex).to.equal(0);
expect(widget.widgets.length).to.equal(count + 1);
expect(widget.activeCell).to.be.an.instanceof(CodeCell);
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.insertAbove(widget);
expect(widget.activeCell).to.be.undefined;
});
it('should widget mode should be preserved', () => {
NotebookActions.insertAbove(widget);
expect(widget.mode).to.equal('command');
widget.mode = 'edit';
NotebookActions.insertAbove(widget);
expect(widget.mode).to.equal('edit');
});
it('should be undo-able', () => {
const count = widget.widgets.length;
NotebookActions.insertAbove(widget);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
});
it('should clear the existing selection', () => {
for (let i = 0; i < widget.widgets.length; i++) {
widget.select(widget.widgets[i]);
}
NotebookActions.insertAbove(widget);
for (let i = 0; i < widget.widgets.length; i++) {
if (i === widget.activeCellIndex) {
continue;
}
expect(widget.isSelected(widget.widgets[i])).to.equal(false);
}
});
it('should be the new active cell', () => {
NotebookActions.insertAbove(widget);
expect(widget.activeCell.model.value.text).to.equal('');
});
});
describe('#insertBelow()', () => {
it('should insert a code cell below the active cell', () => {
const count = widget.widgets.length;
NotebookActions.insertBelow(widget);
expect(widget.activeCellIndex).to.equal(1);
expect(widget.widgets.length).to.equal(count + 1);
expect(widget.activeCell).to.be.an.instanceof(CodeCell);
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.insertBelow(widget);
expect(widget.activeCell).to.be.undefined;
});
it('should widget mode should be preserved', () => {
NotebookActions.insertBelow(widget);
expect(widget.mode).to.equal('command');
widget.mode = 'edit';
NotebookActions.insertBelow(widget);
expect(widget.mode).to.equal('edit');
});
it('should be undo-able', () => {
const count = widget.widgets.length;
NotebookActions.insertBelow(widget);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
});
it('should clear the existing selection', () => {
for (let i = 0; i < widget.widgets.length; i++) {
widget.select(widget.widgets[i]);
}
NotebookActions.insertBelow(widget);
for (let i = 0; i < widget.widgets.length; i++) {
if (i === widget.activeCellIndex) {
continue;
}
expect(widget.isSelected(widget.widgets[i])).to.equal(false);
}
});
it('should be the new active cell', () => {
NotebookActions.insertBelow(widget);
expect(widget.activeCell.model.value.text).to.equal('');
});
});
describe('#changeCellType()', () => {
it('should change the selected cell type(s)', () => {
let next = widget.widgets[1];
widget.select(next);
NotebookActions.changeCellType(widget, 'raw');
expect(widget.activeCell).to.be.an.instanceof(RawCell);
next = widget.widgets[widget.activeCellIndex + 1];
expect(next).to.be.an.instanceof(RawCell);
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.changeCellType(widget, 'code');
expect(widget.activeCell).to.be.undefined;
});
it('should preserve the widget mode', () => {
NotebookActions.changeCellType(widget, 'code');
expect(widget.mode).to.equal('command');
widget.mode = 'edit';
NotebookActions.changeCellType(widget, 'raw');
expect(widget.mode).to.equal('edit');
});
it('should be undo-able', () => {
NotebookActions.changeCellType(widget, 'raw');
NotebookActions.undo(widget);
const cell = widget.widgets[0];
expect(cell).to.be.an.instanceof(CodeCell);
});
it('should clear the existing selection', () => {
for (let i = 0; i < widget.widgets.length; i++) {
widget.select(widget.widgets[i]);
}
NotebookActions.changeCellType(widget, 'raw');
for (let i = 0; i < widget.widgets.length; i++) {
if (i === widget.activeCellIndex) {
continue;
}
expect(widget.isSelected(widget.widgets[i])).to.equal(false);
}
});
it('should unrender markdown cells', () => {
NotebookActions.changeCellType(widget, 'markdown');
const cell = widget.activeCell as MarkdownCell;
expect(cell.rendered).to.equal(false);
});
});
describe('#run()', () => {
it('should run the selected cells', async () => {
const next = widget.widgets[1] as MarkdownCell;
widget.select(next);
const cell = widget.activeCell as CodeCell;
cell.model.outputs.clear();
next.rendered = false;
const result = await NotebookActions.run(widget, session);
expect(result).to.equal(true);
expect(cell.model.outputs.length).to.be.above(0);
expect(next.rendered).to.equal(true);
});
it('should delete deletedCells metadata when cell run', () => {
let cell = widget.activeCell as CodeCell;
cell.model.outputs.clear();
return NotebookActions.run(widget, session).then(result => {
expect(result).to.equal(true);
expect(widget.model.deletedCells.length).to.equal(0);
});
});
it('should be a no-op if there is no model', async () => {
widget.model = null;
const result = await NotebookActions.run(widget, session);
expect(result).to.equal(false);
});
it('should activate the last selected cell', async () => {
const other = widget.widgets[2];
widget.select(other);
other.model.value.text = 'a = 1';
const result = await NotebookActions.run(widget, session);
expect(result).to.equal(true);
expect(widget.activeCell).to.equal(other);
});
it('should clear the selection', async () => {
const next = widget.widgets[1];
widget.select(next);
const result = await NotebookActions.run(widget, session);
expect(result).to.equal(true);
expect(widget.isSelected(widget.widgets[0])).to.equal(false);
});
it('should change to command mode', async () => {
widget.mode = 'edit';
const result = await NotebookActions.run(widget, session);
expect(result).to.equal(true);
expect(widget.mode).to.equal('command');
});
it('should handle no session', async () => {
const result = await NotebookActions.run(widget, null);
expect(result).to.equal(true);
const cell = widget.activeCell as CodeCell;
expect(cell.model.executionCount).to.be.null;
});
it('should stop executing code cells on an error', async () => {
let cell = widget.model.contentFactory.createCodeCell({});
cell.value.text = ERROR_INPUT;
widget.model.cells.insert(2, cell);
widget.select(widget.widgets[2]);
cell = widget.model.contentFactory.createCodeCell({});
widget.model.cells.push(cell);
widget.select(widget.widgets[widget.widgets.length - 1]);
const result = await NotebookActions.run(widget, ipySession);
expect(result).to.equal(false);
expect(cell.executionCount).to.be.null;
await ipySession.kernel.restart();
}).timeout(30000); // Allow for slower CI
it('should render all markdown cells on an error', async () => {
const cell = widget.model.contentFactory.createMarkdownCell({});
widget.model.cells.push(cell);
const child = widget.widgets[widget.widgets.length - 1] as MarkdownCell;
child.rendered = false;
widget.select(child);
widget.activeCell.model.value.text = ERROR_INPUT;
const result = await NotebookActions.run(widget, ipySession);
// Markdown rendering is asynchronous, but the cell
// provides no way to hook into that. Sleep here
// to make sure it finishes.
await sleep(100);
expect(result).to.equal(false);
expect(child.rendered).to.equal(true);
await ipySession.kernel.restart();
}).timeout(60000); // Allow for slower CI
});
describe('#runAndAdvance()', () => {
it('should run the selected cells ', async () => {
const next = widget.widgets[1] as MarkdownCell;
widget.select(next);
const cell = widget.activeCell as CodeCell;
cell.model.outputs.clear();
next.rendered = false;
const result = await NotebookActions.runAndAdvance(widget, session);
expect(result).to.equal(true);
expect(cell.model.outputs.length).to.be.above(0);
expect(next.rendered).to.equal(true);
}).timeout(30000); // Allow for slower CI
it('should be a no-op if there is no model', async () => {
widget.model = null;
const result = await NotebookActions.runAndAdvance(widget, session);
expect(result).to.equal(false);
});
it('should clear the existing selection', async () => {
const next = widget.widgets[2];
widget.select(next);
const result = await NotebookActions.runAndAdvance(widget, ipySession);
expect(result).to.equal(false);
expect(widget.isSelected(widget.widgets[0])).to.equal(false);
await ipySession.kernel.restart();
}).timeout(30000); // Allow for slower CI
it('should change to command mode', async () => {
widget.mode = 'edit';
const result = await NotebookActions.runAndAdvance(widget, session);
expect(result).to.equal(true);
expect(widget.mode).to.equal('command');
});
it('should activate the cell after the last selected cell', async () => {
const next = widget.widgets[3] as MarkdownCell;
widget.select(next);
const result = await NotebookActions.runAndAdvance(widget, session);
expect(result).to.equal(true);
expect(widget.activeCellIndex).to.equal(4);
});
it('should create a new code cell in edit mode if necessary', async () => {
const count = widget.widgets.length;
widget.activeCellIndex = count - 1;
const result = await NotebookActions.runAndAdvance(widget, session);
expect(result).to.equal(true);
expect(widget.widgets.length).to.equal(count + 1);
expect(widget.activeCell).to.be.an.instanceof(CodeCell);
expect(widget.mode).to.equal('edit');
});
it('should allow an undo of the new cell', async () => {
const count = widget.widgets.length;
widget.activeCellIndex = count - 1;
const result = await NotebookActions.runAndAdvance(widget, session);
expect(result).to.equal(true);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
});
it('should stop executing code cells on an error', async () => {
widget.activeCell.model.value.text = ERROR_INPUT;
const cell = widget.model.contentFactory.createCodeCell({});
widget.model.cells.push(cell);
widget.select(widget.widgets[widget.widgets.length - 1]);
const result = await NotebookActions.runAndAdvance(widget, ipySession);
expect(result).to.equal(false);
expect(cell.executionCount).to.be.null;
await ipySession.kernel.restart();
}).timeout(30000); // Allow for slower CI
it('should render all markdown cells on an error', async () => {
widget.activeCell.model.value.text = ERROR_INPUT;
const cell = widget.widgets[1] as MarkdownCell;
cell.rendered = false;
widget.select(cell);
const result = await NotebookActions.runAndAdvance(widget, ipySession);
// Markdown rendering is asynchronous, but the cell
// provides no way to hook into that. Sleep here
// to make sure it finishes.
await sleep(100);
expect(result).to.equal(false);
expect(cell.rendered).to.equal(true);
expect(widget.activeCellIndex).to.equal(2);
await ipySession.kernel.restart();
}).timeout(60000); // Allow for slower CI
});
describe('#runAndInsert()', () => {
it('should run the selected cells ', async () => {
const next = widget.widgets[1] as MarkdownCell;
widget.select(next);
const cell = widget.activeCell as CodeCell;
cell.model.outputs.clear();
next.rendered = false;
const result = await NotebookActions.runAndInsert(widget, session);
expect(result).to.equal(true);
expect(cell.model.outputs.length).to.be.above(0);
expect(next.rendered).to.equal(true);
});
it('should be a no-op if there is no model', async () => {
widget.model = null;
const result = await NotebookActions.runAndInsert(widget, session);
expect(result).to.equal(false);
});
it('should clear the existing selection', async () => {
const next = widget.widgets[1];
widget.select(next);
const result = await NotebookActions.runAndInsert(widget, session);
expect(result).to.equal(true);
expect(widget.isSelected(widget.widgets[0])).to.equal(false);
});
it('should insert a new code cell in edit mode after the last selected cell', async () => {
const next = widget.widgets[2];
widget.select(next);
next.model.value.text = 'a = 1';
const count = widget.widgets.length;
const result = await NotebookActions.runAndInsert(widget, session);
expect(result).to.equal(true);
expect(widget.activeCell).to.be.an.instanceof(CodeCell);
expect(widget.mode).to.equal('edit');
expect(widget.widgets.length).to.equal(count + 1);
});
it('should allow an undo of the cell insert', async () => {
const next = widget.widgets[2];
widget.select(next);
next.model.value.text = 'a = 1';
const count = widget.widgets.length;
const result = await NotebookActions.runAndInsert(widget, session);
expect(result).to.equal(true);
NotebookActions.undo(widget);
expect(widget.widgets.length).to.equal(count);
});
it('should stop executing code cells on an error', async () => {
widget.activeCell.model.value.text = ERROR_INPUT;
const cell = widget.model.contentFactory.createCodeCell({});
widget.model.cells.push(cell);
widget.select(widget.widgets[widget.widgets.length - 1]);
const result = await NotebookActions.runAndInsert(widget, ipySession);
expect(result).to.equal(false);
expect(cell.executionCount).to.be.null;
await ipySession.kernel.restart();
}).timeout(30000); // Allow for slower CI
it('should render all markdown cells on an error', async () => {
widget.activeCell.model.value.text = ERROR_INPUT;
const cell = widget.widgets[1] as MarkdownCell;
cell.rendered = false;
widget.select(cell);
const result = await NotebookActions.runAndInsert(widget, ipySession);
// Markdown rendering is asynchronous, but the cell
// provides no way to hook into that. Sleep here
// to make sure it finishes.
await sleep(100);
expect(result).to.equal(false);
expect(cell.rendered).to.equal(true);
expect(widget.activeCellIndex).to.equal(2);
await ipySession.kernel.restart();
}).timeout(60000); // Allow for slower CI
});
describe('#runAll()', () => {
beforeEach(() => {
// Make sure all cells have valid code.
widget.widgets[2].model.value.text = 'a = 1';
});
it('should run all of the cells in the notebok', async () => {
const next = widget.widgets[1] as MarkdownCell;
const cell = widget.activeCell as CodeCell;
cell.model.outputs.clear();
next.rendered = false;
const result = await NotebookActions.runAll(widget, session);
expect(result).to.equal(true);
expect(cell.model.outputs.length).to.be.above(0);
expect(next.rendered).to.equal(true);
}).timeout(30000); // Allow for slower CI
it('should be a no-op if there is no model', async () => {
widget.model = null;
const result = await NotebookActions.runAll(widget, session);
expect(result).to.equal(false);
});
it('should change to command mode', async () => {
widget.mode = 'edit';
const result = await NotebookActions.runAll(widget, session);
expect(result).to.equal(true);
expect(widget.mode).to.equal('command');
}).timeout(30000); // Allow for slower CI
it('should clear the existing selection', async () => {
const next = widget.widgets[2];
widget.select(next);
const result = await NotebookActions.runAll(widget, session);
expect(result).to.equal(true);
expect(widget.isSelected(widget.widgets[2])).to.equal(false);
}).timeout(30000); // Allow for slower CI
it('should activate the last cell', async () => {
await NotebookActions.runAll(widget, session);
expect(widget.activeCellIndex).to.equal(widget.widgets.length - 1);
}).timeout(30000); // Allow for slower CI
it('should stop executing code cells on an error', async () => {
widget.activeCell.model.value.text = ERROR_INPUT;
const cell = widget.model.contentFactory.createCodeCell({});
widget.model.cells.push(cell);
const result = await NotebookActions.runAll(widget, ipySession);
expect(result).to.equal(false);
expect(cell.executionCount).to.be.null;
expect(widget.activeCellIndex).to.equal(widget.widgets.length - 1);
await ipySession.kernel.restart();
}).timeout(30000); // Allow for slower CI
it('should render all markdown cells on an error', async () => {
widget.activeCell.model.value.text = ERROR_INPUT;
const cell = widget.widgets[1] as MarkdownCell;
cell.rendered = false;
const result = await NotebookActions.runAll(widget, ipySession);
// Markdown rendering is asynchronous, but the cell
// provides no way to hook into that. Sleep here
// to make sure it finishes.
await sleep(100);
expect(result).to.equal(false);
expect(cell.rendered).to.equal(true);
await ipySession.kernel.restart();
}).timeout(60000); // Allow for slower CI
});
describe('#selectAbove()', () => {
it('should select the cell above the active cell', () => {
widget.activeCellIndex = 1;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(0);
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(-1);
});
it('should not wrap around to the bottom', () => {
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(0);
});
it('should preserve the mode', () => {
widget.activeCellIndex = 2;
NotebookActions.selectAbove(widget);
expect(widget.mode).to.equal('command');
widget.mode = 'edit';
NotebookActions.selectAbove(widget);
expect(widget.mode).to.equal('edit');
});
it('should skip collapsed cells in edit mode', () => {
widget.activeCellIndex = 3;
widget.mode = 'edit';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(0);
});
it('should not change if in edit mode and no non-collapsed cells above', () => {
widget.activeCellIndex = 1;
widget.mode = 'edit';
widget.widgets[0].inputHidden = true;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(1);
});
it('should not skip collapsed cells and in command mode', () => {
widget.activeCellIndex = 3;
widget.mode = 'command';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(2);
});
});
describe('#selectBelow()', () => {
it('should select the cell below the active cell', () => {
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(1);
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(-1);
});
it('should not wrap around to the top', () => {
widget.activeCellIndex = widget.widgets.length - 1;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.not.equal(0);
});
it('should preserve the mode', () => {
widget.activeCellIndex = 2;
NotebookActions.selectBelow(widget);
expect(widget.mode).to.equal('command');
widget.mode = 'edit';
NotebookActions.selectBelow(widget);
expect(widget.mode).to.equal('edit');
});
it('should skip collapsed cells in edit mode', () => {
widget.activeCellIndex = 0;
widget.mode = 'edit';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(3);
});
it('should not change if in edit mode and no non-collapsed cells below', () => {
widget.activeCellIndex = widget.widgets.length - 2;
widget.mode = 'edit';
widget.widgets[widget.widgets.length - 1].inputHidden = true;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(widget.widgets.length - 2);
});
it('should not skip collapsed cells and in command mode', () => {
widget.activeCellIndex = 0;
widget.mode = 'command';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(1);
});
});
describe('#extendSelectionAbove()', () => {
it('should extend the selection to the cell above', () => {
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget);
expect(widget.isSelected(widget.widgets[0])).to.equal(true);
});
it('should extend the selection to the topmost cell', () => {
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget, true);
for (let i = widget.activeCellIndex; i >= 0; i--) {
expect(widget.isSelected(widget.widgets[i])).to.equal(true);
}
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.extendSelectionAbove(widget);
expect(widget.activeCellIndex).to.equal(-1);
});
it('should change to command mode if there is a selection', () => {
widget.mode = 'edit';
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget);
expect(widget.mode).to.equal('command');
});
it('should not wrap around to the bottom', () => {
widget.mode = 'edit';
NotebookActions.extendSelectionAbove(widget);
expect(widget.activeCellIndex).to.equal(0);
const last = widget.widgets[widget.widgets.length - 1];
expect(widget.isSelected(last)).to.equal(false);
expect(widget.mode).to.equal('edit');
});
it('should deselect the current cell if the cell above is selected', () => {
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget);
const cell = widget.activeCell;
NotebookActions.extendSelectionAbove(widget);
expect(widget.isSelected(cell)).to.equal(false);
});
it('should select only the first cell if we move from the second to first', () => {
NotebookActions.extendSelectionBelow(widget);
const cell = widget.activeCell;
NotebookActions.extendSelectionAbove(widget);
expect(widget.isSelected(cell)).to.equal(false);
expect(widget.activeCellIndex).to.equal(0);
});
it('should activate the cell', () => {
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget);
expect(widget.activeCellIndex).to.equal(0);
});
});
describe('#extendSelectionBelow()', () => {
it('should extend the selection to the cell below', () => {
NotebookActions.extendSelectionBelow(widget);
expect(widget.isSelected(widget.widgets[0])).to.equal(true);
expect(widget.isSelected(widget.widgets[1])).to.equal(true);
});
it('should extend the selection the bottomost cell', () => {
NotebookActions.extendSelectionBelow(widget, true);
for (let i = widget.activeCellIndex; i < widget.widgets.length; i++) {
expect(widget.isSelected(widget.widgets[i])).to.equal(true);
}
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.extendSelectionBelow(widget);
expect(widget.activeCellIndex).to.equal(-1);
});
it('should change to command mode if there is a selection', () => {
widget.mode = 'edit';
NotebookActions.extendSelectionBelow(widget);
expect(widget.mode).to.equal('command');
});
it('should not wrap around to the top', () => {
const last = widget.widgets.length - 1;
widget.activeCellIndex = last;
widget.mode = 'edit';