shell.spec.ts 15 KB

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