panel-container-spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. const Panel = require('../src/panel');
  3. const PanelContainer = require('../src/panel-container');
  4. describe('PanelContainer', () => {
  5. let container;
  6. class TestPanelItem {}
  7. beforeEach(() => {
  8. container = new PanelContainer({ viewRegistry: atom.views });
  9. });
  10. describe('::addPanel(panel)', () => {
  11. it('emits an onDidAddPanel event with the index the panel was inserted at', () => {
  12. const addPanelSpy = jasmine.createSpy();
  13. container.onDidAddPanel(addPanelSpy);
  14. const panel1 = new Panel({ item: new TestPanelItem() }, atom.views);
  15. container.addPanel(panel1);
  16. expect(addPanelSpy).toHaveBeenCalledWith({ panel: panel1, index: 0 });
  17. const panel2 = new Panel({ item: new TestPanelItem() }, atom.views);
  18. container.addPanel(panel2);
  19. expect(addPanelSpy).toHaveBeenCalledWith({ panel: panel2, index: 1 });
  20. });
  21. });
  22. describe('when a panel is destroyed', () => {
  23. it('emits an onDidRemovePanel event with the index of the removed item', () => {
  24. const removePanelSpy = jasmine.createSpy();
  25. container.onDidRemovePanel(removePanelSpy);
  26. const panel1 = new Panel({ item: new TestPanelItem() }, atom.views);
  27. container.addPanel(panel1);
  28. const panel2 = new Panel({ item: new TestPanelItem() }, atom.views);
  29. container.addPanel(panel2);
  30. expect(removePanelSpy).not.toHaveBeenCalled();
  31. panel2.destroy();
  32. expect(removePanelSpy).toHaveBeenCalledWith({ panel: panel2, index: 1 });
  33. panel1.destroy();
  34. expect(removePanelSpy).toHaveBeenCalledWith({ panel: panel1, index: 0 });
  35. });
  36. });
  37. describe('::destroy()', () => {
  38. it('destroys the container and all of its panels', () => {
  39. const destroyedPanels = [];
  40. const panel1 = new Panel({ item: new TestPanelItem() }, atom.views);
  41. panel1.onDidDestroy(() => {
  42. destroyedPanels.push(panel1);
  43. });
  44. container.addPanel(panel1);
  45. const panel2 = new Panel({ item: new TestPanelItem() }, atom.views);
  46. panel2.onDidDestroy(() => {
  47. destroyedPanels.push(panel2);
  48. });
  49. container.addPanel(panel2);
  50. container.destroy();
  51. expect(container.getPanels().length).toBe(0);
  52. expect(destroyedPanels).toEqual([panel1, panel2]);
  53. });
  54. });
  55. describe('panel priority', () => {
  56. describe('left / top panel container', () => {
  57. let initialPanel;
  58. beforeEach(() => {
  59. // 'left' logic is the same as 'top'
  60. container = new PanelContainer({ location: 'left' });
  61. initialPanel = new Panel({ item: new TestPanelItem() }, atom.views);
  62. container.addPanel(initialPanel);
  63. });
  64. describe('when a panel with low priority is added', () => {
  65. it('is inserted at the beginning of the list', () => {
  66. const addPanelSpy = jasmine.createSpy();
  67. container.onDidAddPanel(addPanelSpy);
  68. const panel = new Panel(
  69. { item: new TestPanelItem(), priority: 0 },
  70. atom.views
  71. );
  72. container.addPanel(panel);
  73. expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 0 });
  74. expect(container.getPanels()[0]).toBe(panel);
  75. });
  76. });
  77. describe('when a panel with priority between two other panels is added', () => {
  78. it('is inserted at the between the two panels', () => {
  79. const addPanelSpy = jasmine.createSpy();
  80. let panel = new Panel(
  81. { item: new TestPanelItem(), priority: 1000 },
  82. atom.views
  83. );
  84. container.addPanel(panel);
  85. container.onDidAddPanel(addPanelSpy);
  86. panel = new Panel(
  87. { item: new TestPanelItem(), priority: 101 },
  88. atom.views
  89. );
  90. container.addPanel(panel);
  91. expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 1 });
  92. expect(container.getPanels()[1]).toBe(panel);
  93. });
  94. });
  95. });
  96. describe('right / bottom panel container', () => {
  97. let initialPanel;
  98. beforeEach(() => {
  99. // 'bottom' logic is the same as 'right'
  100. container = new PanelContainer({ location: 'right' });
  101. initialPanel = new Panel({ item: new TestPanelItem() }, atom.views);
  102. container.addPanel(initialPanel);
  103. });
  104. describe('when a panel with high priority is added', () => {
  105. it('is inserted at the beginning of the list', () => {
  106. const addPanelSpy = jasmine.createSpy();
  107. container.onDidAddPanel(addPanelSpy);
  108. const panel = new Panel(
  109. { item: new TestPanelItem(), priority: 1000 },
  110. atom.views
  111. );
  112. container.addPanel(panel);
  113. expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 0 });
  114. expect(container.getPanels()[0]).toBe(panel);
  115. });
  116. });
  117. describe('when a panel with low priority is added', () => {
  118. it('is inserted at the end of the list', () => {
  119. const addPanelSpy = jasmine.createSpy();
  120. container.onDidAddPanel(addPanelSpy);
  121. const panel = new Panel(
  122. { item: new TestPanelItem(), priority: 0 },
  123. atom.views
  124. );
  125. container.addPanel(panel);
  126. expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 1 });
  127. expect(container.getPanels()[1]).toBe(panel);
  128. });
  129. });
  130. });
  131. });
  132. });