shell.spec.ts 13 KB

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