statusbar.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { Signal } from '@lumino/signaling';
  4. import { Widget } from '@lumino/widgets';
  5. import { StatusBar } from '@jupyterlab/statusbar';
  6. describe('@jupyterlab/statusbar', () => {
  7. describe('StatusBar', () => {
  8. let statusBar: StatusBar;
  9. beforeEach(() => {
  10. statusBar = new StatusBar();
  11. Widget.attach(statusBar, document.body);
  12. });
  13. afterEach(() => {
  14. statusBar.parent = null;
  15. statusBar.dispose();
  16. });
  17. describe('#constructor()', () => {
  18. it('should construct a new status bar', () => {
  19. const statusBar = new StatusBar();
  20. expect(statusBar).toBeInstanceOf(StatusBar);
  21. });
  22. });
  23. describe('#registerStatusItem', () => {
  24. it('should add a new status item to the status bar', () => {
  25. const item = new Widget();
  26. expect(item.isAttached).toBe(false);
  27. statusBar.registerStatusItem('item', { item });
  28. expect(item.isAttached).toBe(true);
  29. expect(statusBar.contains(item)).toBe(true);
  30. });
  31. it('should raise an error if the same key is added twice', () => {
  32. const item1 = new Widget();
  33. const item2 = new Widget();
  34. statusBar.registerStatusItem('item', { item: item1 });
  35. expect(
  36. statusBar.registerStatusItem.bind(statusBar, 'item', { item: item2 })
  37. ).toThrowError();
  38. });
  39. it('should put higher rank left items closer to the middle', () => {
  40. const item1 = new Widget();
  41. const item2 = new Widget();
  42. statusBar.registerStatusItem('item1', {
  43. item: item1,
  44. align: 'left',
  45. rank: 1
  46. });
  47. statusBar.registerStatusItem('item2', {
  48. item: item2,
  49. align: 'left',
  50. rank: 0
  51. });
  52. expect(item2.node.nextSibling).toBe(item1.node);
  53. });
  54. it('should put higher rank right items closer to the middle', () => {
  55. const item1 = new Widget();
  56. const item2 = new Widget();
  57. statusBar.registerStatusItem('item1', {
  58. item: item1,
  59. align: 'right',
  60. rank: 0
  61. });
  62. statusBar.registerStatusItem('item2', {
  63. item: item2,
  64. align: 'right',
  65. rank: 1
  66. });
  67. // Reverse order than what one might expect, as right-to-left
  68. // is set in the styling of the right panel.
  69. expect(item1.node.nextSibling).toBe(item2.node);
  70. });
  71. it('should allow insertion of status items in the middle', () => {
  72. const item = new Widget();
  73. statusBar.registerStatusItem('item', {
  74. item: item,
  75. align: 'middle'
  76. });
  77. expect(item.isAttached).toBe(true);
  78. });
  79. it('should only show if isActive returns true', () => {
  80. const item = new Widget();
  81. statusBar.registerStatusItem('item', {
  82. item,
  83. isActive: () => false
  84. });
  85. expect(item.isHidden).toBe(true);
  86. });
  87. it('should update whether it is shown if activeStateChanged fires', () => {
  88. const item = new Widget();
  89. let active = false;
  90. const isActive = () => active;
  91. const activeStateChanged = new Signal<any, void>({});
  92. statusBar.registerStatusItem('item', {
  93. item,
  94. isActive,
  95. activeStateChanged
  96. });
  97. expect(item.isHidden).toBe(true);
  98. active = true;
  99. activeStateChanged.emit(void 0);
  100. expect(item.isHidden).toBe(false);
  101. });
  102. it('should be removed from the status bar if disposed', () => {
  103. const item = new Widget();
  104. const disposable = statusBar.registerStatusItem('item', { item });
  105. expect(item.isVisible).toBe(true);
  106. disposable.dispose();
  107. expect(item.isVisible).toBe(false);
  108. });
  109. });
  110. describe('#dispose', () => {
  111. it('should dispose of the status bar', () => {
  112. expect(statusBar.isDisposed).toBe(false);
  113. statusBar.dispose();
  114. expect(statusBar.isDisposed).toBe(true);
  115. });
  116. it('should be safe to call more than once', () => {
  117. statusBar.dispose();
  118. expect(statusBar.isDisposed).toBe(true);
  119. statusBar.dispose();
  120. expect(statusBar.isDisposed).toBe(true);
  121. });
  122. it('should dispose of the status items added to it', () => {
  123. const item = new Widget();
  124. statusBar.registerStatusItem('item', { item });
  125. expect(item.isDisposed).toBe(false);
  126. statusBar.dispose();
  127. expect(item.isDisposed).toBe(true);
  128. });
  129. });
  130. });
  131. });