inputarea.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { expect } from 'chai';
  4. import { Widget } from '@lumino/widgets';
  5. import { CodeEditorWrapper } from '@jupyterlab/codeeditor';
  6. import { InputArea, InputPrompt, CodeCellModel } from '@jupyterlab/cells';
  7. const PROMPT_CLASS = 'jp-InputArea-prompt';
  8. describe('@jupyterlab/cells', () => {
  9. const model = new CodeCellModel({});
  10. describe('InputArea', () => {
  11. describe('#constructor()', () => {
  12. it('should create an input area widget', () => {
  13. const widget = new InputArea({ model });
  14. expect(widget).to.be.an.instanceof(InputArea);
  15. });
  16. });
  17. describe('#model', () => {
  18. it('should be the model used by the input area', () => {
  19. const widget = new InputArea({ model });
  20. expect(widget.model).to.equal(model);
  21. });
  22. });
  23. describe('#contentFactory', () => {
  24. it('should be the content factory used by the input area', () => {
  25. const widget = new InputArea({ model });
  26. expect(widget.contentFactory).to.equal(InputArea.defaultContentFactory);
  27. });
  28. });
  29. describe('#editorWidget', () => {
  30. it('should be the editor widget used by the input area', () => {
  31. const widget = new InputArea({ model });
  32. expect(widget.editorWidget).to.be.an.instanceof(CodeEditorWrapper);
  33. });
  34. });
  35. describe('#editor', () => {
  36. it('should be the code editor used by the cell', () => {
  37. const widget = new InputArea({ model });
  38. expect(widget.editor.host).to.equal(widget.editorWidget.node);
  39. });
  40. });
  41. describe('#promptNode', () => {
  42. it('should be the prompt node used by the cell', () => {
  43. const widget = new InputArea({ model });
  44. expect(widget.promptNode.className).to.contain('jp-InputPrompt');
  45. });
  46. });
  47. describe('#renderInput()', () => {
  48. it('should render the widget', () => {
  49. const widget = new InputArea({ model });
  50. const rendered = new Widget();
  51. Widget.attach(widget, document.body);
  52. widget.renderInput(rendered);
  53. expect(rendered.isAttached).to.equal(true);
  54. widget.dispose();
  55. });
  56. });
  57. describe('#showEditor()', () => {
  58. it('should be called to show the editor', () => {
  59. const widget = new InputArea({ model });
  60. const rendered = new Widget();
  61. Widget.attach(widget, document.body);
  62. widget.renderInput(rendered);
  63. widget.showEditor();
  64. expect(rendered.isAttached).to.equal(false);
  65. widget.dispose();
  66. });
  67. });
  68. describe('#setPrompt()', () => {
  69. it('should change the value of the input prompt', () => {
  70. const widget = new InputArea({ model });
  71. const prompt = widget.node.querySelector(`.${PROMPT_CLASS}`)!;
  72. expect(prompt.textContent).to.be.empty;
  73. widget.setPrompt('foo');
  74. expect(prompt.textContent).to.contain('foo');
  75. });
  76. });
  77. describe('#dispose()', () => {
  78. it('should dispose of the resources used by the widget', () => {
  79. const widget = new InputArea({ model });
  80. widget.dispose();
  81. expect(widget.isDisposed).to.equal(true);
  82. widget.dispose();
  83. expect(widget.isDisposed).to.equal(true);
  84. });
  85. });
  86. describe('.ContentFactory', () => {
  87. describe('#constructor()', () => {
  88. it('should create a new content factory', () => {
  89. const factory = new InputArea.ContentFactory();
  90. expect(factory).to.be.an.instanceof(InputArea.ContentFactory);
  91. });
  92. });
  93. describe('#editorFactory', () => {
  94. it('should be the code editor factory being used', () => {
  95. const factory = new InputArea.ContentFactory();
  96. expect(factory.editorFactory).to.equal(
  97. InputArea.defaultEditorFactory
  98. );
  99. });
  100. });
  101. describe('#createInputPrompt()', () => {
  102. it('should create an input prompt', () => {
  103. const factory = new InputArea.ContentFactory();
  104. expect(factory.createInputPrompt()).to.be.an.instanceof(InputPrompt);
  105. });
  106. });
  107. });
  108. describe('.defaultContentFactory', () => {
  109. it('should be an instance of the content factory', () => {
  110. expect(InputArea.defaultContentFactory).to.be.an.instanceof(
  111. InputArea.ContentFactory
  112. );
  113. });
  114. });
  115. describe('.defaultEditorFactory', () => {
  116. it('should be an editor factory', () => {
  117. const factory = InputArea.defaultEditorFactory;
  118. const host = document.createElement('div');
  119. expect(factory({ host, model }).host).to.equal(host);
  120. });
  121. });
  122. });
  123. describe('InputPrompt', () => {
  124. describe('#constructor()', () => {
  125. it('should create an input prompt', () => {
  126. const widget = new InputPrompt();
  127. expect(widget).to.be.an.instanceof(InputPrompt);
  128. });
  129. });
  130. describe('#executionCount', () => {
  131. it('should be the execution count for the prompt', () => {
  132. const widget = new InputPrompt();
  133. expect(widget.executionCount).to.be.null;
  134. widget.executionCount = '1';
  135. expect(widget.executionCount).to.equal('1');
  136. });
  137. });
  138. });
  139. });