inputarea.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  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).toBeInstanceOf(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).toBe(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).toBe(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).toBeInstanceOf(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).toBe(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).toContain('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).toBe(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).toBe(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).toHaveLength(0);
  73. widget.setPrompt('foo');
  74. expect(prompt.textContent).toContain('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).toBe(true);
  82. widget.dispose();
  83. expect(widget.isDisposed).toBe(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).toBeInstanceOf(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).toBe(InputArea.defaultEditorFactory);
  97. });
  98. });
  99. describe('#createInputPrompt()', () => {
  100. it('should create an input prompt', () => {
  101. const factory = new InputArea.ContentFactory();
  102. expect(factory.createInputPrompt()).toBeInstanceOf(InputPrompt);
  103. });
  104. });
  105. });
  106. describe('.defaultContentFactory', () => {
  107. it('should be an instance of the content factory', () => {
  108. expect(InputArea.defaultContentFactory).toBeInstanceOf(
  109. InputArea.ContentFactory
  110. );
  111. });
  112. });
  113. describe('.defaultEditorFactory', () => {
  114. it('should be an editor factory', () => {
  115. const factory = InputArea.defaultEditorFactory;
  116. const host = document.createElement('div');
  117. expect(factory({ host, model }).host).toBe(host);
  118. });
  119. });
  120. });
  121. describe('InputPrompt', () => {
  122. describe('#constructor()', () => {
  123. it('should create an input prompt', () => {
  124. const widget = new InputPrompt();
  125. expect(widget).toBeInstanceOf(InputPrompt);
  126. });
  127. });
  128. describe('#executionCount', () => {
  129. it('should be the execution count for the prompt', () => {
  130. const widget = new InputPrompt();
  131. expect(widget.executionCount).toBeNull();
  132. widget.executionCount = '1';
  133. expect(widget.executionCount).toBe('1');
  134. });
  135. });
  136. });
  137. });