actions.spec.ts 56 KB

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