actions.spec.ts 55 KB

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