shell.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { framePromise } from '@jupyterlab/testutils';
  5. import { toArray } from '@lumino/algorithm';
  6. import { Message } from '@lumino/messaging';
  7. import { Widget, DockPanel } from '@lumino/widgets';
  8. import { simulate } from 'simulate-event';
  9. import { LabShell } from '@jupyterlab/application';
  10. class ContentWidget extends Widget {
  11. activated = false;
  12. onActivateRequest(msg: Message): void {
  13. this.activated = true;
  14. }
  15. }
  16. describe('LabShell', () => {
  17. let shell: LabShell;
  18. beforeAll(() => {
  19. console.debug(
  20. 'Expecting 5 console errors logged in this suite: "Widgets added to app shell must have unique id property."'
  21. );
  22. });
  23. beforeEach(() => {
  24. shell = new LabShell();
  25. Widget.attach(shell, document.body);
  26. });
  27. afterEach(() => {
  28. shell.dispose();
  29. });
  30. describe('#constructor()', () => {
  31. it('should create a LabShell instance', () => {
  32. expect(shell).toBeInstanceOf(LabShell);
  33. });
  34. });
  35. describe('#leftCollapsed', () => {
  36. it('should return whether the left area is collapsed', () => {
  37. const widget = new Widget();
  38. widget.id = 'foo';
  39. shell.add(widget, 'left');
  40. expect(shell.leftCollapsed).toBe(true);
  41. shell.activateById('foo');
  42. expect(shell.leftCollapsed).toBe(false);
  43. });
  44. });
  45. describe('#rightCollapsed', () => {
  46. it('should return whether the right area is collapsed', () => {
  47. const widget = new Widget();
  48. widget.id = 'foo';
  49. shell.add(widget, 'right');
  50. expect(shell.rightCollapsed).toBe(true);
  51. shell.activateById('foo');
  52. expect(shell.rightCollapsed).toBe(false);
  53. });
  54. });
  55. describe('#currentWidget', () => {
  56. it('should be the current widget in the shell main area', () => {
  57. expect(shell.currentWidget).toBe(null);
  58. const widget = new Widget();
  59. widget.node.tabIndex = -1;
  60. widget.id = 'foo';
  61. shell.add(widget, 'main');
  62. expect(shell.currentWidget).toBe(null);
  63. simulate(widget.node, 'focus');
  64. expect(shell.currentWidget).toBe(widget);
  65. widget.parent = null;
  66. expect(shell.currentWidget).toBe(null);
  67. });
  68. });
  69. describe('#isEmpty()', () => {
  70. it('should test whether the main area is empty', () => {
  71. expect(shell.isEmpty('top')).toBe(true);
  72. const widget = new Widget();
  73. widget.id = 'foo';
  74. shell.add(widget, 'main');
  75. expect(shell.isEmpty('main')).toBe(false);
  76. });
  77. it('should test whether the top area is empty', () => {
  78. expect(shell.isEmpty('top')).toBe(true);
  79. const widget = new Widget();
  80. widget.id = 'foo';
  81. shell.add(widget, 'top');
  82. expect(shell.isEmpty('top')).toBe(false);
  83. });
  84. it('should test whether the left area is empty', () => {
  85. expect(shell.isEmpty('left')).toBe(true);
  86. const widget = new Widget();
  87. widget.id = 'foo';
  88. shell.add(widget, 'left');
  89. expect(shell.isEmpty('left')).toBe(false);
  90. });
  91. it('should test whether the right area is empty', () => {
  92. expect(shell.isEmpty('right')).toBe(true);
  93. const widget = new Widget();
  94. widget.id = 'foo';
  95. shell.add(widget, 'right');
  96. expect(shell.isEmpty('right')).toBe(false);
  97. });
  98. });
  99. describe('#restored', () => {
  100. it('should resolve when the app is restored for the first time', () => {
  101. const state = shell.saveLayout();
  102. const mode: DockPanel.Mode = 'multiple-document';
  103. shell.restoreLayout(mode, state);
  104. return shell.restored;
  105. });
  106. });
  107. describe('#add(widget, "header")', () => {
  108. it('should add a widget to the header', () => {
  109. const widget = new Widget();
  110. widget.id = 'foo';
  111. shell.add(widget, 'header');
  112. expect(shell.isEmpty('header')).toBe(false);
  113. });
  114. it('should be a no-op if the widget has no id', () => {
  115. const widget = new Widget();
  116. shell.add(widget, 'header');
  117. expect(shell.isEmpty('header')).toBe(true);
  118. });
  119. it('should accept options', () => {
  120. const widget = new Widget();
  121. widget.id = 'foo';
  122. shell.add(widget, 'header', { rank: 10 });
  123. expect(shell.isEmpty('header')).toBe(false);
  124. });
  125. });
  126. describe('#add(widget, "top")', () => {
  127. it('should add a widget to the top area', () => {
  128. const widget = new Widget();
  129. widget.id = 'foo';
  130. shell.add(widget, 'top');
  131. expect(shell.isEmpty('top')).toBe(false);
  132. });
  133. it('should be a no-op if the widget has no id', () => {
  134. const widget = new Widget();
  135. shell.add(widget, 'top');
  136. expect(shell.isEmpty('top')).toBe(true);
  137. });
  138. it('should accept options', () => {
  139. const widget = new Widget();
  140. widget.id = 'foo';
  141. shell.add(widget, 'top', { rank: 10 });
  142. expect(shell.isEmpty('top')).toBe(false);
  143. });
  144. it('should add widgets according to their ranks', () => {
  145. const foo = new Widget();
  146. const bar = new Widget();
  147. foo.id = 'foo';
  148. bar.id = 'bar';
  149. shell.add(foo, 'top', { rank: 20 });
  150. shell.add(bar, 'top', { rank: 10 });
  151. expect(toArray(shell.widgets('top'))).toEqual([bar, foo]);
  152. });
  153. });
  154. describe('#add(widget, "left")', () => {
  155. it('should add a widget to the left area', () => {
  156. const widget = new Widget();
  157. widget.id = 'foo';
  158. shell.add(widget, 'left');
  159. expect(shell.isEmpty('left')).toBe(false);
  160. });
  161. it('should be a no-op if the widget has no id', () => {
  162. const widget = new Widget();
  163. shell.add(widget, 'left');
  164. expect(shell.isEmpty('left')).toBe(true);
  165. });
  166. it('should accept options', () => {
  167. const widget = new Widget();
  168. widget.id = 'foo';
  169. shell.add(widget, 'left', { rank: 10 });
  170. expect(shell.isEmpty('left')).toBe(false);
  171. });
  172. });
  173. describe('#add(widget, "right")', () => {
  174. it('should add a widget to the right area', () => {
  175. const widget = new Widget();
  176. widget.id = 'foo';
  177. shell.add(widget, 'right');
  178. expect(shell.isEmpty('right')).toBe(false);
  179. });
  180. it('should be a no-op if the widget has no id', () => {
  181. const widget = new Widget();
  182. shell.add(widget, 'right');
  183. expect(shell.isEmpty('right')).toBe(true);
  184. });
  185. it('should accept options', () => {
  186. const widget = new Widget();
  187. widget.id = 'foo';
  188. shell.add(widget, 'right', { rank: 10 });
  189. expect(shell.isEmpty('right')).toBe(false);
  190. });
  191. });
  192. describe('#add(widget, "main")', () => {
  193. it('should add a widget to the main area', () => {
  194. const widget = new Widget();
  195. widget.id = 'foo';
  196. shell.add(widget, 'main');
  197. expect(shell.isEmpty('main')).toBe(false);
  198. });
  199. it('should be a no-op if the widget has no id', () => {
  200. const widget = new Widget();
  201. shell.add(widget, 'main');
  202. expect(shell.isEmpty('main')).toBe(true);
  203. });
  204. });
  205. describe('#activateById()', () => {
  206. it('should activate a widget in the left area', () => {
  207. const widget = new Widget();
  208. widget.id = 'foo';
  209. shell.add(widget, 'left');
  210. expect(widget.isVisible).toBe(false);
  211. shell.activateById('foo');
  212. expect(widget.isVisible).toBe(true);
  213. });
  214. it('should be a no-op if the widget is not in the left area', () => {
  215. const widget = new Widget();
  216. widget.id = 'foo';
  217. expect(widget.isVisible).toBe(false);
  218. shell.activateById('foo');
  219. expect(widget.isVisible).toBe(false);
  220. });
  221. it('should activate a widget in the right area', () => {
  222. const widget = new Widget();
  223. widget.id = 'foo';
  224. shell.add(widget, 'right');
  225. expect(widget.isVisible).toBe(false);
  226. shell.activateById('foo');
  227. expect(widget.isVisible).toBe(true);
  228. });
  229. it('should be a no-op if the widget is not in the right area', () => {
  230. const widget = new Widget();
  231. widget.id = 'foo';
  232. expect(widget.isVisible).toBe(false);
  233. shell.activateById('foo');
  234. expect(widget.isVisible).toBe(false);
  235. });
  236. it('should activate a widget in the main area', async () => {
  237. const widget = new ContentWidget();
  238. widget.id = 'foo';
  239. shell.add(widget, 'main');
  240. shell.activateById('foo');
  241. await framePromise();
  242. expect(widget.activated).toBe(true);
  243. });
  244. it('should be a no-op if the widget is not in the main area', async () => {
  245. const widget = new ContentWidget();
  246. widget.id = 'foo';
  247. shell.activateById('foo');
  248. await framePromise();
  249. expect(widget.activated).toBe(false);
  250. });
  251. });
  252. describe('#collapseLeft()', () => {
  253. it('should collapse all widgets in the left area', () => {
  254. const widget = new Widget();
  255. widget.id = 'foo';
  256. shell.add(widget, 'left');
  257. shell.activateById('foo');
  258. expect(widget.isVisible).toBe(true);
  259. shell.collapseLeft();
  260. expect(widget.isVisible).toBe(false);
  261. });
  262. });
  263. describe('#collapseRight()', () => {
  264. it('should collapse all widgets in the right area', () => {
  265. const widget = new Widget();
  266. widget.id = 'foo';
  267. shell.add(widget, 'right');
  268. shell.activateById('foo');
  269. expect(widget.isVisible).toBe(true);
  270. shell.collapseRight();
  271. expect(widget.isVisible).toBe(false);
  272. });
  273. });
  274. describe('#expandLeft()', () => {
  275. it('should expand the most recently used widget', () => {
  276. const widget = new Widget();
  277. widget.id = 'foo';
  278. const widget2 = new Widget();
  279. widget2.id = 'bar';
  280. shell.add(widget, 'left', { rank: 10 });
  281. shell.add(widget2, 'left', { rank: 1 });
  282. shell.activateById('foo');
  283. shell.collapseLeft();
  284. expect(widget.isVisible).toBe(false);
  285. shell.expandLeft();
  286. expect(widget.isVisible).toBe(true);
  287. });
  288. it('should expand the first widget if none have been activated', () => {
  289. const widget = new Widget();
  290. widget.id = 'foo';
  291. const widget2 = new Widget();
  292. widget2.id = 'bar';
  293. shell.add(widget, 'left', { rank: 10 });
  294. shell.add(widget2, 'left', { rank: 1 });
  295. expect(widget2.isVisible).toBe(false);
  296. shell.expandLeft();
  297. expect(widget2.isVisible).toBe(true);
  298. });
  299. });
  300. describe('#expandRight()', () => {
  301. it('should expand the most recently used widget', () => {
  302. const widget = new Widget();
  303. widget.id = 'foo';
  304. const widget2 = new Widget();
  305. widget2.id = 'bar';
  306. shell.add(widget, 'right', { rank: 10 });
  307. shell.add(widget2, 'right', { rank: 1 });
  308. shell.activateById('foo');
  309. shell.collapseRight();
  310. expect(widget.isVisible).toBe(false);
  311. shell.expandRight();
  312. expect(widget.isVisible).toBe(true);
  313. });
  314. it('should expand the first widget if none have been activated', () => {
  315. const widget = new Widget();
  316. widget.id = 'foo';
  317. const widget2 = new Widget();
  318. widget2.id = 'bar';
  319. shell.add(widget, 'right', { rank: 10 });
  320. shell.add(widget2, 'right', { rank: 1 });
  321. expect(widget2.isVisible).toBe(false);
  322. shell.expandRight();
  323. expect(widget2.isVisible).toBe(true);
  324. });
  325. });
  326. describe('#closeAll()', () => {
  327. it('should close all of the widgets in the main area', () => {
  328. const foo = new Widget();
  329. foo.id = 'foo';
  330. shell.add(foo, 'main');
  331. const bar = new Widget();
  332. bar.id = 'bar';
  333. shell.add(bar, 'main');
  334. shell.closeAll();
  335. expect(foo.parent).toBe(null);
  336. expect(bar.parent).toBe(null);
  337. });
  338. });
  339. describe('#saveLayout', () => {
  340. it('should save the layout of the shell', () => {
  341. const foo = new Widget();
  342. foo.id = 'foo';
  343. shell.add(foo, 'main');
  344. const state = shell.saveLayout();
  345. shell.activateById('foo');
  346. expect(shell.mode).toBe('multiple-document');
  347. expect(state.mainArea?.currentWidget).toBe(null);
  348. });
  349. });
  350. describe('#restoreLayout', () => {
  351. it('should restore the layout of the shell', () => {
  352. const state = shell.saveLayout();
  353. const mode: DockPanel.Mode = 'multiple-document';
  354. shell.mode = 'single-document';
  355. shell.restoreLayout(mode, state);
  356. expect(shell.mode).toBe('multiple-document');
  357. });
  358. });
  359. describe('#titlePanel', () => {
  360. it('should be hidden in multiple document mode and visible in single document mode', () => {
  361. const widget = new Widget();
  362. widget.id = 'foo';
  363. shell.add(widget, 'right', { rank: 10 });
  364. shell.mode = 'multiple-document';
  365. expect(widget.isVisible).toBe(false);
  366. shell.mode = 'single-document';
  367. expect(widget.isVisible).toBe(false);
  368. });
  369. });
  370. });