widget.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { Message, MessageLoop } from '@lumino/messaging';
  5. import { Widget } from '@lumino/widgets';
  6. import { SessionContext } from '@jupyterlab/apputils';
  7. import { CodeConsole } from '../src';
  8. import {
  9. CodeCell,
  10. CodeCellModel,
  11. RawCellModel,
  12. RawCell
  13. } from '@jupyterlab/cells';
  14. import { createSessionContext, NBTestUtils } from '@jupyterlab/testutils';
  15. import {
  16. createConsoleFactory,
  17. rendermime,
  18. mimeTypeService,
  19. editorFactory
  20. } from './utils';
  21. class TestConsole extends CodeConsole {
  22. methods: string[] = [];
  23. protected newPromptCell(): void {
  24. this.methods.push('newPromptCell');
  25. super.newPromptCell();
  26. }
  27. protected onActivateRequest(msg: Message): void {
  28. this.methods.push('onActivateRequest');
  29. super.onActivateRequest(msg);
  30. }
  31. protected onAfterAttach(msg: Message): void {
  32. this.methods.push('onAfterAttach');
  33. super.onAfterAttach(msg);
  34. }
  35. protected onUpdateRequest(msg: Message): void {
  36. this.methods.push('onUpdateRequest');
  37. super.onUpdateRequest(msg);
  38. }
  39. }
  40. const contentFactory = createConsoleFactory();
  41. describe('console/widget', () => {
  42. describe('CodeConsole', () => {
  43. let widget: TestConsole;
  44. beforeEach(async () => {
  45. const sessionContext = await createSessionContext();
  46. widget = new TestConsole({
  47. contentFactory,
  48. rendermime,
  49. sessionContext,
  50. mimeTypeService
  51. });
  52. });
  53. afterEach(async () => {
  54. await widget.sessionContext.shutdown();
  55. widget.sessionContext.dispose();
  56. widget.dispose();
  57. });
  58. describe('#constructor()', () => {
  59. it('should create a new console content widget', () => {
  60. Widget.attach(widget, document.body);
  61. expect(widget).toBeInstanceOf(CodeConsole);
  62. expect(Array.from(widget.node.classList)).toEqual(
  63. expect.arrayContaining(['jp-CodeConsole'])
  64. );
  65. });
  66. });
  67. describe('#cells', () => {
  68. it('should exist upon instantiation', () => {
  69. expect(widget.cells).toBeTruthy();
  70. });
  71. it('should reflect the contents of the widget', async () => {
  72. const force = true;
  73. Widget.attach(widget, document.body);
  74. await (widget.sessionContext as SessionContext).initialize();
  75. await widget.execute(force);
  76. expect(widget.cells.length).toBe(1);
  77. widget.clear();
  78. expect(widget.cells.length).toBe(0);
  79. });
  80. });
  81. describe('#executed', () => {
  82. it('should emit a date upon execution', async () => {
  83. let called: Date | null = null;
  84. const force = true;
  85. Widget.attach(widget, document.body);
  86. widget.executed.connect((sender, time) => {
  87. called = time;
  88. });
  89. await (widget.sessionContext as SessionContext).initialize();
  90. await widget.execute(force);
  91. expect(called).toBeInstanceOf(Date);
  92. });
  93. });
  94. describe('#promptCell', () => {
  95. it('should be a code cell widget', () => {
  96. Widget.attach(widget, document.body);
  97. expect(widget.promptCell).toBeInstanceOf(CodeCell);
  98. });
  99. it('should be replaced after execution', async () => {
  100. const force = true;
  101. Widget.attach(widget, document.body);
  102. const old = widget.promptCell;
  103. expect(old).toBeInstanceOf(CodeCell);
  104. await (widget.sessionContext as SessionContext).initialize();
  105. await widget.execute(force);
  106. expect(widget.promptCell).toBeInstanceOf(CodeCell);
  107. expect(widget.promptCell).not.toBe(old);
  108. });
  109. });
  110. describe('#session', () => {
  111. it('should be a client session object', () => {
  112. expect(widget.sessionContext.sessionChanged).toBeTruthy();
  113. });
  114. });
  115. describe('#contentFactory', () => {
  116. it('should be the content factory used by the widget', () => {
  117. expect(widget.contentFactory).toBeInstanceOf(
  118. CodeConsole.ContentFactory
  119. );
  120. });
  121. });
  122. describe('#addCell()', () => {
  123. it('should add a code cell to the content widget', () => {
  124. const contentFactory = NBTestUtils.createCodeCellFactory();
  125. const model = new CodeCellModel({});
  126. const cell = new CodeCell({
  127. model,
  128. contentFactory,
  129. rendermime
  130. }).initializeState();
  131. Widget.attach(widget, document.body);
  132. expect(widget.cells.length).toBe(0);
  133. widget.addCell(cell);
  134. expect(widget.cells.length).toBe(1);
  135. });
  136. });
  137. describe('#clear()', () => {
  138. it('should clear all of the content cells except the banner', async () => {
  139. const force = true;
  140. Widget.attach(widget, document.body);
  141. await (widget.sessionContext as SessionContext).initialize();
  142. await widget.execute(force);
  143. expect(widget.cells.length).toBeGreaterThan(0);
  144. widget.clear();
  145. expect(widget.cells.length).toBe(0);
  146. expect(widget.promptCell!.model.value.text).toBe('');
  147. });
  148. });
  149. describe('#dispose()', () => {
  150. it('should dispose the content widget', () => {
  151. Widget.attach(widget, document.body);
  152. expect(widget.isDisposed).toBe(false);
  153. widget.dispose();
  154. expect(widget.isDisposed).toBe(true);
  155. });
  156. it('should be safe to dispose multiple times', () => {
  157. Widget.attach(widget, document.body);
  158. expect(widget.isDisposed).toBe(false);
  159. widget.dispose();
  160. widget.dispose();
  161. expect(widget.isDisposed).toBe(true);
  162. });
  163. });
  164. describe('#execute()', () => {
  165. it('should execute contents of the prompt if forced', async () => {
  166. const force = true;
  167. Widget.attach(widget, document.body);
  168. expect(widget.cells.length).toBe(0);
  169. await (widget.sessionContext as SessionContext).initialize();
  170. await widget.execute(force);
  171. expect(widget.cells.length).toBeGreaterThan(0);
  172. });
  173. it('should check if code is multiline and allow amending', async () => {
  174. const force = false;
  175. const timeout = 9000;
  176. Widget.attach(widget, document.body);
  177. widget.promptCell!.model.value.text = 'for x in range(5):';
  178. expect(widget.cells.length).toBe(0);
  179. const session = widget.sessionContext as SessionContext;
  180. session.kernelPreference = { name: 'ipython' };
  181. await session.initialize();
  182. await widget.execute(force, timeout);
  183. expect(widget.cells.length).toBe(0);
  184. });
  185. });
  186. describe('#inject()', () => {
  187. it('should add a code cell and execute it', async () => {
  188. const code = 'print("#inject()")';
  189. Widget.attach(widget, document.body);
  190. expect(widget.cells.length).toBe(0);
  191. await widget.inject(code);
  192. expect(widget.cells.length).toBeGreaterThan(0);
  193. });
  194. });
  195. describe('#insertLinebreak()', () => {
  196. it('should insert a line break into the prompt', () => {
  197. Widget.attach(widget, document.body);
  198. const model = widget.promptCell!.model;
  199. expect(model.value.text).toHaveLength(0);
  200. widget.insertLinebreak();
  201. expect(model.value.text).toBe('\n');
  202. });
  203. });
  204. describe('#serialize()', () => {
  205. it('should serialize the contents of a console', () => {
  206. Widget.attach(widget, document.body);
  207. widget.promptCell!.model.value.text = 'foo';
  208. const serialized = widget.serialize();
  209. expect(serialized).toHaveLength(1);
  210. expect(serialized[0].source).toBe('foo');
  211. });
  212. });
  213. describe('#newPromptCell()', () => {
  214. it('should be called after attach, creating a prompt', () => {
  215. expect(widget.promptCell).toBeFalsy();
  216. expect(widget.methods).toEqual(
  217. expect.not.arrayContaining(['newPromptCell'])
  218. );
  219. Widget.attach(widget, document.body);
  220. expect(widget.methods).toEqual(
  221. expect.arrayContaining(['newPromptCell'])
  222. );
  223. expect(widget.promptCell).toBeTruthy();
  224. });
  225. it('should be called after execution, creating a prompt', async () => {
  226. expect(widget.promptCell).toBeFalsy();
  227. expect(widget.methods).toEqual(
  228. expect.not.arrayContaining(['newPromptCell'])
  229. );
  230. Widget.attach(widget, document.body);
  231. expect(widget.methods).toEqual(
  232. expect.arrayContaining(['newPromptCell'])
  233. );
  234. const old = widget.promptCell;
  235. const force = true;
  236. expect(old).toBeInstanceOf(CodeCell);
  237. widget.methods = [];
  238. await (widget.sessionContext as SessionContext).initialize();
  239. await widget.execute(force);
  240. expect(widget.promptCell).toBeInstanceOf(CodeCell);
  241. expect(widget.promptCell).not.toBe(old);
  242. expect(widget.methods).toEqual(
  243. expect.arrayContaining(['newPromptCell'])
  244. );
  245. });
  246. });
  247. describe('#onActivateRequest()', () => {
  248. it('should focus the prompt editor', () => {
  249. expect(widget.promptCell).toBeFalsy();
  250. expect(widget.methods).toEqual(
  251. expect.not.arrayContaining(['onActivateRequest'])
  252. );
  253. Widget.attach(widget, document.body);
  254. MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
  255. expect(widget.methods).toEqual(
  256. expect.arrayContaining(['onActivateRequest'])
  257. );
  258. expect(widget.promptCell!.editor.hasFocus()).toBe(true);
  259. });
  260. });
  261. describe('#onAfterAttach()', () => {
  262. it('should be called after attach, creating a prompt', () => {
  263. expect(widget.promptCell).toBeFalsy();
  264. expect(widget.methods).toEqual(
  265. expect.not.arrayContaining(['onAfterAttach'])
  266. );
  267. Widget.attach(widget, document.body);
  268. expect(widget.methods).toEqual(
  269. expect.arrayContaining(['onAfterAttach'])
  270. );
  271. expect(widget.promptCell).toBeTruthy();
  272. });
  273. });
  274. describe('.ContentFactory', () => {
  275. describe('#constructor', () => {
  276. it('should create a new ContentFactory', () => {
  277. const factory = new CodeConsole.ContentFactory({ editorFactory });
  278. expect(factory).toBeInstanceOf(CodeConsole.ContentFactory);
  279. });
  280. });
  281. describe('#createCodeCell', () => {
  282. it('should create a code cell', () => {
  283. const model = new CodeCellModel({});
  284. const prompt = contentFactory.createCodeCell({
  285. rendermime: widget.rendermime,
  286. model,
  287. contentFactory
  288. });
  289. expect(prompt).toBeInstanceOf(CodeCell);
  290. });
  291. });
  292. describe('#createRawCell', () => {
  293. it('should create a foreign cell', () => {
  294. const model = new RawCellModel({});
  295. const prompt = contentFactory.createRawCell({
  296. model,
  297. contentFactory
  298. });
  299. expect(prompt).toBeInstanceOf(RawCell);
  300. });
  301. });
  302. });
  303. describe('.ModelFactory', () => {
  304. describe('#constructor()', () => {
  305. it('should create a new model factory', () => {
  306. const factory = new CodeConsole.ModelFactory({});
  307. expect(factory).toBeInstanceOf(CodeConsole.ModelFactory);
  308. });
  309. it('should accept a codeCellContentFactory', () => {
  310. const codeCellContentFactory = new CodeCellModel.ContentFactory();
  311. const factory = new CodeConsole.ModelFactory({
  312. codeCellContentFactory
  313. });
  314. expect(factory.codeCellContentFactory).toBe(codeCellContentFactory);
  315. });
  316. });
  317. describe('#codeCellContentFactory', () => {
  318. it('should be the code cell content factory used by the factory', () => {
  319. const factory = new CodeConsole.ModelFactory({});
  320. expect(factory.codeCellContentFactory).toBe(
  321. CodeCellModel.defaultContentFactory
  322. );
  323. });
  324. });
  325. describe('#createCodeCell()', () => {
  326. it('should create a code cell', () => {
  327. const factory = new CodeConsole.ModelFactory({});
  328. expect(factory.createCodeCell({})).toBeInstanceOf(CodeCellModel);
  329. });
  330. });
  331. describe('#createRawCell()', () => {
  332. it('should create a raw cell model', () => {
  333. const factory = new CodeConsole.ModelFactory({});
  334. expect(factory.createRawCell({})).toBeInstanceOf(RawCellModel);
  335. });
  336. });
  337. });
  338. describe('.defaultModelFactory', () => {
  339. it('should be a ModelFactory', () => {
  340. expect(CodeConsole.defaultModelFactory).toBeInstanceOf(
  341. CodeConsole.ModelFactory
  342. );
  343. });
  344. });
  345. });
  346. });