shell.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. expect(shell.isEmpty('top')).toBe(true);
  78. const widget = new Widget();
  79. widget.id = 'foo';
  80. shell.add(widget, 'top');
  81. expect(shell.isEmpty('top')).toBe(false);
  82. });
  83. it('should test whether the left area is empty', () => {
  84. expect(shell.isEmpty('left')).toBe(true);
  85. const widget = new Widget();
  86. widget.id = 'foo';
  87. shell.add(widget, 'left');
  88. expect(shell.isEmpty('left')).toBe(false);
  89. });
  90. it('should test whether the right area is empty', () => {
  91. expect(shell.isEmpty('right')).toBe(true);
  92. const widget = new Widget();
  93. widget.id = 'foo';
  94. shell.add(widget, 'right');
  95. expect(shell.isEmpty('right')).toBe(false);
  96. });
  97. });
  98. describe('#restored', () => {
  99. it('should resolve when the app is restored for the first time', () => {
  100. const state = shell.saveLayout();
  101. const mode: DockPanel.Mode = 'multiple-document';
  102. shell.restoreLayout(mode, state);
  103. return shell.restored;
  104. });
  105. });
  106. describe('#add(widget, "header")', () => {
  107. it('should add a widget to the header', () => {
  108. const widget = new Widget();
  109. widget.id = 'foo';
  110. shell.add(widget, 'header');
  111. expect(shell.isEmpty('header')).toBe(false);
  112. });
  113. it('should be a no-op if the widget has no id', () => {
  114. const widget = new Widget();
  115. shell.add(widget, 'header');
  116. expect(shell.isEmpty('header')).toBe(true);
  117. });
  118. it('should accept options', () => {
  119. const widget = new Widget();
  120. widget.id = 'foo';
  121. shell.add(widget, 'header', { rank: 10 });
  122. expect(shell.isEmpty('header')).toBe(false);
  123. });
  124. });
  125. describe('#add(widget, "top")', () => {
  126. it('should add a widget to the top area', () => {
  127. const widget = new Widget();
  128. widget.id = 'foo';
  129. shell.add(widget, 'top');
  130. // the added widget
  131. console.log(toArray(shell.widgets('top')));
  132. expect(toArray(shell.widgets('top')).length).toEqual(1);
  133. });
  134. it('should be a no-op if the widget has no id', () => {
  135. const widget = new Widget();
  136. shell.add(widget, 'top');
  137. expect(toArray(shell.widgets('top')).length).toEqual(0);
  138. });
  139. it('should accept options', () => {
  140. const widget = new Widget();
  141. widget.id = 'foo';
  142. shell.add(widget, 'top', { rank: 10 });
  143. expect(toArray(shell.widgets('top')).length).toEqual(1);
  144. });
  145. it('should add widgets according to their ranks', () => {
  146. const foo = new Widget();
  147. const bar = new Widget();
  148. foo.id = 'foo';
  149. bar.id = 'bar';
  150. shell.add(foo, 'top', { rank: 20 });
  151. shell.add(bar, 'top', { rank: 10 });
  152. expect(toArray(shell.widgets('top')).slice(0, 2)).toEqual([bar, foo]);
  153. });
  154. });
  155. describe('#add(widget, "left")', () => {
  156. it('should add a widget to the left area', () => {
  157. const widget = new Widget();
  158. widget.id = 'foo';
  159. shell.add(widget, 'left');
  160. expect(shell.isEmpty('left')).toBe(false);
  161. });
  162. it('should be a no-op if the widget has no id', () => {
  163. const widget = new Widget();
  164. shell.add(widget, 'left');
  165. expect(shell.isEmpty('left')).toBe(true);
  166. });
  167. it('should accept options', () => {
  168. const widget = new Widget();
  169. widget.id = 'foo';
  170. shell.add(widget, 'left', { rank: 10 });
  171. expect(shell.isEmpty('left')).toBe(false);
  172. });
  173. });
  174. describe('#add(widget, "right")', () => {
  175. it('should add a widget to the right area', () => {
  176. const widget = new Widget();
  177. widget.id = 'foo';
  178. shell.add(widget, 'right');
  179. expect(shell.isEmpty('right')).toBe(false);
  180. });
  181. it('should be a no-op if the widget has no id', () => {
  182. const widget = new Widget();
  183. shell.add(widget, 'right');
  184. expect(shell.isEmpty('right')).toBe(true);
  185. });
  186. it('should accept options', () => {
  187. const widget = new Widget();
  188. widget.id = 'foo';
  189. shell.add(widget, 'right', { rank: 10 });
  190. expect(shell.isEmpty('right')).toBe(false);
  191. });
  192. });
  193. describe('#add(widget, "main")', () => {
  194. it('should add a widget to the main area', () => {
  195. const widget = new Widget();
  196. widget.id = 'foo';
  197. shell.add(widget, 'main');
  198. expect(shell.isEmpty('main')).toBe(false);
  199. });
  200. it('should be a no-op if the widget has no id', () => {
  201. const widget = new Widget();
  202. shell.add(widget, 'main');
  203. expect(shell.isEmpty('main')).toBe(true);
  204. });
  205. });
  206. describe('#activateById()', () => {
  207. it('should activate a widget in the left area', () => {
  208. const widget = new Widget();
  209. widget.id = 'foo';
  210. shell.add(widget, 'left');
  211. expect(widget.isVisible).toBe(false);
  212. shell.activateById('foo');
  213. expect(widget.isVisible).toBe(true);
  214. });
  215. it('should be a no-op if the widget is not in the left area', () => {
  216. const widget = new Widget();
  217. widget.id = 'foo';
  218. expect(widget.isVisible).toBe(false);
  219. shell.activateById('foo');
  220. expect(widget.isVisible).toBe(false);
  221. });
  222. it('should activate a widget in the right area', () => {
  223. const widget = new Widget();
  224. widget.id = 'foo';
  225. shell.add(widget, 'right');
  226. expect(widget.isVisible).toBe(false);
  227. shell.activateById('foo');
  228. expect(widget.isVisible).toBe(true);
  229. });
  230. it('should be a no-op if the widget is not in the right area', () => {
  231. const widget = new Widget();
  232. widget.id = 'foo';
  233. expect(widget.isVisible).toBe(false);
  234. shell.activateById('foo');
  235. expect(widget.isVisible).toBe(false);
  236. });
  237. it('should activate a widget in the main area', async () => {
  238. const widget = new ContentWidget();
  239. widget.id = 'foo';
  240. shell.add(widget, 'main');
  241. shell.activateById('foo');
  242. await framePromise();
  243. expect(widget.activated).toBe(true);
  244. });
  245. it('should be a no-op if the widget is not in the main area', async () => {
  246. const widget = new ContentWidget();
  247. widget.id = 'foo';
  248. shell.activateById('foo');
  249. await framePromise();
  250. expect(widget.activated).toBe(false);
  251. });
  252. });
  253. describe('#collapseLeft()', () => {
  254. it('should collapse all widgets in the left area', () => {
  255. const widget = new Widget();
  256. widget.id = 'foo';
  257. shell.add(widget, 'left');
  258. shell.activateById('foo');
  259. expect(widget.isVisible).toBe(true);
  260. shell.collapseLeft();
  261. expect(widget.isVisible).toBe(false);
  262. });
  263. });
  264. describe('#collapseRight()', () => {
  265. it('should collapse all widgets in the right area', () => {
  266. const widget = new Widget();
  267. widget.id = 'foo';
  268. shell.add(widget, 'right');
  269. shell.activateById('foo');
  270. expect(widget.isVisible).toBe(true);
  271. shell.collapseRight();
  272. expect(widget.isVisible).toBe(false);
  273. });
  274. });
  275. describe('#expandLeft()', () => {
  276. it('should expand the most recently used widget', () => {
  277. const widget = new Widget();
  278. widget.id = 'foo';
  279. const widget2 = new Widget();
  280. widget2.id = 'bar';
  281. shell.add(widget, 'left', { rank: 10 });
  282. shell.add(widget2, 'left', { rank: 1 });
  283. shell.activateById('foo');
  284. shell.collapseLeft();
  285. expect(widget.isVisible).toBe(false);
  286. shell.expandLeft();
  287. expect(widget.isVisible).toBe(true);
  288. });
  289. it('should expand the first widget if none have been activated', () => {
  290. const widget = new Widget();
  291. widget.id = 'foo';
  292. const widget2 = new Widget();
  293. widget2.id = 'bar';
  294. shell.add(widget, 'left', { rank: 10 });
  295. shell.add(widget2, 'left', { rank: 1 });
  296. expect(widget2.isVisible).toBe(false);
  297. shell.expandLeft();
  298. expect(widget2.isVisible).toBe(true);
  299. });
  300. });
  301. describe('#expandRight()', () => {
  302. it('should expand the most recently used widget', () => {
  303. const widget = new Widget();
  304. widget.id = 'foo';
  305. const widget2 = new Widget();
  306. widget2.id = 'bar';
  307. shell.add(widget, 'right', { rank: 10 });
  308. shell.add(widget2, 'right', { rank: 1 });
  309. shell.activateById('foo');
  310. shell.collapseRight();
  311. expect(widget.isVisible).toBe(false);
  312. shell.expandRight();
  313. expect(widget.isVisible).toBe(true);
  314. });
  315. it('should expand the first widget if none have been activated', () => {
  316. const widget = new Widget();
  317. widget.id = 'foo';
  318. const widget2 = new Widget();
  319. widget2.id = 'bar';
  320. shell.add(widget, 'right', { rank: 10 });
  321. shell.add(widget2, 'right', { rank: 1 });
  322. expect(widget2.isVisible).toBe(false);
  323. shell.expandRight();
  324. expect(widget2.isVisible).toBe(true);
  325. });
  326. });
  327. describe('#closeAll()', () => {
  328. it('should close all of the widgets in the main area', () => {
  329. const foo = new Widget();
  330. foo.id = 'foo';
  331. shell.add(foo, 'main');
  332. const bar = new Widget();
  333. bar.id = 'bar';
  334. shell.add(bar, 'main');
  335. shell.closeAll();
  336. expect(foo.parent).toBe(null);
  337. expect(bar.parent).toBe(null);
  338. });
  339. });
  340. describe('#saveLayout', () => {
  341. it('should save the layout of the shell', () => {
  342. const foo = new Widget();
  343. foo.id = 'foo';
  344. shell.add(foo, 'main');
  345. const state = shell.saveLayout();
  346. shell.activateById('foo');
  347. expect(shell.mode).toBe('multiple-document');
  348. expect(state.mainArea?.currentWidget).toBe(null);
  349. });
  350. });
  351. describe('#restoreLayout', () => {
  352. it('should restore the layout of the shell', () => {
  353. const state = shell.saveLayout();
  354. const mode: DockPanel.Mode = 'multiple-document';
  355. shell.mode = 'single-document';
  356. shell.restoreLayout(mode, state);
  357. expect(shell.mode).toBe('multiple-document');
  358. });
  359. });
  360. describe('#titlePanel', () => {
  361. it('should be hidden in multiple document mode and visible in single document mode', () => {
  362. const widget = new Widget();
  363. widget.id = 'foo';
  364. shell.add(widget, 'right', { rank: 10 });
  365. shell.mode = 'multiple-document';
  366. expect(widget.isVisible).toBe(false);
  367. shell.mode = 'single-document';
  368. expect(widget.isVisible).toBe(false);
  369. });
  370. });
  371. });