editor.spec.ts 3.9 KB

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