shell.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 } 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. shell.restoreLayout(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(state.mainArea?.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. shell.mode = 'single-document';
  353. shell.restoreLayout(state);
  354. expect(state.mainArea?.mode).toBe('multiple-document');
  355. });
  356. });
  357. describe('#titlePanel', () => {
  358. it('should be hidden in multiple document mode and visible in single document mode', () => {
  359. const widget = new Widget();
  360. widget.id = 'foo';
  361. shell.add(widget, 'right', { rank: 10 });
  362. shell.mode = 'multiple-document';
  363. expect(widget.isVisible).toBe(false);
  364. shell.mode = 'single-document';
  365. expect(widget.isVisible).toBe(false);
  366. });
  367. });
  368. });