inputarea.spec.ts 5.0 KB

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