actions.spec.ts 56 KB

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