default-toolbar.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { simulate } from 'simulate-event';
  5. import { Widget } from '@lumino/widgets';
  6. import { Context } from '@jupyterlab/docregistry';
  7. import { CodeCell, MarkdownCell } from '@jupyterlab/cells';
  8. import {
  9. INotebookModel,
  10. NotebookActions,
  11. NotebookPanel,
  12. ToolbarItems
  13. } from '../src';
  14. import {
  15. signalToPromise,
  16. sleep,
  17. framePromise,
  18. acceptDialog
  19. } from '@jupyterlab/testutils';
  20. import * as utils from './utils';
  21. import { PromiseDelegate } from '@lumino/coreutils';
  22. import { KernelMessage } from '@jupyterlab/services';
  23. const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
  24. describe('@jupyterlab/notebook', () => {
  25. describe('ToolbarItems', () => {
  26. describe('noKernel', () => {
  27. let context: Context<INotebookModel>;
  28. let panel: NotebookPanel;
  29. beforeEach(async () => {
  30. context = await utils.createMockContext();
  31. panel = utils.createNotebookPanel(context);
  32. context.model.fromJSON(utils.DEFAULT_CONTENT);
  33. });
  34. afterEach(() => {
  35. panel.dispose();
  36. context.dispose();
  37. });
  38. describe('#createSaveButton()', () => {
  39. it('should save when clicked', async () => {
  40. const button = ToolbarItems.createSaveButton(panel);
  41. Widget.attach(button, document.body);
  42. const promise = signalToPromise(context.fileChanged);
  43. await framePromise();
  44. simulate(button.node.firstChild as HTMLElement, 'mousedown');
  45. await promise;
  46. button.dispose();
  47. });
  48. it("should add an inline svg node with the 'save' icon", async () => {
  49. const button = ToolbarItems.createSaveButton(panel);
  50. Widget.attach(button, document.body);
  51. await framePromise();
  52. expect(
  53. button.node.querySelector("[data-icon$='save']")
  54. ).toBeDefined();
  55. });
  56. });
  57. describe('#createInsertButton()', () => {
  58. it('should insert below when clicked', async () => {
  59. const button = ToolbarItems.createInsertButton(panel);
  60. Widget.attach(button, document.body);
  61. await framePromise();
  62. simulate(button.node.firstChild as HTMLElement, 'mousedown');
  63. expect(panel.content.activeCellIndex).toBe(1);
  64. expect(panel.content.activeCell).toBeInstanceOf(CodeCell);
  65. button.dispose();
  66. });
  67. it("should add an inline svg node with the 'add' icon", async () => {
  68. const button = ToolbarItems.createInsertButton(panel);
  69. Widget.attach(button, document.body);
  70. await framePromise();
  71. expect(button.node.querySelector("[data-icon$='add']")).toBeDefined();
  72. button.dispose();
  73. });
  74. });
  75. describe('#createCutButton()', () => {
  76. it('should cut when clicked', async () => {
  77. const button = ToolbarItems.createCutButton(panel);
  78. const count = panel.content.widgets.length;
  79. Widget.attach(button, document.body);
  80. await framePromise();
  81. simulate(button.node.firstChild as HTMLElement, 'mousedown');
  82. expect(panel.content.widgets.length).toBe(count - 1);
  83. expect(utils.clipboard.hasData(JUPYTER_CELL_MIME)).toBe(true);
  84. button.dispose();
  85. });
  86. it("should add an inline svg node with the 'cut' icon", async () => {
  87. const button = ToolbarItems.createCutButton(panel);
  88. Widget.attach(button, document.body);
  89. await framePromise();
  90. expect(button.node.querySelector("[data-icon$='cut']")).toBeDefined();
  91. button.dispose();
  92. });
  93. });
  94. describe('#createCopyButton()', () => {
  95. it('should copy when clicked', async () => {
  96. const button = ToolbarItems.createCopyButton(panel);
  97. const count = panel.content.widgets.length;
  98. Widget.attach(button, document.body);
  99. await framePromise();
  100. simulate(button.node.firstChild as HTMLElement, 'mousedown');
  101. expect(panel.content.widgets.length).toBe(count);
  102. expect(utils.clipboard.hasData(JUPYTER_CELL_MIME)).toBe(true);
  103. button.dispose();
  104. });
  105. it("should add an inline svg node with the 'copy' icon", async () => {
  106. const button = ToolbarItems.createCopyButton(panel);
  107. Widget.attach(button, document.body);
  108. await framePromise();
  109. expect(
  110. button.node.querySelector("[data-icon$='copy']")
  111. ).toBeDefined();
  112. button.dispose();
  113. });
  114. });
  115. describe('#createPasteButton()', () => {
  116. it('should paste when clicked', async () => {
  117. const button = ToolbarItems.createPasteButton(panel);
  118. const count = panel.content.widgets.length;
  119. Widget.attach(button, document.body);
  120. await framePromise();
  121. NotebookActions.copy(panel.content);
  122. simulate(button.node.firstChild as HTMLElement, 'mousedown');
  123. await sleep();
  124. expect(panel.content.widgets.length).toBe(count + 1);
  125. button.dispose();
  126. });
  127. it("should add an inline svg node with the 'paste' icon", async () => {
  128. const button = ToolbarItems.createPasteButton(panel);
  129. Widget.attach(button, document.body);
  130. await framePromise();
  131. expect(
  132. button.node.querySelector("[data-icon$='paste']")
  133. ).toBeDefined();
  134. button.dispose();
  135. });
  136. });
  137. describe('#createCellTypeItem()', () => {
  138. it('should track the cell type of the current cell', async () => {
  139. const item = ToolbarItems.createCellTypeItem(panel);
  140. Widget.attach(item, document.body);
  141. await framePromise();
  142. const node = item.node.getElementsByTagName(
  143. 'select'
  144. )[0] as HTMLSelectElement;
  145. expect(node.value).toBe('code');
  146. panel.content.activeCellIndex++;
  147. await framePromise();
  148. expect(node.value).toBe('markdown');
  149. item.dispose();
  150. });
  151. it("should display `'-'` if multiple cell types are selected", async () => {
  152. const item = ToolbarItems.createCellTypeItem(panel);
  153. Widget.attach(item, document.body);
  154. await framePromise();
  155. const node = item.node.getElementsByTagName(
  156. 'select'
  157. )[0] as HTMLSelectElement;
  158. expect(node.value).toBe('code');
  159. panel.content.select(panel.content.widgets[1]);
  160. await framePromise();
  161. expect(node.value).toBe('-');
  162. item.dispose();
  163. });
  164. it('should display the active cell type if multiple cells of the same type are selected', async () => {
  165. const item = ToolbarItems.createCellTypeItem(panel);
  166. Widget.attach(item, document.body);
  167. await framePromise();
  168. const node = item.node.getElementsByTagName(
  169. 'select'
  170. )[0] as HTMLSelectElement;
  171. expect(node.value).toBe('code');
  172. const cell = panel.model!.contentFactory.createCodeCell({});
  173. panel.model!.cells.insert(1, cell);
  174. panel.content.select(panel.content.widgets[1]);
  175. await framePromise();
  176. expect(node.value).toBe('code');
  177. item.dispose();
  178. });
  179. });
  180. describe('#getDefaultItems()', () => {
  181. it('should return the default items of the panel toolbar', () => {
  182. const names = ToolbarItems.getDefaultItems(panel).map(item => {
  183. const name = item.name;
  184. item.widget.dispose();
  185. return name;
  186. });
  187. expect(names).toEqual([
  188. 'save',
  189. 'insert',
  190. 'cut',
  191. 'copy',
  192. 'paste',
  193. 'run',
  194. 'interrupt',
  195. 'restart',
  196. 'restart-and-run',
  197. 'cellType',
  198. 'spacer',
  199. 'kernelName',
  200. 'kernelStatus'
  201. ]);
  202. });
  203. });
  204. });
  205. describe('kernelRequired', () => {
  206. let context: Context<INotebookModel>;
  207. let panel: NotebookPanel;
  208. beforeEach(async function() {
  209. context = await utils.createMockContext(true);
  210. panel = utils.createNotebookPanel(context);
  211. context.model.fromJSON(utils.DEFAULT_CONTENT);
  212. });
  213. afterEach(async () => {
  214. await context.sessionContext.shutdown();
  215. panel.dispose();
  216. context.dispose();
  217. });
  218. describe('#createRunButton()', () => {
  219. it('should run and advance when clicked', async () => {
  220. const button = ToolbarItems.createRunButton(panel);
  221. const widget = panel.content;
  222. // Clear and select the first two cells.
  223. const codeCell = widget.widgets[0] as CodeCell;
  224. codeCell.model.outputs.clear();
  225. widget.select(codeCell);
  226. const mdCell = widget.widgets[1] as MarkdownCell;
  227. mdCell.rendered = false;
  228. widget.select(mdCell);
  229. Widget.attach(button, document.body);
  230. await context.sessionContext.session!.kernel!.info;
  231. const delegate = new PromiseDelegate();
  232. panel.sessionContext.iopubMessage.connect((_, msg) => {
  233. if (KernelMessage.isExecuteInputMsg(msg)) {
  234. delegate.resolve(void 0);
  235. }
  236. });
  237. simulate(button.node.firstChild as HTMLElement, 'mousedown');
  238. await delegate.promise;
  239. button.dispose();
  240. });
  241. it("should add an inline svg node with the 'run' icon", async () => {
  242. const button = ToolbarItems.createRunButton(panel);
  243. Widget.attach(button, document.body);
  244. await framePromise();
  245. expect(button.node.querySelector("[data-icon$='run']")).toBeDefined();
  246. });
  247. });
  248. describe('#createRestartRunAllButton()', () => {
  249. it('should restart and run all when clicked', async () => {
  250. const button = ToolbarItems.createRestartRunAllButton(panel);
  251. const widget = panel.content;
  252. // Clear the first two cells.
  253. const codeCell = widget.widgets[0] as CodeCell;
  254. codeCell.model.outputs.clear();
  255. const mdCell = widget.widgets[1] as MarkdownCell;
  256. mdCell.rendered = false;
  257. Widget.attach(button, document.body);
  258. await panel.sessionContext.ready;
  259. const delegate = new PromiseDelegate();
  260. panel.sessionContext.iopubMessage.connect((_, msg) => {
  261. if (KernelMessage.isExecuteInputMsg(msg)) {
  262. delegate.resolve(void 0);
  263. }
  264. });
  265. simulate(button.node.firstChild as HTMLElement, 'mousedown');
  266. await acceptDialog();
  267. await delegate.promise;
  268. button.dispose();
  269. });
  270. it("should add an inline svg node with the 'fast-forward' icon", async () => {
  271. const button = ToolbarItems.createRestartRunAllButton(panel);
  272. Widget.attach(button, document.body);
  273. await framePromise();
  274. expect(
  275. button.node.querySelector("[data-icon$='fast-forward']")
  276. ).toBeDefined();
  277. });
  278. });
  279. });
  280. });
  281. });