factory.spec.ts 3.0 KB

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