actions.spec.ts 58 KB

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