factory.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { CodeEditor } from '@jupyterlab/codeeditor';
  5. import {
  6. CodeMirrorEditorFactory,
  7. CodeMirrorEditor
  8. } from '@jupyterlab/codemirror';
  9. class ExposeCodeMirrorEditorFactory extends CodeMirrorEditorFactory {
  10. public inlineCodeMirrorConfig: CodeMirrorEditor.IConfig;
  11. public documentCodeMirrorConfig: CodeMirrorEditor.IConfig;
  12. }
  13. describe('CodeMirrorEditorFactory', () => {
  14. let host: HTMLElement;
  15. let model: CodeEditor.IModel;
  16. const options: Partial<CodeMirrorEditor.IConfig> = {
  17. lineNumbers: false,
  18. lineWrap: 'on',
  19. extraKeys: {
  20. 'Ctrl-Tab': 'indentAuto'
  21. }
  22. };
  23. beforeEach(() => {
  24. host = document.createElement('div');
  25. document.body.appendChild(host);
  26. model = new CodeEditor.Model();
  27. });
  28. afterEach(() => {
  29. document.body.removeChild(host);
  30. });
  31. describe('#constructor()', () => {
  32. it('should create a CodeMirrorEditorFactory', () => {
  33. const factory = new CodeMirrorEditorFactory();
  34. expect(factory).toBeInstanceOf(CodeMirrorEditorFactory);
  35. });
  36. it('should create a CodeMirrorEditorFactory with options', () => {
  37. const factory = new ExposeCodeMirrorEditorFactory(options);
  38. expect(factory).toBeInstanceOf(CodeMirrorEditorFactory);
  39. expect(factory.inlineCodeMirrorConfig.extraKeys).toEqual(
  40. options.extraKeys
  41. );
  42. expect(factory.documentCodeMirrorConfig.extraKeys).toEqual(
  43. options.extraKeys
  44. );
  45. });
  46. });
  47. describe('#newInlineEditor', () => {
  48. it('should create a new editor', () => {
  49. const factory = new CodeMirrorEditorFactory();
  50. const editor = factory.newInlineEditor({ host, model });
  51. expect(editor).toBeInstanceOf(CodeMirrorEditor);
  52. editor.dispose();
  53. });
  54. it('should create a new editor with given options', () => {
  55. const factory = new CodeMirrorEditorFactory(options);
  56. const editor = factory.newInlineEditor({
  57. host,
  58. model
  59. }) as CodeMirrorEditor;
  60. expect(editor).toBeInstanceOf(CodeMirrorEditor);
  61. for (const key in Object.keys(options)) {
  62. const option = key as keyof CodeMirrorEditor.IConfig;
  63. expect(editor.getOption(option)).toBe(options[option]);
  64. }
  65. editor.dispose();
  66. });
  67. });
  68. describe('#newDocumentEditor', () => {
  69. it('should create a new editor', () => {
  70. const factory = new CodeMirrorEditorFactory();
  71. const editor = factory.newDocumentEditor({ host, model });
  72. expect(editor).toBeInstanceOf(CodeMirrorEditor);
  73. editor.dispose();
  74. });
  75. it('should create a new editor with given options', () => {
  76. const factory = new CodeMirrorEditorFactory(options);
  77. const editor = factory.newDocumentEditor({
  78. host,
  79. model
  80. }) as CodeMirrorEditor;
  81. expect(editor).toBeInstanceOf(CodeMirrorEditor);
  82. for (const key in Object.keys(options)) {
  83. const option = key as keyof CodeMirrorEditor.IConfig;
  84. expect(editor.getOption(option)).toBe(options[option]);
  85. }
  86. editor.dispose();
  87. });
  88. });
  89. });