panel-spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const Panel = require('../src/panel');
  2. describe('Panel', () => {
  3. class TestPanelItem {
  4. getElement() {
  5. if (!this.element) {
  6. this.element = document.createElement('div');
  7. this.element.tabIndex = -1;
  8. this.element.className = 'test-root';
  9. }
  10. return this.element;
  11. }
  12. }
  13. it("adds the item's element as a child of the panel", () => {
  14. const panel = new Panel({ item: new TestPanelItem() }, atom.views);
  15. const element = panel.getElement();
  16. expect(element.tagName.toLowerCase()).toBe('atom-panel');
  17. expect(element.firstChild).toBe(panel.getItem().getElement());
  18. });
  19. describe('destroying the panel', () => {
  20. it('removes the element when the panel is destroyed', () => {
  21. const panel = new Panel({ item: new TestPanelItem() }, atom.views);
  22. const element = panel.getElement();
  23. const jasmineContent = document.getElementById('jasmine-content');
  24. jasmineContent.appendChild(element);
  25. expect(element.parentNode).toBe(jasmineContent);
  26. panel.destroy();
  27. expect(element.parentNode).not.toBe(jasmineContent);
  28. });
  29. it('does not try to remove the element twice', () => {
  30. const item = new TestPanelItem();
  31. const panel = new Panel({ item }, atom.views);
  32. const element = panel.getElement();
  33. const jasmineContent = document.getElementById('jasmine-content');
  34. jasmineContent.appendChild(element);
  35. item.getElement().focus();
  36. expect(item.getElement()).toHaveFocus();
  37. // Avoid this error:
  38. // NotFoundError: Failed to execute 'remove' on 'Element':
  39. // The node to be removed is no longer a child of this node.
  40. // Perhaps it was moved in a 'blur' event handler?
  41. item.getElement().addEventListener('blur', () => panel.destroy());
  42. panel.destroy();
  43. });
  44. });
  45. describe('changing panel visibility', () => {
  46. it('notifies observers added with onDidChangeVisible', () => {
  47. const panel = new Panel({ item: new TestPanelItem() }, atom.views);
  48. const spy = jasmine.createSpy();
  49. panel.onDidChangeVisible(spy);
  50. panel.hide();
  51. expect(panel.isVisible()).toBe(false);
  52. expect(spy).toHaveBeenCalledWith(false);
  53. spy.reset();
  54. panel.show();
  55. expect(panel.isVisible()).toBe(true);
  56. expect(spy).toHaveBeenCalledWith(true);
  57. panel.destroy();
  58. expect(panel.isVisible()).toBe(false);
  59. expect(spy).toHaveBeenCalledWith(false);
  60. });
  61. it('initially renders panel created with visible: false', () => {
  62. const panel = new Panel(
  63. { visible: false, item: new TestPanelItem() },
  64. atom.views
  65. );
  66. const element = panel.getElement();
  67. expect(element.style.display).toBe('none');
  68. });
  69. it('hides and shows the panel element when Panel::hide() and Panel::show() are called', () => {
  70. const panel = new Panel({ item: new TestPanelItem() }, atom.views);
  71. const element = panel.getElement();
  72. expect(element.style.display).not.toBe('none');
  73. panel.hide();
  74. expect(element.style.display).toBe('none');
  75. panel.show();
  76. expect(element.style.display).not.toBe('none');
  77. });
  78. });
  79. describe('when a class name is specified', () => {
  80. it('initially renders panel created with visible: false', () => {
  81. const panel = new Panel(
  82. { className: 'some classes', item: new TestPanelItem() },
  83. atom.views
  84. );
  85. const element = panel.getElement();
  86. expect(element).toHaveClass('some');
  87. expect(element).toHaveClass('classes');
  88. });
  89. });
  90. describe('creating an atom-panel via markup', () => {
  91. it('does not throw an error', () => {
  92. document.createElement('atom-panel');
  93. });
  94. });
  95. });