editor.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { IObservableString } from '@jupyterlab/observables';
  6. describe('CodeEditor.Model', () => {
  7. let model: CodeEditor.Model;
  8. beforeEach(() => {
  9. model = new CodeEditor.Model();
  10. });
  11. afterEach(() => {
  12. model.dispose();
  13. });
  14. describe('#constructor()', () => {
  15. it('should create a CodeEditor Model', () => {
  16. expect(model).toBeInstanceOf(CodeEditor.Model);
  17. expect(model.value.text).toBe('');
  18. });
  19. it('should create a CodeEditor Model with an initial value', () => {
  20. const other = new CodeEditor.Model({ value: 'Initial text here' });
  21. expect(other).toBeInstanceOf(CodeEditor.Model);
  22. expect(other.value.text).toBe('Initial text here');
  23. other.dispose();
  24. });
  25. it('should create a CodeEditor Model with an initial mimetype', () => {
  26. const other = new CodeEditor.Model({
  27. value: 'import this',
  28. mimeType: 'text/x-python'
  29. });
  30. expect(other).toBeInstanceOf(CodeEditor.Model);
  31. expect(other.mimeType).toBe('text/x-python');
  32. expect(other.value.text).toBe('import this');
  33. other.dispose();
  34. });
  35. });
  36. describe('#mimeTypeChanged', () => {
  37. it('should be emitted when the mime type changes', () => {
  38. let called = false;
  39. model.mimeTypeChanged.connect((sender, args) => {
  40. expect(sender).toBe(model);
  41. expect(args.oldValue).toBe('text/plain');
  42. expect(args.newValue).toBe('text/foo');
  43. called = true;
  44. });
  45. model.mimeType = 'text/foo';
  46. expect(called).toBe(true);
  47. });
  48. });
  49. describe('#value', () => {
  50. it('should be the observable value of the model', () => {
  51. let called = false;
  52. const handler = (
  53. sender: IObservableString,
  54. args: IObservableString.IChangedArgs
  55. ) => {
  56. expect(sender).toBe(model.value);
  57. expect(args.type).toBe('set');
  58. expect(args.value).toBe('foo');
  59. called = true;
  60. };
  61. model.value.changed.connect(handler);
  62. model.value.text = 'foo';
  63. expect(called).toBe(true);
  64. model.value.changed.disconnect(handler);
  65. });
  66. it('should handle an insert', () => {
  67. let called = false;
  68. const handler = (
  69. sender: IObservableString,
  70. args: IObservableString.IChangedArgs
  71. ) => {
  72. expect(args.type).toBe('insert');
  73. expect(args.value).toBe('foo');
  74. called = true;
  75. };
  76. model.value.changed.connect(handler);
  77. model.value.insert(0, 'foo');
  78. expect(called).toBe(true);
  79. model.value.changed.disconnect(handler);
  80. });
  81. it('should handle a remove', () => {
  82. let called = false;
  83. model.value.text = 'foo';
  84. const handler = (
  85. sender: IObservableString,
  86. args: IObservableString.IChangedArgs
  87. ) => {
  88. expect(args.type).toBe('remove');
  89. expect(args.value).toBe('f');
  90. called = true;
  91. };
  92. model.value.changed.connect(handler);
  93. model.value.remove(0, 1);
  94. expect(called).toBe(true);
  95. model.value.changed.disconnect(handler);
  96. });
  97. });
  98. describe('#selections', () => {
  99. it('should be the selections associated with the model', () => {
  100. expect(model.selections.keys().length).toBe(0);
  101. });
  102. });
  103. describe('#mimeType', () => {
  104. it('should be the mime type of the model', () => {
  105. expect(model.mimeType).toBe('text/plain');
  106. model.mimeType = 'text/foo';
  107. expect(model.mimeType).toBe('text/foo');
  108. });
  109. });
  110. describe('#modelDB', () => {
  111. it('should get the modelDB object associated with the model', () => {
  112. expect(model.modelDB.has('value')).toBe(true);
  113. });
  114. });
  115. describe('#isDisposed', () => {
  116. it('should test whether the model is disposed', () => {
  117. expect(model.isDisposed).toBe(false);
  118. model.dispose();
  119. expect(model.isDisposed).toBe(true);
  120. });
  121. });
  122. });