shell.spec.ts 12 KB

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