shell.spec.ts 15 KB

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