statusbar.spec.ts 4.5 KB

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