observablestring.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ObservableString } from '@jupyterlab/observables';
  4. describe('@jupyterlab/observables', () => {
  5. describe('ObservableString', () => {
  6. describe('#constructor()', () => {
  7. it('should accept no arguments', () => {
  8. const value = new ObservableString();
  9. expect(value instanceof ObservableString).toBe(true);
  10. });
  11. it('should accept a string argument', () => {
  12. const value = new ObservableString('hello');
  13. expect(value instanceof ObservableString).toBe(true);
  14. });
  15. it('should initialize the string value', () => {
  16. const value = new ObservableString('hello');
  17. expect(value.text).toEqual('hello');
  18. });
  19. });
  20. describe('#type', () => {
  21. it('should return `String`', () => {
  22. const value = new ObservableString();
  23. expect(value.type).toBe('String');
  24. });
  25. });
  26. describe('#changed', () => {
  27. it('should be emitted when the string changes', () => {
  28. let called = false;
  29. const value = new ObservableString();
  30. value.changed.connect(() => {
  31. called = true;
  32. });
  33. value.text = 'change';
  34. expect(called).toBe(true);
  35. });
  36. it('should have value changed args', () => {
  37. let called = false;
  38. const value = new ObservableString();
  39. value.changed.connect((sender, args) => {
  40. expect(sender).toBe(value);
  41. expect(args.type).toBe('set');
  42. expect(args.start).toBe(0);
  43. expect(args.end).toBe(3);
  44. expect(args.value).toBe('new');
  45. called = true;
  46. });
  47. value.text = 'new';
  48. expect(called).toBe(true);
  49. });
  50. });
  51. describe('#isDisposed', () => {
  52. it('should test whether the string is disposed', () => {
  53. const value = new ObservableString();
  54. expect(value.isDisposed).toBe(false);
  55. value.dispose();
  56. expect(value.isDisposed).toBe(true);
  57. });
  58. });
  59. describe('#setter()', () => {
  60. it('should set the item at a specific index', () => {
  61. const value = new ObservableString('old');
  62. value.text = 'new';
  63. expect(value.text).toEqual('new');
  64. });
  65. it('should trigger a changed signal', () => {
  66. let called = false;
  67. const value = new ObservableString('old');
  68. value.changed.connect((sender, args) => {
  69. expect(sender).toBe(value);
  70. expect(args.type).toBe('set');
  71. expect(args.start).toBe(0);
  72. expect(args.end).toBe(3);
  73. expect(args.value).toBe('new');
  74. called = true;
  75. });
  76. value.text = 'new';
  77. expect(called).toBe(true);
  78. });
  79. });
  80. describe('#insert()', () => {
  81. it('should insert an substring into the string at a specific index', () => {
  82. const value = new ObservableString('one three');
  83. value.insert(4, 'two ');
  84. expect(value.text).toEqual('one two three');
  85. });
  86. it('should trigger a changed signal', () => {
  87. let called = false;
  88. const value = new ObservableString('one three');
  89. value.changed.connect((sender, args) => {
  90. expect(sender).toBe(value);
  91. expect(args.type).toBe('insert');
  92. expect(args.start).toBe(4);
  93. expect(args.end).toBe(8);
  94. expect(args.value).toBe('two ');
  95. called = true;
  96. });
  97. value.insert(4, 'two ');
  98. expect(called).toBe(true);
  99. });
  100. });
  101. describe('#remove()', () => {
  102. it('should remove a substring from the string', () => {
  103. const value = new ObservableString('one two two three');
  104. value.remove(4, 8);
  105. expect(value.text).toEqual('one two three');
  106. });
  107. it('should trigger a changed signal', () => {
  108. let called = false;
  109. const value = new ObservableString('one two two three');
  110. value.changed.connect((sender, args) => {
  111. expect(sender).toBe(value);
  112. expect(args.type).toBe('remove');
  113. expect(args.start).toBe(4);
  114. expect(args.end).toBe(8);
  115. expect(args.value).toBe('two ');
  116. called = true;
  117. });
  118. value.remove(4, 8);
  119. expect(called).toBe(true);
  120. });
  121. });
  122. describe('#clear()', () => {
  123. it('should empty the string', () => {
  124. const value = new ObservableString('full');
  125. value.clear();
  126. expect(value.text.length).toBe(0);
  127. expect(value.text).toBe('');
  128. });
  129. it('should trigger a changed signal', () => {
  130. let called = false;
  131. const value = new ObservableString('full');
  132. value.changed.connect((sender, args) => {
  133. expect(sender).toBe(value);
  134. expect(args.type).toBe('set');
  135. expect(args.start).toBe(0);
  136. expect(args.end).toBe(0);
  137. expect(args.value).toBe('');
  138. called = true;
  139. });
  140. value.clear();
  141. expect(called).toBe(true);
  142. });
  143. });
  144. });
  145. });