actions.spec.ts 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { ISessionContext, SessionContext } from '@jupyterlab/apputils';
  5. import { each } from '@lumino/algorithm';
  6. import { CodeCell, MarkdownCell, RawCell } from '@jupyterlab/cells';
  7. import { NotebookModel } from '../src';
  8. import { NotebookActions } from '../src';
  9. import { Notebook } from '../src';
  10. import {
  11. acceptDialog,
  12. createSessionContext,
  13. dismissDialog,
  14. sleep
  15. } from '@jupyterlab/testutils';
  16. import { JSONObject, JSONArray } from '@lumino/coreutils';
  17. import { JupyterServer } from '@jupyterlab/testutils/lib/start_jupyter_server';
  18. import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
  19. import * as utils from './utils';
  20. const ERROR_INPUT = 'a = foo';
  21. const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
  22. const server = new JupyterServer();
  23. beforeAll(async () => {
  24. jest.setTimeout(20000);
  25. await server.start();
  26. });
  27. afterAll(async () => {
  28. await server.shutdown();
  29. });
  30. describe('@jupyterlab/notebook', () => {
  31. let rendermime: IRenderMimeRegistry;
  32. describe('NotebookActions', () => {
  33. let widget: Notebook;
  34. let sessionContext: ISessionContext;
  35. let ipySessionContext: ISessionContext;
  36. beforeAll(async function() {
  37. jest.setTimeout(20000);
  38. rendermime = utils.defaultRenderMime();
  39. async function createContext(options?: Partial<SessionContext.IOptions>) {
  40. const context = await createSessionContext(options);
  41. await context.initialize();
  42. await context.session?.kernel?.info;
  43. return context;
  44. }
  45. [sessionContext, ipySessionContext] = await Promise.all([
  46. createContext(),
  47. createContext({ kernelPreference: { name: 'ipython' } })
  48. ]);
  49. });
  50. beforeEach(() => {
  51. widget = new Notebook({
  52. rendermime,
  53. contentFactory: utils.createNotebookFactory(),
  54. mimeTypeService: utils.mimeTypeService
  55. });
  56. const model = new NotebookModel();
  57. model.fromJSON(utils.DEFAULT_CONTENT);
  58. widget.model = model;
  59. widget.activeCellIndex = 0;
  60. });
  61. afterEach(() => {
  62. widget.dispose();
  63. utils.clipboard.clear();
  64. });
  65. afterAll(async () => {
  66. await Promise.all([
  67. sessionContext.shutdown(),
  68. ipySessionContext.shutdown()
  69. ]);
  70. });
  71. describe('#executed', () => {
  72. it('should emit when Markdown and code cells are run', async () => {
  73. const cell = widget.activeCell as CodeCell;
  74. const next = widget.widgets[1] as MarkdownCell;
  75. let emitted = 0;
  76. widget.select(next);
  77. cell.model.outputs.clear();
  78. next.rendered = false;
  79. NotebookActions.executed.connect(() => {
  80. emitted += 1;
  81. });
  82. await NotebookActions.run(widget, sessionContext);
  83. expect(emitted).toBe(2);
  84. expect(next.rendered).toBe(true);
  85. });
  86. });
  87. describe('#splitCell({})', () => {
  88. it('should split the active cell into two cells', () => {
  89. const cell = widget.activeCell!;
  90. const source = 'thisisasamplestringwithnospaces';
  91. cell.model.value.text = source;
  92. const index = widget.activeCellIndex;
  93. const editor = cell.editor;
  94. editor.setCursorPosition(editor.getPositionAt(10)!);
  95. NotebookActions.splitCell(widget);
  96. const cells = widget.model!.cells;
  97. const newSource =
  98. cells.get(index).value.text + cells.get(index + 1).value.text;
  99. expect(newSource).toBe(source);
  100. });
  101. it('should preserve leading white space in the second cell', () => {
  102. const cell = widget.activeCell!;
  103. const source = 'this\n\n is a test';
  104. cell.model.value.text = source;
  105. const editor = cell.editor;
  106. editor.setCursorPosition(editor.getPositionAt(4)!);
  107. NotebookActions.splitCell(widget);
  108. expect(widget.activeCell!.model.value.text).toBe(' is a test');
  109. });
  110. it('should clear the existing selection', () => {
  111. each(widget.widgets, child => {
  112. widget.select(child);
  113. });
  114. NotebookActions.splitCell(widget);
  115. for (let i = 0; i < widget.widgets.length; i++) {
  116. if (i === widget.activeCellIndex) {
  117. continue;
  118. }
  119. expect(widget.isSelected(widget.widgets[i])).toBe(false);
  120. }
  121. });
  122. it('should activate the second cell', () => {
  123. NotebookActions.splitCell(widget);
  124. expect(widget.activeCellIndex).toBe(1);
  125. });
  126. it('should preserve the types of each cell', () => {
  127. NotebookActions.changeCellType(widget, 'markdown');
  128. NotebookActions.splitCell(widget);
  129. expect(widget.activeCell).toBeInstanceOf(MarkdownCell);
  130. const prev = widget.widgets[0];
  131. expect(prev).toBeInstanceOf(MarkdownCell);
  132. });
  133. it('should create two empty cells if there is no content', () => {
  134. widget.activeCell!.model.value.text = '';
  135. NotebookActions.splitCell(widget);
  136. expect(widget.activeCell!.model.value.text).toBe('');
  137. const prev = widget.widgets[0];
  138. expect(prev.model.value.text).toBe('');
  139. });
  140. it('should be a no-op if there is no model', () => {
  141. widget.model = null;
  142. NotebookActions.splitCell(widget);
  143. expect(widget.activeCell).toBeUndefined();
  144. });
  145. it('should preserve the widget mode', () => {
  146. NotebookActions.splitCell(widget);
  147. expect(widget.mode).toBe('command');
  148. widget.mode = 'edit';
  149. NotebookActions.splitCell(widget);
  150. expect(widget.mode).toBe('edit');
  151. });
  152. it('should be undo-able', () => {
  153. const source = widget.activeCell!.model.value.text;
  154. const count = widget.widgets.length;
  155. NotebookActions.splitCell(widget);
  156. NotebookActions.undo(widget);
  157. expect(widget.widgets.length).toBe(count);
  158. const cell = widget.widgets[0];
  159. expect(cell.model.value.text).toBe(source);
  160. });
  161. });
  162. describe('#mergeCells', () => {
  163. it('should merge the selected cells', () => {
  164. let source = widget.activeCell!.model.value.text + '\n\n';
  165. let next = widget.widgets[1];
  166. widget.select(next);
  167. source += next.model.value.text + '\n\n';
  168. next = widget.widgets[2];
  169. widget.select(next);
  170. source += next.model.value.text;
  171. const count = widget.widgets.length;
  172. NotebookActions.mergeCells(widget);
  173. expect(widget.widgets.length).toBe(count - 2);
  174. expect(widget.activeCell!.model.value.text).toBe(source);
  175. });
  176. it('should be a no-op if there is no model', () => {
  177. widget.model = null;
  178. NotebookActions.mergeCells(widget);
  179. expect(widget.activeCell).toBeUndefined();
  180. });
  181. it('should select the next cell if there is only one cell selected', () => {
  182. let source = widget.activeCell!.model.value.text + '\n\n';
  183. const next = widget.widgets[1];
  184. source += next.model.value.text;
  185. NotebookActions.mergeCells(widget);
  186. expect(widget.activeCell!.model.value.text).toBe(source);
  187. });
  188. it('should clear the outputs of a code cell', () => {
  189. NotebookActions.mergeCells(widget);
  190. const cell = widget.activeCell as CodeCell;
  191. expect(cell.model.outputs.length).toBe(0);
  192. });
  193. it('should preserve the widget mode', () => {
  194. widget.mode = 'edit';
  195. NotebookActions.mergeCells(widget);
  196. expect(widget.mode).toBe('edit');
  197. widget.mode = 'command';
  198. NotebookActions.mergeCells(widget);
  199. expect(widget.mode).toBe('command');
  200. });
  201. it('should be undo-able', () => {
  202. const source = widget.activeCell!.model.value.text;
  203. const count = widget.widgets.length;
  204. NotebookActions.mergeCells(widget);
  205. NotebookActions.undo(widget);
  206. expect(widget.widgets.length).toBe(count);
  207. const cell = widget.widgets[0];
  208. expect(cell.model.value.text).toBe(source);
  209. });
  210. it('should unrender a markdown cell', () => {
  211. NotebookActions.changeCellType(widget, 'markdown');
  212. let cell = widget.activeCell as MarkdownCell;
  213. cell.rendered = true;
  214. NotebookActions.mergeCells(widget);
  215. cell = widget.activeCell as MarkdownCell;
  216. expect(cell.rendered).toBe(false);
  217. expect(widget.mode).toBe('command');
  218. });
  219. it('should preserve the cell type of the active cell', () => {
  220. NotebookActions.changeCellType(widget, 'raw');
  221. NotebookActions.mergeCells(widget);
  222. expect(widget.activeCell).toBeInstanceOf(RawCell);
  223. expect(widget.mode).toBe('command');
  224. });
  225. });
  226. describe('#deleteCells()', () => {
  227. it('should delete the selected cells', () => {
  228. const next = widget.widgets[1];
  229. widget.select(next);
  230. const count = widget.widgets.length;
  231. NotebookActions.deleteCells(widget);
  232. expect(widget.widgets.length).toBe(count - 2);
  233. });
  234. it('should increment deletedCells model when cells deleted', () => {
  235. const next = widget.widgets[1];
  236. widget.select(next);
  237. const count = widget.model!.deletedCells.length;
  238. NotebookActions.deleteCells(widget);
  239. expect(widget.model!.deletedCells.length).toBe(count + 2);
  240. });
  241. it('should be a no-op if there is no model', () => {
  242. widget.model = null;
  243. NotebookActions.deleteCells(widget);
  244. expect(widget.activeCell).toBeUndefined();
  245. });
  246. it('should switch to command mode', () => {
  247. widget.mode = 'edit';
  248. NotebookActions.deleteCells(widget);
  249. expect(widget.mode).toBe('command');
  250. });
  251. it('should activate the cell after the last selected cell', () => {
  252. widget.activeCellIndex = 4;
  253. const prev = widget.widgets[2];
  254. widget.select(prev);
  255. NotebookActions.deleteCells(widget);
  256. expect(widget.activeCellIndex).toBe(3);
  257. });
  258. it('should select the previous cell if the last cell is deleted', () => {
  259. widget.select(widget.widgets[widget.widgets.length - 1]);
  260. NotebookActions.deleteCells(widget);
  261. expect(widget.activeCellIndex).toBe(widget.widgets.length - 1);
  262. });
  263. it('should add a code cell if all cells are deleted', async () => {
  264. for (let i = 0; i < widget.widgets.length; i++) {
  265. widget.select(widget.widgets[i]);
  266. }
  267. NotebookActions.deleteCells(widget);
  268. await sleep();
  269. expect(widget.widgets.length).toBe(1);
  270. expect(widget.activeCell).toBeInstanceOf(CodeCell);
  271. });
  272. it('should be undo-able', () => {
  273. const next = widget.widgets[1];
  274. widget.select(next);
  275. const source = widget.activeCell!.model.value.text;
  276. const count = widget.widgets.length;
  277. NotebookActions.deleteCells(widget);
  278. NotebookActions.undo(widget);
  279. expect(widget.widgets.length).toBe(count);
  280. const cell = widget.widgets[0];
  281. expect(cell.model.value.text).toBe(source);
  282. });
  283. it('should be undo-able if all the cells are deleted', () => {
  284. for (let i = 0; i < widget.widgets.length; i++) {
  285. widget.select(widget.widgets[i]);
  286. }
  287. const count = widget.widgets.length;
  288. const source = widget.widgets[1].model.value.text;
  289. NotebookActions.deleteCells(widget);
  290. NotebookActions.undo(widget);
  291. expect(widget.widgets.length).toBe(count);
  292. expect(widget.widgets[1].model.value.text).toBe(source);
  293. });
  294. });
  295. describe('#insertAbove()', () => {
  296. it('should insert a code cell above the active cell', () => {
  297. const count = widget.widgets.length;
  298. NotebookActions.insertAbove(widget);
  299. expect(widget.activeCellIndex).toBe(0);
  300. expect(widget.widgets.length).toBe(count + 1);
  301. expect(widget.activeCell).toBeInstanceOf(CodeCell);
  302. });
  303. it('should be a no-op if there is no model', () => {
  304. widget.model = null;
  305. NotebookActions.insertAbove(widget);
  306. expect(widget.activeCell).toBeUndefined();
  307. });
  308. it('should widget mode should be preserved', () => {
  309. NotebookActions.insertAbove(widget);
  310. expect(widget.mode).toBe('command');
  311. widget.mode = 'edit';
  312. NotebookActions.insertAbove(widget);
  313. expect(widget.mode).toBe('edit');
  314. });
  315. it('should be undo-able', () => {
  316. const count = widget.widgets.length;
  317. NotebookActions.insertAbove(widget);
  318. NotebookActions.undo(widget);
  319. expect(widget.widgets.length).toBe(count);
  320. });
  321. it('should clear the existing selection', () => {
  322. for (let i = 0; i < widget.widgets.length; i++) {
  323. widget.select(widget.widgets[i]);
  324. }
  325. NotebookActions.insertAbove(widget);
  326. for (let i = 0; i < widget.widgets.length; i++) {
  327. if (i === widget.activeCellIndex) {
  328. continue;
  329. }
  330. expect(widget.isSelected(widget.widgets[i])).toBe(false);
  331. }
  332. });
  333. it('should be the new active cell', () => {
  334. NotebookActions.insertAbove(widget);
  335. expect(widget.activeCell!.model.value.text).toBe('');
  336. });
  337. });
  338. describe('#insertBelow()', () => {
  339. it('should insert a code cell below the active cell', () => {
  340. const count = widget.widgets.length;
  341. NotebookActions.insertBelow(widget);
  342. expect(widget.activeCellIndex).toBe(1);
  343. expect(widget.widgets.length).toBe(count + 1);
  344. expect(widget.activeCell).toBeInstanceOf(CodeCell);
  345. });
  346. it('should be a no-op if there is no model', () => {
  347. widget.model = null;
  348. NotebookActions.insertBelow(widget);
  349. expect(widget.activeCell).toBeUndefined();
  350. });
  351. it('should widget mode should be preserved', () => {
  352. NotebookActions.insertBelow(widget);
  353. expect(widget.mode).toBe('command');
  354. widget.mode = 'edit';
  355. NotebookActions.insertBelow(widget);
  356. expect(widget.mode).toBe('edit');
  357. });
  358. it('should be undo-able', () => {
  359. const count = widget.widgets.length;
  360. NotebookActions.insertBelow(widget);
  361. NotebookActions.undo(widget);
  362. expect(widget.widgets.length).toBe(count);
  363. });
  364. it('should clear the existing selection', () => {
  365. for (let i = 0; i < widget.widgets.length; i++) {
  366. widget.select(widget.widgets[i]);
  367. }
  368. NotebookActions.insertBelow(widget);
  369. for (let i = 0; i < widget.widgets.length; i++) {
  370. if (i === widget.activeCellIndex) {
  371. continue;
  372. }
  373. expect(widget.isSelected(widget.widgets[i])).toBe(false);
  374. }
  375. });
  376. it('should be the new active cell', () => {
  377. NotebookActions.insertBelow(widget);
  378. expect(widget.activeCell!.model.value.text).toBe('');
  379. });
  380. });
  381. describe('#changeCellType()', () => {
  382. it('should change the selected cell type(s)', () => {
  383. let next = widget.widgets[1];
  384. widget.select(next);
  385. NotebookActions.changeCellType(widget, 'raw');
  386. expect(widget.activeCell).toBeInstanceOf(RawCell);
  387. next = widget.widgets[widget.activeCellIndex + 1];
  388. expect(next).toBeInstanceOf(RawCell);
  389. });
  390. it('should be a no-op if there is no model', () => {
  391. widget.model = null;
  392. NotebookActions.changeCellType(widget, 'code');
  393. expect(widget.activeCell).toBeUndefined();
  394. });
  395. it('should preserve the widget mode', () => {
  396. NotebookActions.changeCellType(widget, 'code');
  397. expect(widget.mode).toBe('command');
  398. widget.mode = 'edit';
  399. NotebookActions.changeCellType(widget, 'raw');
  400. expect(widget.mode).toBe('edit');
  401. });
  402. it('should be undo-able', () => {
  403. NotebookActions.changeCellType(widget, 'raw');
  404. NotebookActions.undo(widget);
  405. const cell = widget.widgets[0];
  406. expect(cell).toBeInstanceOf(CodeCell);
  407. });
  408. it('should clear the existing selection', () => {
  409. for (let i = 0; i < widget.widgets.length; i++) {
  410. widget.select(widget.widgets[i]);
  411. }
  412. NotebookActions.changeCellType(widget, 'raw');
  413. for (let i = 0; i < widget.widgets.length; i++) {
  414. if (i === widget.activeCellIndex) {
  415. continue;
  416. }
  417. expect(widget.isSelected(widget.widgets[i])).toBe(false);
  418. }
  419. });
  420. it('should unrender markdown cells', () => {
  421. NotebookActions.changeCellType(widget, 'markdown');
  422. const cell = widget.activeCell as MarkdownCell;
  423. expect(cell.rendered).toBe(false);
  424. });
  425. });
  426. describe('#run()', () => {
  427. it('should run the selected cells', async () => {
  428. const next = widget.widgets[1] as MarkdownCell;
  429. widget.select(next);
  430. const cell = widget.activeCell as CodeCell;
  431. cell.model.outputs.clear();
  432. next.rendered = false;
  433. const result = await NotebookActions.run(widget, sessionContext);
  434. expect(result).toBe(true);
  435. expect(cell.model.outputs.length).toBeGreaterThan(0);
  436. expect(next.rendered).toBe(true);
  437. });
  438. it('should delete deletedCells metadata when cell run', () => {
  439. const cell = widget.activeCell as CodeCell;
  440. cell.model.outputs.clear();
  441. return NotebookActions.run(widget, sessionContext).then(result => {
  442. expect(result).toBe(true);
  443. expect(widget.model!.deletedCells.length).toBe(0);
  444. });
  445. });
  446. it('should be a no-op if there is no model', async () => {
  447. widget.model = null;
  448. const result = await NotebookActions.run(widget, sessionContext);
  449. expect(result).toBe(false);
  450. });
  451. it('should activate the last selected cell', async () => {
  452. const other = widget.widgets[2];
  453. widget.select(other);
  454. other.model.value.text = 'a = 1';
  455. const result = await NotebookActions.run(widget, sessionContext);
  456. expect(result).toBe(true);
  457. expect(widget.activeCell).toBe(other);
  458. });
  459. it('should clear the selection', async () => {
  460. const next = widget.widgets[1];
  461. widget.select(next);
  462. const result = await NotebookActions.run(widget, sessionContext);
  463. expect(result).toBe(true);
  464. expect(widget.isSelected(widget.widgets[0])).toBe(false);
  465. });
  466. it('should change to command mode', async () => {
  467. widget.mode = 'edit';
  468. const result = await NotebookActions.run(widget, sessionContext);
  469. expect(result).toBe(true);
  470. expect(widget.mode).toBe('command');
  471. });
  472. it('should handle no session', async () => {
  473. const result = await NotebookActions.run(widget, undefined);
  474. expect(result).toBe(true);
  475. const cell = widget.activeCell as CodeCell;
  476. expect(cell.model.executionCount).toBeNull();
  477. });
  478. it('should stop executing code cells on an error', async () => {
  479. let cell = widget.model!.contentFactory.createCodeCell({});
  480. cell.value.text = ERROR_INPUT;
  481. widget.model!.cells.insert(2, cell);
  482. widget.select(widget.widgets[2]);
  483. cell = widget.model!.contentFactory.createCodeCell({});
  484. widget.model!.cells.push(cell);
  485. widget.select(widget.widgets[widget.widgets.length - 1]);
  486. const result = await NotebookActions.run(widget, ipySessionContext);
  487. expect(result).toBe(false);
  488. expect(cell.executionCount).toBeNull();
  489. await ipySessionContext.session!.kernel!.restart();
  490. });
  491. it('should render all markdown cells on an error', async () => {
  492. const cell = widget.model!.contentFactory.createMarkdownCell({});
  493. widget.model!.cells.push(cell);
  494. const child = widget.widgets[widget.widgets.length - 1] as MarkdownCell;
  495. child.rendered = false;
  496. widget.select(child);
  497. widget.activeCell!.model.value.text = ERROR_INPUT;
  498. const result = await NotebookActions.run(widget, ipySessionContext);
  499. // Markdown rendering is asynchronous, but the cell
  500. // provides no way to hook into that. Sleep here
  501. // to make sure it finishes.
  502. await sleep(100);
  503. expect(result).toBe(false);
  504. expect(child.rendered).toBe(true);
  505. await ipySessionContext.session!.kernel!.restart();
  506. });
  507. });
  508. describe('#runAndAdvance()', () => {
  509. it('should run the selected cells ', async () => {
  510. const next = widget.widgets[1] as MarkdownCell;
  511. widget.select(next);
  512. const cell = widget.activeCell as CodeCell;
  513. cell.model.outputs.clear();
  514. next.rendered = false;
  515. const result = await NotebookActions.runAndAdvance(
  516. widget,
  517. sessionContext
  518. );
  519. expect(result).toBe(true);
  520. expect(cell.model.outputs.length).toBeGreaterThan(0);
  521. expect(next.rendered).toBe(true);
  522. });
  523. it('should be a no-op if there is no model', async () => {
  524. widget.model = null;
  525. const result = await NotebookActions.runAndAdvance(
  526. widget,
  527. sessionContext
  528. );
  529. expect(result).toBe(false);
  530. });
  531. it('should clear the existing selection', async () => {
  532. const next = widget.widgets[2];
  533. widget.select(next);
  534. const result = await NotebookActions.runAndAdvance(
  535. widget,
  536. ipySessionContext
  537. );
  538. expect(result).toBe(false);
  539. expect(widget.isSelected(widget.widgets[0])).toBe(false);
  540. await ipySessionContext.session!.kernel!.restart();
  541. });
  542. it('should change to command mode', async () => {
  543. widget.mode = 'edit';
  544. const result = await NotebookActions.runAndAdvance(
  545. widget,
  546. sessionContext
  547. );
  548. expect(result).toBe(true);
  549. expect(widget.mode).toBe('command');
  550. });
  551. it('should activate the cell after the last selected cell', async () => {
  552. const next = widget.widgets[3] as MarkdownCell;
  553. widget.select(next);
  554. const result = await NotebookActions.runAndAdvance(
  555. widget,
  556. sessionContext
  557. );
  558. expect(result).toBe(true);
  559. expect(widget.activeCellIndex).toBe(4);
  560. });
  561. it('should create a new code cell in edit mode if necessary', async () => {
  562. const count = widget.widgets.length;
  563. widget.activeCellIndex = count - 1;
  564. const result = await NotebookActions.runAndAdvance(
  565. widget,
  566. sessionContext
  567. );
  568. expect(result).toBe(true);
  569. expect(widget.widgets.length).toBe(count + 1);
  570. expect(widget.activeCell).toBeInstanceOf(CodeCell);
  571. expect(widget.mode).toBe('edit');
  572. });
  573. it('should allow an undo of the new cell', async () => {
  574. const count = widget.widgets.length;
  575. widget.activeCellIndex = count - 1;
  576. const result = await NotebookActions.runAndAdvance(
  577. widget,
  578. sessionContext
  579. );
  580. expect(result).toBe(true);
  581. NotebookActions.undo(widget);
  582. expect(widget.widgets.length).toBe(count);
  583. });
  584. it('should stop executing code cells on an error', async () => {
  585. widget.activeCell!.model.value.text = ERROR_INPUT;
  586. const cell = widget.model!.contentFactory.createCodeCell({});
  587. widget.model!.cells.push(cell);
  588. widget.select(widget.widgets[widget.widgets.length - 1]);
  589. const result = await NotebookActions.runAndAdvance(
  590. widget,
  591. ipySessionContext
  592. );
  593. expect(result).toBe(false);
  594. expect(cell.executionCount).toBeNull();
  595. await ipySessionContext.session!.kernel!.restart();
  596. });
  597. it('should render all markdown cells on an error', async () => {
  598. widget.activeCell!.model.value.text = ERROR_INPUT;
  599. const cell = widget.widgets[1] as MarkdownCell;
  600. cell.rendered = false;
  601. widget.select(cell);
  602. const result = await NotebookActions.runAndAdvance(
  603. widget,
  604. ipySessionContext
  605. );
  606. // Markdown rendering is asynchronous, but the cell
  607. // provides no way to hook into that. Sleep here
  608. // to make sure it finishes.
  609. await sleep(100);
  610. expect(result).toBe(false);
  611. expect(cell.rendered).toBe(true);
  612. expect(widget.activeCellIndex).toBe(2);
  613. await ipySessionContext.session!.kernel!.restart();
  614. });
  615. });
  616. describe('#runAndInsert()', () => {
  617. it('should run the selected cells ', async () => {
  618. const next = widget.widgets[1] as MarkdownCell;
  619. widget.select(next);
  620. const cell = widget.activeCell as CodeCell;
  621. cell.model.outputs.clear();
  622. next.rendered = false;
  623. const result = await NotebookActions.runAndInsert(
  624. widget,
  625. sessionContext
  626. );
  627. expect(result).toBe(true);
  628. expect(cell.model.outputs.length).toBeGreaterThan(0);
  629. expect(next.rendered).toBe(true);
  630. });
  631. it('should be a no-op if there is no model', async () => {
  632. widget.model = null;
  633. const result = await NotebookActions.runAndInsert(
  634. widget,
  635. sessionContext
  636. );
  637. expect(result).toBe(false);
  638. });
  639. it('should clear the existing selection', async () => {
  640. const next = widget.widgets[1];
  641. widget.select(next);
  642. const result = await NotebookActions.runAndInsert(
  643. widget,
  644. sessionContext
  645. );
  646. expect(result).toBe(true);
  647. expect(widget.isSelected(widget.widgets[0])).toBe(false);
  648. });
  649. it('should insert a new code cell in edit mode after the last selected cell', async () => {
  650. const next = widget.widgets[2];
  651. widget.select(next);
  652. next.model.value.text = 'a = 1';
  653. const count = widget.widgets.length;
  654. const result = await NotebookActions.runAndInsert(
  655. widget,
  656. sessionContext
  657. );
  658. expect(result).toBe(true);
  659. expect(widget.activeCell).toBeInstanceOf(CodeCell);
  660. expect(widget.mode).toBe('edit');
  661. expect(widget.widgets.length).toBe(count + 1);
  662. });
  663. it('should allow an undo of the cell insert', async () => {
  664. const next = widget.widgets[2];
  665. widget.select(next);
  666. next.model.value.text = 'a = 1';
  667. const count = widget.widgets.length;
  668. const result = await NotebookActions.runAndInsert(
  669. widget,
  670. sessionContext
  671. );
  672. expect(result).toBe(true);
  673. NotebookActions.undo(widget);
  674. expect(widget.widgets.length).toBe(count);
  675. });
  676. it('should stop executing code cells on an error', async () => {
  677. widget.activeCell!.model.value.text = ERROR_INPUT;
  678. const cell = widget.model!.contentFactory.createCodeCell({});
  679. widget.model!.cells.push(cell);
  680. widget.select(widget.widgets[widget.widgets.length - 1]);
  681. const result = await NotebookActions.runAndInsert(
  682. widget,
  683. ipySessionContext
  684. );
  685. expect(result).toBe(false);
  686. expect(cell.executionCount).toBeNull();
  687. await ipySessionContext.session!.kernel!.restart();
  688. });
  689. it('should render all markdown cells on an error', async () => {
  690. widget.activeCell!.model.value.text = ERROR_INPUT;
  691. const cell = widget.widgets[1] as MarkdownCell;
  692. cell.rendered = false;
  693. widget.select(cell);
  694. const result = await NotebookActions.runAndInsert(
  695. widget,
  696. ipySessionContext
  697. );
  698. // Markdown rendering is asynchronous, but the cell
  699. // provides no way to hook into that. Sleep here
  700. // to make sure it finishes.
  701. await sleep(100);
  702. expect(result).toBe(false);
  703. expect(cell.rendered).toBe(true);
  704. expect(widget.activeCellIndex).toBe(2);
  705. await ipySessionContext.session!.kernel!.restart();
  706. });
  707. });
  708. describe('#runAll()', () => {
  709. beforeEach(() => {
  710. // Make sure all cells have valid code.
  711. widget.widgets[2].model.value.text = 'a = 1';
  712. });
  713. it('should run all of the cells in the notebok', async () => {
  714. const next = widget.widgets[1] as MarkdownCell;
  715. const cell = widget.activeCell as CodeCell;
  716. cell.model.outputs.clear();
  717. next.rendered = false;
  718. const result = await NotebookActions.runAll(widget, sessionContext);
  719. expect(result).toBe(true);
  720. expect(cell.model.outputs.length).toBeGreaterThan(0);
  721. expect(next.rendered).toBe(true);
  722. });
  723. it('should be a no-op if there is no model', async () => {
  724. widget.model = null;
  725. const result = await NotebookActions.runAll(widget, sessionContext);
  726. expect(result).toBe(false);
  727. });
  728. it('should change to command mode', async () => {
  729. widget.mode = 'edit';
  730. const result = await NotebookActions.runAll(widget, sessionContext);
  731. expect(result).toBe(true);
  732. expect(widget.mode).toBe('command');
  733. });
  734. it('should clear the existing selection', async () => {
  735. const next = widget.widgets[2];
  736. widget.select(next);
  737. const result = await NotebookActions.runAll(widget, sessionContext);
  738. expect(result).toBe(true);
  739. expect(widget.isSelected(widget.widgets[2])).toBe(false);
  740. });
  741. it('should activate the last cell', async () => {
  742. await NotebookActions.runAll(widget, sessionContext);
  743. expect(widget.activeCellIndex).toBe(widget.widgets.length - 1);
  744. });
  745. it('should stop executing code cells on an error', async () => {
  746. widget.activeCell!.model.value.text = ERROR_INPUT;
  747. const cell = widget.model!.contentFactory.createCodeCell({});
  748. widget.model!.cells.push(cell);
  749. const result = await NotebookActions.runAll(widget, ipySessionContext);
  750. expect(result).toBe(false);
  751. expect(cell.executionCount).toBeNull();
  752. expect(widget.activeCellIndex).toBe(widget.widgets.length - 1);
  753. await ipySessionContext.session!.kernel!.restart();
  754. });
  755. it('should render all markdown cells on an error', async () => {
  756. widget.activeCell!.model.value.text = ERROR_INPUT;
  757. const cell = widget.widgets[1] as MarkdownCell;
  758. cell.rendered = false;
  759. const result = await NotebookActions.runAll(widget, ipySessionContext);
  760. // Markdown rendering is asynchronous, but the cell
  761. // provides no way to hook into that. Sleep here
  762. // to make sure it finishes.
  763. await sleep(100);
  764. expect(result).toBe(false);
  765. expect(cell.rendered).toBe(true);
  766. await ipySessionContext.session!.kernel!.restart();
  767. });
  768. });
  769. describe('#selectAbove()', () => {
  770. it('should select the cell above the active cell', () => {
  771. widget.activeCellIndex = 1;
  772. NotebookActions.selectAbove(widget);
  773. expect(widget.activeCellIndex).toBe(0);
  774. });
  775. it('should be a no-op if there is no model', () => {
  776. widget.model = null;
  777. NotebookActions.selectAbove(widget);
  778. expect(widget.activeCellIndex).toBe(-1);
  779. });
  780. it('should not wrap around to the bottom', () => {
  781. NotebookActions.selectAbove(widget);
  782. expect(widget.activeCellIndex).toBe(0);
  783. });
  784. it('should preserve the mode', () => {
  785. widget.activeCellIndex = 2;
  786. NotebookActions.selectAbove(widget);
  787. expect(widget.mode).toBe('command');
  788. widget.mode = 'edit';
  789. NotebookActions.selectAbove(widget);
  790. expect(widget.mode).toBe('edit');
  791. });
  792. it('should skip collapsed cells in edit mode', () => {
  793. widget.activeCellIndex = 3;
  794. widget.mode = 'edit';
  795. widget.widgets[1].inputHidden = true;
  796. widget.widgets[2].inputHidden = true;
  797. widget.widgets[3].inputHidden = false;
  798. NotebookActions.selectAbove(widget);
  799. expect(widget.activeCellIndex).toBe(0);
  800. });
  801. it('should not change if in edit mode and no non-collapsed cells above', () => {
  802. widget.activeCellIndex = 1;
  803. widget.mode = 'edit';
  804. widget.widgets[0].inputHidden = true;
  805. NotebookActions.selectAbove(widget);
  806. expect(widget.activeCellIndex).toBe(1);
  807. });
  808. it('should not skip collapsed cells and in command mode', () => {
  809. widget.activeCellIndex = 3;
  810. widget.mode = 'command';
  811. widget.widgets[1].inputHidden = true;
  812. widget.widgets[2].inputHidden = true;
  813. widget.widgets[3].inputHidden = false;
  814. NotebookActions.selectAbove(widget);
  815. expect(widget.activeCellIndex).toBe(2);
  816. });
  817. });
  818. describe('#selectBelow()', () => {
  819. it('should select the cell below the active cell', () => {
  820. NotebookActions.selectBelow(widget);
  821. expect(widget.activeCellIndex).toBe(1);
  822. });
  823. it('should be a no-op if there is no model', () => {
  824. widget.model = null;
  825. NotebookActions.selectBelow(widget);
  826. expect(widget.activeCellIndex).toBe(-1);
  827. });
  828. it('should not wrap around to the top', () => {
  829. widget.activeCellIndex = widget.widgets.length - 1;
  830. NotebookActions.selectBelow(widget);
  831. expect(widget.activeCellIndex).not.toBe(0);
  832. });
  833. it('should preserve the mode', () => {
  834. widget.activeCellIndex = 2;
  835. NotebookActions.selectBelow(widget);
  836. expect(widget.mode).toBe('command');
  837. widget.mode = 'edit';
  838. NotebookActions.selectBelow(widget);
  839. expect(widget.mode).toBe('edit');
  840. });
  841. it('should skip collapsed cells in edit mode', () => {
  842. widget.activeCellIndex = 0;
  843. widget.mode = 'edit';
  844. widget.widgets[1].inputHidden = true;
  845. widget.widgets[2].inputHidden = true;
  846. widget.widgets[3].inputHidden = false;
  847. NotebookActions.selectBelow(widget);
  848. expect(widget.activeCellIndex).toBe(3);
  849. });
  850. it('should not change if in edit mode and no non-collapsed cells below', () => {
  851. widget.activeCellIndex = widget.widgets.length - 2;
  852. widget.mode = 'edit';
  853. widget.widgets[widget.widgets.length - 1].inputHidden = true;
  854. NotebookActions.selectBelow(widget);
  855. expect(widget.activeCellIndex).toBe(widget.widgets.length - 2);
  856. });
  857. it('should not skip collapsed cells and in command mode', () => {
  858. widget.activeCellIndex = 0;
  859. widget.mode = 'command';
  860. widget.widgets[1].inputHidden = true;
  861. widget.widgets[2].inputHidden = true;
  862. widget.widgets[3].inputHidden = false;
  863. NotebookActions.selectBelow(widget);
  864. expect(widget.activeCellIndex).toBe(1);
  865. });
  866. });
  867. describe('#extendSelectionAbove()', () => {
  868. it('should extend the selection to the cell above', () => {
  869. widget.activeCellIndex = 1;
  870. NotebookActions.extendSelectionAbove(widget);
  871. expect(widget.isSelected(widget.widgets[0])).toBe(true);
  872. });
  873. it('should extend the selection to the topmost cell', () => {
  874. widget.activeCellIndex = 1;
  875. NotebookActions.extendSelectionAbove(widget, true);
  876. for (let i = widget.activeCellIndex; i >= 0; i--) {
  877. expect(widget.isSelected(widget.widgets[i])).toBe(true);
  878. }
  879. });
  880. it('should be a no-op if there is no model', () => {
  881. widget.model = null;
  882. NotebookActions.extendSelectionAbove(widget);
  883. expect(widget.activeCellIndex).toBe(-1);
  884. });
  885. it('should change to command mode if there is a selection', () => {
  886. widget.mode = 'edit';
  887. widget.activeCellIndex = 1;
  888. NotebookActions.extendSelectionAbove(widget);
  889. expect(widget.mode).toBe('command');
  890. });
  891. it('should not wrap around to the bottom', () => {
  892. widget.mode = 'edit';
  893. NotebookActions.extendSelectionAbove(widget);
  894. expect(widget.activeCellIndex).toBe(0);
  895. const last = widget.widgets[widget.widgets.length - 1];
  896. expect(widget.isSelected(last)).toBe(false);
  897. expect(widget.mode).toBe('edit');
  898. });
  899. it('should deselect the current cell if the cell above is selected', () => {
  900. NotebookActions.extendSelectionBelow(widget);
  901. NotebookActions.extendSelectionBelow(widget);
  902. const cell = widget.activeCell!;
  903. NotebookActions.extendSelectionAbove(widget);
  904. expect(widget.isSelected(cell)).toBe(false);
  905. });
  906. it('should select only the first cell if we move from the second to first', () => {
  907. NotebookActions.extendSelectionBelow(widget);
  908. const cell = widget.activeCell!;
  909. NotebookActions.extendSelectionAbove(widget);
  910. expect(widget.isSelected(cell)).toBe(false);
  911. expect(widget.activeCellIndex).toBe(0);
  912. });
  913. it('should activate the cell', () => {
  914. widget.activeCellIndex = 1;
  915. NotebookActions.extendSelectionAbove(widget);
  916. expect(widget.activeCellIndex).toBe(0);
  917. });
  918. });
  919. describe('#extendSelectionBelow()', () => {
  920. it('should extend the selection to the cell below', () => {
  921. NotebookActions.extendSelectionBelow(widget);
  922. expect(widget.isSelected(widget.widgets[0])).toBe(true);
  923. expect(widget.isSelected(widget.widgets[1])).toBe(true);
  924. });
  925. it('should extend the selection the bottomost cell', () => {
  926. NotebookActions.extendSelectionBelow(widget, true);
  927. for (let i = widget.activeCellIndex; i < widget.widgets.length; i++) {
  928. expect(widget.isSelected(widget.widgets[i])).toBe(true);
  929. }
  930. });
  931. it('should be a no-op if there is no model', () => {
  932. widget.model = null;
  933. NotebookActions.extendSelectionBelow(widget);
  934. expect(widget.activeCellIndex).toBe(-1);
  935. });
  936. it('should change to command mode if there is a selection', () => {
  937. widget.mode = 'edit';
  938. NotebookActions.extendSelectionBelow(widget);
  939. expect(widget.mode).toBe('command');
  940. });
  941. it('should not wrap around to the top', () => {
  942. const last = widget.widgets.length - 1;
  943. widget.activeCellIndex = last;
  944. widget.mode = 'edit';
  945. NotebookActions.extendSelectionBelow(widget);
  946. expect(widget.activeCellIndex).toBe(last);
  947. expect(widget.isSelected(widget.widgets[0])).toBe(false);
  948. expect(widget.mode).toBe('edit');
  949. });
  950. it('should deselect the current cell if the cell below is selected', () => {
  951. const last = widget.widgets.length - 1;
  952. widget.activeCellIndex = last;
  953. NotebookActions.extendSelectionAbove(widget);
  954. NotebookActions.extendSelectionAbove(widget);
  955. const current = widget.activeCell!;
  956. NotebookActions.extendSelectionBelow(widget);
  957. expect(widget.isSelected(current)).toBe(false);
  958. });
  959. it('should select only the last cell if we move from the second last to last', () => {
  960. const last = widget.widgets.length - 1;
  961. widget.activeCellIndex = last;
  962. NotebookActions.extendSelectionAbove(widget);
  963. const current = widget.activeCell!;
  964. NotebookActions.extendSelectionBelow(widget);
  965. expect(widget.isSelected(current)).toBe(false);
  966. expect(widget.activeCellIndex).toBe(last);
  967. });
  968. it('should activate the cell', () => {
  969. NotebookActions.extendSelectionBelow(widget);
  970. expect(widget.activeCellIndex).toBe(1);
  971. });
  972. });
  973. describe('#moveUp()', () => {
  974. it('should move the selected cells up', () => {
  975. widget.activeCellIndex = 2;
  976. NotebookActions.extendSelectionAbove(widget);
  977. NotebookActions.moveUp(widget);
  978. expect(widget.isSelected(widget.widgets[0])).toBe(true);
  979. expect(widget.isSelected(widget.widgets[1])).toBe(true);
  980. expect(widget.isSelected(widget.widgets[2])).toBe(false);
  981. expect(widget.activeCellIndex).toBe(0);
  982. });
  983. it('should be a no-op if there is no model', () => {
  984. widget.model = null;
  985. NotebookActions.moveUp(widget);
  986. expect(widget.activeCellIndex).toBe(-1);
  987. });
  988. it('should not wrap around to the bottom', () => {
  989. expect(widget.activeCellIndex).toBe(0);
  990. NotebookActions.moveUp(widget);
  991. expect(widget.activeCellIndex).toBe(0);
  992. });
  993. it('should be undo-able', () => {
  994. widget.activeCellIndex++;
  995. const source = widget.activeCell!.model.value.text;
  996. NotebookActions.moveUp(widget);
  997. expect(widget.model!.cells.get(0).value.text).toBe(source);
  998. NotebookActions.undo(widget);
  999. expect(widget.model!.cells.get(1).value.text).toBe(source);
  1000. });
  1001. });
  1002. describe('#moveDown()', () => {
  1003. it('should move the selected cells down', () => {
  1004. NotebookActions.extendSelectionBelow(widget);
  1005. NotebookActions.moveDown(widget);
  1006. expect(widget.isSelected(widget.widgets[0])).toBe(false);
  1007. expect(widget.isSelected(widget.widgets[1])).toBe(true);
  1008. expect(widget.isSelected(widget.widgets[2])).toBe(true);
  1009. expect(widget.activeCellIndex).toBe(2);
  1010. });
  1011. it('should be a no-op if there is no model', () => {
  1012. widget.model = null;
  1013. NotebookActions.moveUp(widget);
  1014. expect(widget.activeCellIndex).toBe(-1);
  1015. });
  1016. it('should not wrap around to the top', () => {
  1017. widget.activeCellIndex = widget.widgets.length - 1;
  1018. NotebookActions.moveDown(widget);
  1019. expect(widget.activeCellIndex).toBe(widget.widgets.length - 1);
  1020. });
  1021. it('should be undo-able', () => {
  1022. const source = widget.activeCell!.model.value.text;
  1023. NotebookActions.moveDown(widget);
  1024. expect(widget.model!.cells.get(1).value.text).toBe(source);
  1025. NotebookActions.undo(widget);
  1026. expect(widget.model!.cells.get(0).value.text).toBe(source);
  1027. });
  1028. });
  1029. describe('#copy()', () => {
  1030. it('should copy the selected cells to a utils.clipboard', () => {
  1031. const next = widget.widgets[1];
  1032. widget.select(next);
  1033. NotebookActions.copy(widget);
  1034. expect(utils.clipboard.hasData(JUPYTER_CELL_MIME)).toBe(true);
  1035. const data = utils.clipboard.getData(JUPYTER_CELL_MIME);
  1036. expect(data.length).toBe(2);
  1037. });
  1038. it('should be a no-op if there is no model', () => {
  1039. widget.model = null;
  1040. NotebookActions.copy(widget);
  1041. expect(utils.clipboard.hasData(JUPYTER_CELL_MIME)).toBe(false);
  1042. });
  1043. it('should change to command mode', () => {
  1044. widget.mode = 'edit';
  1045. NotebookActions.copy(widget);
  1046. expect(widget.mode).toBe('command');
  1047. });
  1048. it('should delete metadata.deletable', () => {
  1049. const next = widget.widgets[1];
  1050. widget.select(next);
  1051. next.model.metadata.set('deletable', false);
  1052. NotebookActions.copy(widget);
  1053. const data = utils.clipboard.getData(JUPYTER_CELL_MIME) as JSONArray;
  1054. data.map(cell => {
  1055. expect(
  1056. ((cell as JSONObject).metadata as JSONObject).deletable
  1057. ).toBeUndefined();
  1058. });
  1059. });
  1060. });
  1061. describe('#cut()', () => {
  1062. it('should cut the selected cells to a utils.clipboard', () => {
  1063. const next = widget.widgets[1];
  1064. widget.select(next);
  1065. const count = widget.widgets.length;
  1066. NotebookActions.cut(widget);
  1067. expect(widget.widgets.length).toBe(count - 2);
  1068. });
  1069. it('should be a no-op if there is no model', () => {
  1070. widget.model = null;
  1071. NotebookActions.cut(widget);
  1072. expect(utils.clipboard.hasData(JUPYTER_CELL_MIME)).toBe(false);
  1073. });
  1074. it('should change to command mode', () => {
  1075. widget.mode = 'edit';
  1076. NotebookActions.cut(widget);
  1077. expect(widget.mode).toBe('command');
  1078. });
  1079. it('should be undo-able', () => {
  1080. const source = widget.activeCell!.model.value.text;
  1081. NotebookActions.cut(widget);
  1082. NotebookActions.undo(widget);
  1083. expect(widget.widgets[0].model.value.text).toBe(source);
  1084. });
  1085. it('should add a new code cell if all cells were cut', async () => {
  1086. for (let i = 0; i < widget.widgets.length; i++) {
  1087. widget.select(widget.widgets[i]);
  1088. }
  1089. NotebookActions.cut(widget);
  1090. await sleep();
  1091. expect(widget.widgets.length).toBe(1);
  1092. expect(widget.activeCell).toBeInstanceOf(CodeCell);
  1093. });
  1094. });
  1095. describe('#paste()', () => {
  1096. it('should paste cells from a utils.clipboard', () => {
  1097. const source = widget.activeCell!.model.value.text;
  1098. const next = widget.widgets[1];
  1099. widget.select(next);
  1100. const count = widget.widgets.length;
  1101. NotebookActions.cut(widget);
  1102. widget.activeCellIndex = 1;
  1103. NotebookActions.paste(widget);
  1104. expect(widget.widgets.length).toBe(count);
  1105. expect(widget.widgets[2].model.value.text).toBe(source);
  1106. expect(widget.activeCellIndex).toBe(3);
  1107. });
  1108. it('should be a no-op if there is no model', () => {
  1109. NotebookActions.copy(widget);
  1110. widget.model = null;
  1111. NotebookActions.paste(widget);
  1112. expect(widget.activeCellIndex).toBe(-1);
  1113. });
  1114. it('should be a no-op if there is no cell data on the utils.clipboard', () => {
  1115. const count = widget.widgets.length;
  1116. NotebookActions.paste(widget);
  1117. expect(widget.widgets.length).toBe(count);
  1118. });
  1119. it('should change to command mode', () => {
  1120. widget.mode = 'edit';
  1121. NotebookActions.cut(widget);
  1122. NotebookActions.paste(widget);
  1123. expect(widget.mode).toBe('command');
  1124. });
  1125. it('should be undo-able', () => {
  1126. const next = widget.widgets[1];
  1127. widget.select(next);
  1128. const count = widget.widgets.length;
  1129. NotebookActions.cut(widget);
  1130. widget.activeCellIndex = 1;
  1131. NotebookActions.paste(widget);
  1132. NotebookActions.undo(widget);
  1133. expect(widget.widgets.length).toBe(count - 2);
  1134. });
  1135. });
  1136. describe('#undo()', () => {
  1137. it('should undo a cell action', () => {
  1138. const count = widget.widgets.length;
  1139. const next = widget.widgets[1];
  1140. widget.select(next);
  1141. NotebookActions.deleteCells(widget);
  1142. NotebookActions.undo(widget);
  1143. expect(widget.widgets.length).toBe(count);
  1144. });
  1145. it('should switch the widget to command mode', () => {
  1146. widget.mode = 'edit';
  1147. NotebookActions.undo(widget);
  1148. expect(widget.mode).toBe('command');
  1149. });
  1150. it('should be a no-op if there is no model', () => {
  1151. widget.model = null;
  1152. NotebookActions.undo(widget);
  1153. expect(widget.activeCellIndex).toBe(-1);
  1154. });
  1155. it('should be a no-op if there are no cell actions to undo', () => {
  1156. const count = widget.widgets.length;
  1157. NotebookActions.deleteCells(widget);
  1158. widget.model!.cells.clearUndo();
  1159. NotebookActions.undo(widget);
  1160. expect(widget.widgets.length).toBe(count - 1);
  1161. });
  1162. });
  1163. describe('#redo()', () => {
  1164. it('should redo a cell action', () => {
  1165. const count = widget.widgets.length;
  1166. const next = widget.widgets[1];
  1167. widget.select(next);
  1168. NotebookActions.deleteCells(widget);
  1169. NotebookActions.undo(widget);
  1170. NotebookActions.redo(widget);
  1171. expect(widget.widgets.length).toBe(count - 2);
  1172. });
  1173. it('should switch the widget to command mode', () => {
  1174. NotebookActions.undo(widget);
  1175. widget.mode = 'edit';
  1176. NotebookActions.redo(widget);
  1177. expect(widget.mode).toBe('command');
  1178. });
  1179. it('should be a no-op if there is no model', () => {
  1180. NotebookActions.undo(widget);
  1181. widget.model = null;
  1182. NotebookActions.redo(widget);
  1183. expect(widget.activeCellIndex).toBe(-1);
  1184. });
  1185. it('should be a no-op if there are no cell actions to redo', () => {
  1186. const count = widget.widgets.length;
  1187. NotebookActions.redo(widget);
  1188. expect(widget.widgets.length).toBe(count);
  1189. });
  1190. });
  1191. describe('#toggleAllLineNumbers()', () => {
  1192. it('should toggle line numbers on all cells', () => {
  1193. const state = widget.activeCell!.editor.getOption('lineNumbers');
  1194. NotebookActions.toggleAllLineNumbers(widget);
  1195. for (let i = 0; i < widget.widgets.length; i++) {
  1196. const lineNumbers = widget.widgets[i].editor.getOption('lineNumbers');
  1197. expect(lineNumbers).toBe(!state);
  1198. }
  1199. });
  1200. it('should be based on the state of the active cell', () => {
  1201. const state = widget.activeCell!.editor.getOption('lineNumbers');
  1202. for (let i = 1; i < widget.widgets.length; i++) {
  1203. widget.widgets[i].editor.setOption('lineNumbers', !state);
  1204. }
  1205. NotebookActions.toggleAllLineNumbers(widget);
  1206. for (let i = 0; i < widget.widgets.length; i++) {
  1207. const lineNumbers = widget.widgets[i].editor.getOption('lineNumbers');
  1208. expect(lineNumbers).toBe(!state);
  1209. }
  1210. });
  1211. it('should preserve the widget mode', () => {
  1212. NotebookActions.toggleAllLineNumbers(widget);
  1213. expect(widget.mode).toBe('command');
  1214. widget.mode = 'edit';
  1215. NotebookActions.toggleAllLineNumbers(widget);
  1216. expect(widget.mode).toBe('edit');
  1217. });
  1218. it('should be a no-op if there is no model', () => {
  1219. widget.model = null;
  1220. NotebookActions.toggleAllLineNumbers(widget);
  1221. expect(widget.activeCellIndex).toBe(-1);
  1222. });
  1223. });
  1224. describe('#clearOutputs()', () => {
  1225. it('should clear the outputs on the selected cells', () => {
  1226. // Select the next code cell that has outputs.
  1227. let index = 0;
  1228. for (let i = 1; i < widget.widgets.length; i++) {
  1229. const cell = widget.widgets[i];
  1230. if (cell instanceof CodeCell && cell.model.outputs.length) {
  1231. widget.select(cell);
  1232. index = i;
  1233. break;
  1234. }
  1235. }
  1236. NotebookActions.clearOutputs(widget);
  1237. let cell = widget.widgets[0] as CodeCell;
  1238. expect(cell.model.outputs.length).toBe(0);
  1239. cell = widget.widgets[index] as CodeCell;
  1240. expect(cell.model.outputs.length).toBe(0);
  1241. });
  1242. it('should preserve the widget mode', () => {
  1243. NotebookActions.clearOutputs(widget);
  1244. expect(widget.mode).toBe('command');
  1245. widget.mode = 'edit';
  1246. NotebookActions.clearOutputs(widget);
  1247. expect(widget.mode).toBe('edit');
  1248. });
  1249. it('should be a no-op if there is no model', () => {
  1250. widget.model = null;
  1251. NotebookActions.clearOutputs(widget);
  1252. expect(widget.activeCellIndex).toBe(-1);
  1253. });
  1254. });
  1255. describe('#clearAllOutputs()', () => {
  1256. it('should clear the outputs on all cells', () => {
  1257. const next = widget.widgets[1];
  1258. widget.select(next);
  1259. NotebookActions.clearAllOutputs(widget);
  1260. for (let i = 0; i < widget.widgets.length; i++) {
  1261. const cell = widget.widgets[i];
  1262. if (cell instanceof CodeCell) {
  1263. expect(cell.model.outputs.length).toBe(0);
  1264. }
  1265. }
  1266. });
  1267. it('should preserve the widget mode', () => {
  1268. NotebookActions.clearAllOutputs(widget);
  1269. expect(widget.mode).toBe('command');
  1270. widget.mode = 'edit';
  1271. NotebookActions.clearAllOutputs(widget);
  1272. expect(widget.mode).toBe('edit');
  1273. });
  1274. it('should be a no-op if there is no model', () => {
  1275. widget.model = null;
  1276. NotebookActions.clearAllOutputs(widget);
  1277. expect(widget.activeCellIndex).toBe(-1);
  1278. });
  1279. });
  1280. describe('#setMarkdownHeader()', () => {
  1281. it('should set the markdown header level of selected cells', () => {
  1282. const next = widget.widgets[1];
  1283. widget.select(next);
  1284. NotebookActions.setMarkdownHeader(widget, 2);
  1285. expect(widget.activeCell!.model.value.text.slice(0, 3)).toBe('## ');
  1286. expect(next.model.value.text.slice(0, 3)).toBe('## ');
  1287. });
  1288. it('should convert the cells to markdown type', () => {
  1289. NotebookActions.setMarkdownHeader(widget, 2);
  1290. expect(widget.activeCell).toBeInstanceOf(MarkdownCell);
  1291. });
  1292. it('should be clamped between 1 and 6', () => {
  1293. NotebookActions.setMarkdownHeader(widget, -1);
  1294. expect(widget.activeCell!.model.value.text.slice(0, 2)).toBe('# ');
  1295. NotebookActions.setMarkdownHeader(widget, 10);
  1296. expect(widget.activeCell!.model.value.text.slice(0, 7)).toBe('###### ');
  1297. });
  1298. it('should be a no-op if there is no model', () => {
  1299. widget.model = null;
  1300. NotebookActions.setMarkdownHeader(widget, 1);
  1301. expect(widget.activeCellIndex).toBe(-1);
  1302. });
  1303. it('should replace an existing header', () => {
  1304. widget.activeCell!.model.value.text = '# foo';
  1305. NotebookActions.setMarkdownHeader(widget, 2);
  1306. expect(widget.activeCell!.model.value.text).toBe('## foo');
  1307. });
  1308. it('should replace leading white space', () => {
  1309. widget.activeCell!.model.value.text = ' foo';
  1310. NotebookActions.setMarkdownHeader(widget, 2);
  1311. expect(widget.activeCell!.model.value.text).toBe('## foo');
  1312. });
  1313. it('should unrender the cells', () => {
  1314. NotebookActions.setMarkdownHeader(widget, 1);
  1315. expect((widget.activeCell as MarkdownCell).rendered).toBe(false);
  1316. });
  1317. });
  1318. describe('#trust()', () => {
  1319. it('should trust the notebook cells if the user accepts', async () => {
  1320. const model = widget.model!;
  1321. model.fromJSON(utils.DEFAULT_CONTENT);
  1322. const cell = model.cells.get(0);
  1323. expect(cell.trusted).not.toBe(true);
  1324. const promise = NotebookActions.trust(widget);
  1325. await acceptDialog();
  1326. await promise;
  1327. expect(cell.trusted).toBe(true);
  1328. });
  1329. it('should not trust the notebook cells if the user aborts', async () => {
  1330. const model = widget.model!;
  1331. model.fromJSON(utils.DEFAULT_CONTENT);
  1332. const cell = model.cells.get(0);
  1333. expect(cell.trusted).not.toBe(true);
  1334. const promise = NotebookActions.trust(widget);
  1335. await dismissDialog();
  1336. await promise;
  1337. expect(cell.trusted).not.toBe(true);
  1338. });
  1339. it('should be a no-op if the model is `null`', async () => {
  1340. widget.model = null;
  1341. await NotebookActions.trust(widget);
  1342. });
  1343. it('should show a dialog if all cells are trusted', async () => {
  1344. const model = widget.model!;
  1345. model.fromJSON(utils.DEFAULT_CONTENT);
  1346. model.fromJSON(utils.DEFAULT_CONTENT);
  1347. for (let i = 0; i < model.cells.length; i++) {
  1348. const cell = model.cells.get(i);
  1349. cell.trusted = true;
  1350. }
  1351. const promise = NotebookActions.trust(widget);
  1352. await acceptDialog();
  1353. await promise;
  1354. });
  1355. });
  1356. });
  1357. });