actions.spec.ts 52 KB

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