decoration-manager-spec.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const DecorationManager = require('../src/decoration-manager');
  2. const TextEditor = require('../src/text-editor');
  3. describe('DecorationManager', function() {
  4. let [decorationManager, buffer, editor, markerLayer1, markerLayer2] = [];
  5. beforeEach(function() {
  6. buffer = atom.project.bufferForPathSync('sample.js');
  7. editor = new TextEditor({ buffer });
  8. markerLayer1 = editor.addMarkerLayer();
  9. markerLayer2 = editor.addMarkerLayer();
  10. decorationManager = new DecorationManager(editor);
  11. waitsForPromise(() => atom.packages.activatePackage('language-javascript'));
  12. });
  13. afterEach(() => buffer.destroy());
  14. describe('decorations', function() {
  15. let [
  16. layer1Marker,
  17. layer2Marker,
  18. layer1MarkerDecoration,
  19. layer2MarkerDecoration,
  20. decorationProperties
  21. ] = [];
  22. beforeEach(function() {
  23. layer1Marker = markerLayer1.markBufferRange([[2, 13], [3, 15]]);
  24. decorationProperties = { type: 'line-number', class: 'one' };
  25. layer1MarkerDecoration = decorationManager.decorateMarker(
  26. layer1Marker,
  27. decorationProperties
  28. );
  29. layer2Marker = markerLayer2.markBufferRange([[2, 13], [3, 15]]);
  30. layer2MarkerDecoration = decorationManager.decorateMarker(
  31. layer2Marker,
  32. decorationProperties
  33. );
  34. });
  35. it('can add decorations associated with markers and remove them', function() {
  36. expect(layer1MarkerDecoration).toBeDefined();
  37. expect(layer1MarkerDecoration.getProperties()).toBe(decorationProperties);
  38. expect(decorationManager.decorationsForScreenRowRange(2, 3)).toEqual({
  39. [layer1Marker.id]: [layer1MarkerDecoration],
  40. [layer2Marker.id]: [layer2MarkerDecoration]
  41. });
  42. layer1MarkerDecoration.destroy();
  43. expect(
  44. decorationManager.decorationsForScreenRowRange(2, 3)[layer1Marker.id]
  45. ).not.toBeDefined();
  46. layer2MarkerDecoration.destroy();
  47. expect(
  48. decorationManager.decorationsForScreenRowRange(2, 3)[layer2Marker.id]
  49. ).not.toBeDefined();
  50. });
  51. it('will not fail if the decoration is removed twice', function() {
  52. layer1MarkerDecoration.destroy();
  53. layer1MarkerDecoration.destroy();
  54. });
  55. it('does not allow destroyed markers to be decorated', function() {
  56. layer1Marker.destroy();
  57. expect(() =>
  58. decorationManager.decorateMarker(layer1Marker, {
  59. type: 'overlay',
  60. item: document.createElement('div')
  61. })
  62. ).toThrow('Cannot decorate a destroyed marker');
  63. expect(decorationManager.getOverlayDecorations()).toEqual([]);
  64. });
  65. it('does not allow destroyed marker layers to be decorated', function() {
  66. const layer = editor.addMarkerLayer();
  67. layer.destroy();
  68. expect(() =>
  69. decorationManager.decorateMarkerLayer(layer, { type: 'highlight' })
  70. ).toThrow('Cannot decorate a destroyed marker layer');
  71. });
  72. describe('when a decoration is updated via Decoration::update()', () =>
  73. it("emits an 'updated' event containing the new and old params", function() {
  74. let updatedSpy;
  75. layer1MarkerDecoration.onDidChangeProperties(
  76. (updatedSpy = jasmine.createSpy())
  77. );
  78. layer1MarkerDecoration.setProperties({
  79. type: 'line-number',
  80. class: 'two'
  81. });
  82. const {
  83. oldProperties,
  84. newProperties
  85. } = updatedSpy.mostRecentCall.args[0];
  86. expect(oldProperties).toEqual(decorationProperties);
  87. expect(newProperties.type).toBe('line-number');
  88. expect(newProperties.gutterName).toBe('line-number');
  89. expect(newProperties.class).toBe('two');
  90. }));
  91. describe('::getDecorations(properties)', () =>
  92. it('returns decorations matching the given optional properties', function() {
  93. expect(decorationManager.getDecorations()).toEqual([
  94. layer1MarkerDecoration,
  95. layer2MarkerDecoration
  96. ]);
  97. expect(
  98. decorationManager.getDecorations({ class: 'two' }).length
  99. ).toEqual(0);
  100. expect(
  101. decorationManager.getDecorations({ class: 'one' }).length
  102. ).toEqual(2);
  103. }));
  104. });
  105. describe('::decorateMarker', () =>
  106. describe('when decorating gutters', function() {
  107. let [layer1Marker] = [];
  108. beforeEach(
  109. () => (layer1Marker = markerLayer1.markBufferRange([[1, 0], [1, 0]]))
  110. );
  111. it("creates a decoration that is both of 'line-number' and 'gutter' type when called with the 'line-number' type", function() {
  112. const decorationProperties = { type: 'line-number', class: 'one' };
  113. const layer1MarkerDecoration = decorationManager.decorateMarker(
  114. layer1Marker,
  115. decorationProperties
  116. );
  117. expect(layer1MarkerDecoration.isType('line-number')).toBe(true);
  118. expect(layer1MarkerDecoration.isType('gutter')).toBe(true);
  119. expect(layer1MarkerDecoration.getProperties().gutterName).toBe(
  120. 'line-number'
  121. );
  122. expect(layer1MarkerDecoration.getProperties().class).toBe('one');
  123. });
  124. it("creates a decoration that is only of 'gutter' type if called with the 'gutter' type and a 'gutterName'", function() {
  125. const decorationProperties = {
  126. type: 'gutter',
  127. gutterName: 'test-gutter',
  128. class: 'one'
  129. };
  130. const layer1MarkerDecoration = decorationManager.decorateMarker(
  131. layer1Marker,
  132. decorationProperties
  133. );
  134. expect(layer1MarkerDecoration.isType('gutter')).toBe(true);
  135. expect(layer1MarkerDecoration.isType('line-number')).toBe(false);
  136. expect(layer1MarkerDecoration.getProperties().gutterName).toBe(
  137. 'test-gutter'
  138. );
  139. expect(layer1MarkerDecoration.getProperties().class).toBe('one');
  140. });
  141. }));
  142. });