widget.spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import expect = require('expect.js');
  4. import * as CodeMirror from 'codemirror';
  5. import {
  6. MockKernel
  7. } from 'jupyter-js-services/lib/mockkernel';
  8. import {
  9. Message
  10. } from 'phosphor-messaging';
  11. import {
  12. BaseCellWidget, CellModel, InputAreaWidget
  13. } from '../../../../lib/notebook/cells';
  14. import {
  15. CellEditorWidget
  16. } from '../../../../lib/notebook/cells/editor';
  17. const INPUT_CLASS = 'jp-InputArea';
  18. class LogCell extends BaseCellWidget {
  19. methods: string[] = [];
  20. messages: string[] = [];
  21. processMessage(msg: Message): void {
  22. super.processMessage(msg);
  23. this.messages.push(msg.type);
  24. }
  25. protected onAfterAttach(msg: Message): void {
  26. super.onAfterAttach(msg);
  27. this.methods.push('onAfterAttach');
  28. }
  29. }
  30. describe('jupyter-js-notebook', () => {
  31. describe('BaseCellWidget', () => {
  32. describe('.createCellEditor()', () => {
  33. it('should create a cell editor widget', () => {
  34. let editor = BaseCellWidget.createCellEditor(new CellModel());
  35. expect(editor).to.be.a(CellEditorWidget);
  36. });
  37. });
  38. describe('.createInputArea()', () => {
  39. it('should create an input area widget', () => {
  40. let editor = BaseCellWidget.createCellEditor(new CellModel());
  41. let input = BaseCellWidget.createInputArea(editor);
  42. expect(input).to.be.a(InputAreaWidget);
  43. });
  44. });
  45. describe('#constructor()', () => {
  46. it('should create a base cell widget', () => {
  47. let widget = new BaseCellWidget(new CellModel());
  48. expect(widget).to.be.a(BaseCellWidget);
  49. });
  50. });
  51. describe('#model', () => {
  52. it('should be read-only', () => {
  53. let widget = new BaseCellWidget(new CellModel());
  54. expect(() => { widget.model = null; }).to.throwError();
  55. });
  56. });
  57. describe('#editor', () => {
  58. it('should be a cell editor widget', () => {
  59. let widget = new BaseCellWidget(new CellModel());
  60. expect(widget.editor).to.be.a(CellEditorWidget);
  61. });
  62. it('should be read-only', () => {
  63. let widget = new BaseCellWidget(new CellModel());
  64. expect(() => { widget.editor = null; }).to.throwError();
  65. });
  66. });
  67. describe('#mimetype', () => {
  68. it('should be a string', () => {
  69. let widget = new BaseCellWidget(new CellModel());
  70. expect(typeof widget.mimetype).to.be('string');
  71. });
  72. it('should default to text/plain', () => {
  73. let widget = new BaseCellWidget(new CellModel());
  74. expect(widget.mimetype).to.be('text/plain');
  75. });
  76. it('should supporting being set to other types', () => {
  77. let widget = new BaseCellWidget(new CellModel());
  78. widget.mimetype = 'test/test';
  79. expect(widget.mimetype).to.be('test/test');
  80. });
  81. it('should not allow being set to empty or null strings', () => {
  82. let widget = new BaseCellWidget(new CellModel());
  83. widget.mimetype = null;
  84. expect(widget.mimetype).to.be('text/plain');
  85. widget.mimetype = '';
  86. expect(widget.mimetype).to.be('text/plain');
  87. });
  88. });
  89. describe('#readOnly', () => {
  90. it('should be a boolean', () => {
  91. let widget = new BaseCellWidget(new CellModel());
  92. expect(typeof widget.readOnly).to.be('boolean');
  93. });
  94. it('should default to false', () => {
  95. let widget = new BaseCellWidget(new CellModel());
  96. expect(widget.readOnly).to.be(false);
  97. });
  98. it('should be settable', () => {
  99. let widget = new BaseCellWidget(new CellModel());
  100. widget.readOnly = true;
  101. expect(widget.readOnly).to.be(true);
  102. });
  103. });
  104. describe('#trusted', () => {
  105. it('should be a boolean', () => {
  106. let widget = new BaseCellWidget(new CellModel());
  107. expect(typeof widget.trusted).to.be('boolean');
  108. });
  109. it('should default to false', () => {
  110. let widget = new BaseCellWidget(new CellModel());
  111. expect(widget.trusted).to.be(false);
  112. });
  113. it('should be settable', () => {
  114. let widget = new BaseCellWidget(new CellModel());
  115. widget.trusted = true;
  116. expect(widget.trusted).to.be(true);
  117. });
  118. });
  119. describe('#focus()', () => {
  120. it('should focus the cell editor', () => {
  121. let widget = new BaseCellWidget(new CellModel());
  122. widget.attach(document.body);
  123. expect(widget.editor.editor.hasFocus()).to.be(false);
  124. widget.focus();
  125. expect(widget.editor.editor.hasFocus()).to.be(true);
  126. widget.dispose();
  127. });
  128. });
  129. describe('#setPrompt()', () => {
  130. it('should not throw an error (full test in input area)', () => {
  131. let widget = new BaseCellWidget(new CellModel());
  132. expect(() => { widget.setPrompt(void 0); }).to.not.throwError();
  133. expect(() => { widget.setPrompt(null); }).to.not.throwError();
  134. expect(() => { widget.setPrompt(''); }).to.not.throwError();
  135. expect(() => { widget.setPrompt('null'); }).to.not.throwError();
  136. expect(() => { widget.setPrompt('test'); }).to.not.throwError();
  137. });
  138. });
  139. describe('#toggleInput()', () => {
  140. it('should toggle whether the input is shown', () => {
  141. let widget = new BaseCellWidget(new CellModel());
  142. let input = widget.node.getElementsByClassName(INPUT_CLASS)[0];
  143. widget.attach(document.body);
  144. expect(window.getComputedStyle(input).display).to.not.be('none');
  145. widget.toggleInput(false);
  146. expect(window.getComputedStyle(input).display).to.be('none');
  147. widget.toggleInput(true);
  148. expect(window.getComputedStyle(input).display).to.not.be('none');
  149. });
  150. });
  151. describe('#dispose()', () => {
  152. it('should dispose of the resources held by the widget', () => {
  153. let widget = new BaseCellWidget(new CellModel());
  154. widget.dispose();
  155. expect(widget.isDisposed).to.be(true);
  156. });
  157. it('should be safe to call multiple times', () => {
  158. let widget = new BaseCellWidget(new CellModel());
  159. widget.dispose();
  160. widget.dispose();
  161. expect(widget.isDisposed).to.be(true);
  162. });
  163. });
  164. describe('#onAfterAttach()', () => {
  165. it('should update the widget', () => {
  166. let widget = new LogCell(new CellModel());
  167. expect(widget.methods).to.not.contain('onAfterAttach');
  168. widget.attach(document.body);
  169. expect(widget.methods).to.contain('onAfterAttach');
  170. widget.dispose();
  171. });
  172. });
  173. });
  174. });