dialog.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // Copyright (c) Jupyter Development Team.
  2. import 'jest';
  3. // Distributed under the terms of the Modified BSD License.
  4. // import { expect } from 'chai';
  5. import { Dialog, showDialog } from '@jupyterlab/apputils';
  6. import { each } from '@lumino/algorithm';
  7. import { Message } from '@lumino/messaging';
  8. import { Widget } from '@lumino/widgets';
  9. import { generate, simulate } from 'simulate-event';
  10. import * as React from 'react';
  11. import {
  12. acceptDialog,
  13. dismissDialog,
  14. waitForDialog
  15. } from '@jupyterlab/testutils';
  16. class TestDialog extends Dialog<any> {
  17. methods: string[] = [];
  18. events: string[] = [];
  19. handleEvent(event: Event): void {
  20. super.handleEvent(event);
  21. this.events.push(event.type);
  22. }
  23. protected onAfterAttach(msg: Message): void {
  24. super.onAfterAttach(msg);
  25. this.methods.push('onAfterAttach');
  26. }
  27. protected onAfterDetach(msg: Message): void {
  28. super.onAfterDetach(msg);
  29. this.methods.push('onAfterDetach');
  30. }
  31. protected onCloseRequest(msg: Message): void {
  32. super.onCloseRequest(msg);
  33. this.methods.push('onCloseRequest');
  34. }
  35. }
  36. class ValueWidget extends Widget {
  37. getValue(): string {
  38. return 'foo';
  39. }
  40. }
  41. describe('@jupyterlab/apputils', () => {
  42. describe('Dialog', () => {
  43. let dialog: TestDialog;
  44. beforeEach(() => {
  45. dialog = new TestDialog();
  46. });
  47. afterEach(() => {
  48. dialog.dispose();
  49. });
  50. describe('#constructor()', () => {
  51. it('should create a new dialog', () => {
  52. expect(dialog).toBeInstanceOf(Dialog);
  53. });
  54. it('should accept options', () => {
  55. const dialog = new TestDialog({
  56. title: 'foo',
  57. body: 'Hello',
  58. buttons: [Dialog.okButton()]
  59. });
  60. expect(dialog).toBeInstanceOf(Dialog);
  61. dialog.dispose();
  62. });
  63. });
  64. describe('#launch()', () => {
  65. it.skip('should attach the dialog to the host', async () => {
  66. const host = document.createElement('div');
  67. const dialog = new TestDialog({ host });
  68. document.body.appendChild(host);
  69. void dialog.launch();
  70. await waitForDialog(host);
  71. expect(host.contains(dialog.node)).toBe(true);
  72. dialog.dispose();
  73. document.body.removeChild(host);
  74. });
  75. it('should resolve with `true` when accepted', async () => {
  76. const prompt = dialog.launch();
  77. await waitForDialog();
  78. dialog.resolve();
  79. expect((await prompt).button.accept).toBe(true);
  80. });
  81. it('should resolve with `false` when rejected', async () => {
  82. const prompt = dialog.launch();
  83. await waitForDialog();
  84. dialog.reject();
  85. expect((await prompt).button.accept).toBe(false);
  86. });
  87. it('should resolve with `false` when closed', async () => {
  88. const prompt = dialog.launch();
  89. await waitForDialog();
  90. dialog.close();
  91. expect((await prompt).button.accept).toBe(false);
  92. });
  93. it('should return focus to the original focused element', async () => {
  94. const input = document.createElement('input');
  95. document.body.appendChild(input);
  96. input.focus();
  97. expect(document.activeElement).toBe(input);
  98. const prompt = dialog.launch();
  99. await waitForDialog();
  100. expect(document.activeElement).not.toBe(input);
  101. dialog.resolve();
  102. await prompt;
  103. expect(document.activeElement).toBe(input);
  104. document.body.removeChild(input);
  105. });
  106. });
  107. describe('#resolve()', () => {
  108. it('should resolve with the default item', async () => {
  109. const prompt = dialog.launch();
  110. await waitForDialog();
  111. dialog.resolve();
  112. expect((await prompt).button.accept).toBe(true);
  113. });
  114. it('should resolve with the item at the given index', async () => {
  115. const prompt = dialog.launch();
  116. await waitForDialog();
  117. dialog.resolve(0);
  118. expect((await prompt).button.accept).toBe(false);
  119. });
  120. });
  121. describe('#reject()', () => {
  122. it('should reject with the default reject item', async () => {
  123. const prompt = dialog.launch();
  124. await waitForDialog();
  125. dialog.reject();
  126. const result = await prompt;
  127. expect(result.button.label).toBe('Cancel');
  128. expect(result.button.accept).toBe(false);
  129. });
  130. });
  131. describe('#handleEvent()', () => {
  132. describe('keydown', () => {
  133. it('should reject on escape key', async () => {
  134. const prompt = dialog.launch();
  135. await waitForDialog();
  136. simulate(dialog.node, 'keydown', { keyCode: 27 });
  137. expect((await prompt).button.accept).toBe(false);
  138. });
  139. it('should accept on enter key', async () => {
  140. const prompt = dialog.launch();
  141. await waitForDialog();
  142. simulate(dialog.node, 'keydown', { keyCode: 13 });
  143. expect((await prompt).button.accept).toBe(true);
  144. });
  145. it('should cycle to the first button on a tab key', async () => {
  146. const prompt = dialog.launch();
  147. await waitForDialog();
  148. expect(document.activeElement!.className).toContain('jp-mod-accept');
  149. simulate(dialog.node, 'keydown', { keyCode: 9 });
  150. expect(document.activeElement!.className).toContain('jp-mod-reject');
  151. simulate(document.activeElement!, 'click');
  152. expect((await prompt).button.accept).toBe(false);
  153. });
  154. });
  155. describe('contextmenu', () => {
  156. it('should cancel context menu events', async () => {
  157. const prompt = dialog.launch();
  158. await waitForDialog();
  159. const canceled = !dialog.node.dispatchEvent(generate('contextmenu'));
  160. expect(canceled).toBe(true);
  161. simulate(dialog.node, 'keydown', { keyCode: 27 });
  162. expect((await prompt).button.accept).toBe(false);
  163. });
  164. });
  165. describe('click', () => {
  166. it('should prevent clicking outside of the content area', async () => {
  167. const prompt = dialog.launch();
  168. await waitForDialog();
  169. const canceled = !dialog.node.dispatchEvent(generate('click'));
  170. expect(canceled).toBe(true);
  171. dialog.resolve();
  172. await prompt;
  173. });
  174. it('should resolve a clicked button', async () => {
  175. const prompt = dialog.launch();
  176. await waitForDialog();
  177. simulate(dialog.node.querySelector('.jp-mod-reject')!, 'click');
  178. expect((await prompt).button.accept).toBe(false);
  179. });
  180. });
  181. describe('focus', () => {
  182. it('should focus the default button when focus leaves the dialog', async () => {
  183. const host = document.createElement('div');
  184. const target = document.createElement('div');
  185. const dialog = new TestDialog({ host });
  186. target.tabIndex = -1;
  187. document.body.appendChild(target);
  188. document.body.appendChild(host);
  189. target.focus();
  190. expect(document.activeElement).toBe(target);
  191. const prompt = dialog.launch();
  192. await waitForDialog();
  193. simulate(target, 'focus');
  194. expect(document.activeElement).not.toBe(target);
  195. expect(document.activeElement!.className).toContain('jp-mod-accept');
  196. dialog.resolve();
  197. await prompt;
  198. dialog.dispose();
  199. });
  200. });
  201. });
  202. describe('#onAfterAttach()', () => {
  203. it('should attach event listeners', () => {
  204. Widget.attach(dialog, document.body);
  205. expect(dialog.methods).toContain('onAfterAttach');
  206. ['keydown', 'contextmenu', 'click', 'focus'].forEach(event => {
  207. simulate(dialog.node, event);
  208. expect(dialog.events).toContain(event);
  209. });
  210. });
  211. it('should focus the default button', () => {
  212. Widget.attach(dialog, document.body);
  213. expect(document.activeElement!.className).toContain('jp-mod-accept');
  214. });
  215. it('should focus the primary element', () => {
  216. const body = (
  217. <div>
  218. <input type={'text'} />
  219. </div>
  220. );
  221. const dialog = new TestDialog({ body, focusNodeSelector: 'input' });
  222. Widget.attach(dialog, document.body);
  223. expect(document.activeElement!.localName).toBe('input');
  224. dialog.dispose();
  225. });
  226. });
  227. describe('#onAfterDetach()', () => {
  228. it('should remove event listeners', () => {
  229. Widget.attach(dialog, document.body);
  230. Widget.detach(dialog);
  231. expect(dialog.methods).toContain('onAfterDetach');
  232. dialog.events = [];
  233. ['keydown', 'contextmenu', 'click', 'focus'].forEach(event => {
  234. simulate(dialog.node, event);
  235. expect(dialog.events).not.toContain(event);
  236. });
  237. });
  238. it('should return focus to the original focused element', () => {
  239. const input = document.createElement('input');
  240. document.body.appendChild(input);
  241. input.focus();
  242. Widget.attach(dialog, document.body);
  243. Widget.detach(dialog);
  244. expect(document.activeElement).toBe(input);
  245. document.body.removeChild(input);
  246. });
  247. });
  248. describe('#onCloseRequest()', () => {
  249. it('should reject an existing promise', async () => {
  250. const prompt = dialog.launch();
  251. await waitForDialog();
  252. dialog.close();
  253. expect((await prompt).button.accept).toBe(false);
  254. });
  255. });
  256. describe('.defaultRenderer', () => {
  257. it('should be an instance of a Renderer', () => {
  258. expect(Dialog.defaultRenderer).toBeInstanceOf(Dialog.Renderer);
  259. });
  260. });
  261. describe('.Renderer', () => {
  262. const renderer = Dialog.defaultRenderer;
  263. const data: Dialog.IButton = {
  264. label: 'foo',
  265. iconClass: 'bar',
  266. iconLabel: 'foo',
  267. caption: 'hello',
  268. className: 'baz',
  269. accept: false,
  270. displayType: 'warn'
  271. };
  272. describe('#createHeader()', () => {
  273. it('should create the header of the dialog', () => {
  274. const widget = renderer.createHeader('foo');
  275. expect(widget.hasClass('jp-Dialog-header')).toBe(true);
  276. });
  277. });
  278. describe('#createBody()', () => {
  279. it('should create the body from a string', () => {
  280. const widget = renderer.createBody('foo');
  281. expect(widget.hasClass('jp-Dialog-body')).toBe(true);
  282. expect(widget.node.firstChild!.textContent).toBe('foo');
  283. });
  284. it('should create the body from a virtual node', () => {
  285. const vnode = (
  286. <div>
  287. <input type={'text'} />
  288. <select>
  289. <option value={'foo'}>foo</option>
  290. </select>
  291. <button />
  292. </div>
  293. );
  294. const widget = renderer.createBody(vnode);
  295. const button = widget.node.querySelector('button')!;
  296. const input = widget.node.querySelector('input')!;
  297. const select = widget.node.querySelector('select')!;
  298. Widget.attach(widget, document.body);
  299. expect(button.className).toContain('jp-mod-styled');
  300. expect(input.className).toContain('jp-mod-styled');
  301. expect(select.className).toContain('jp-mod-styled');
  302. widget.dispose();
  303. });
  304. it('should create the body from a widget', () => {
  305. const body = new Widget();
  306. renderer.createBody(body);
  307. expect(body.hasClass('jp-Dialog-body')).toBe(true);
  308. });
  309. });
  310. describe('#createFooter()', () => {
  311. it('should create the footer of the dialog', () => {
  312. const buttons = [Dialog.okButton, { label: 'foo' }];
  313. const nodes = buttons.map((button: Dialog.IButton) => {
  314. return renderer.createButtonNode(button);
  315. });
  316. const footer = renderer.createFooter(nodes);
  317. const buttonNodes = footer.node.querySelectorAll('button');
  318. expect(footer.hasClass('jp-Dialog-footer')).toBe(true);
  319. expect(footer.node.contains(nodes[0])).toBe(true);
  320. expect(footer.node.contains(nodes[1])).toBe(true);
  321. expect(buttonNodes.length).toBeGreaterThan(0);
  322. each(buttonNodes, (node: Element) => {
  323. expect(node.className).toContain('jp-mod-styled');
  324. });
  325. });
  326. });
  327. describe('#createButtonNode()', () => {
  328. it('should create a button node for the dialog', () => {
  329. const node = renderer.createButtonNode(data);
  330. expect(node.className).toContain('jp-Dialog-button');
  331. expect(node.querySelector('.jp-Dialog-buttonIcon')).toBeTruthy();
  332. expect(node.querySelector('.jp-Dialog-buttonLabel')).toBeTruthy();
  333. });
  334. });
  335. describe('#renderIcon()', () => {
  336. it('should render an icon element for a dialog item', () => {
  337. const node = renderer.renderIcon(data);
  338. expect(node.className).toContain('jp-Dialog-buttonIcon');
  339. expect(node.textContent).toBe('foo');
  340. });
  341. });
  342. describe('#createItemClass()', () => {
  343. it('should create the class name for the button', () => {
  344. const value = renderer.createItemClass(data);
  345. expect(value).toContain('jp-Dialog-button');
  346. expect(value).toContain('jp-mod-reject');
  347. expect(value).toContain(data.className);
  348. });
  349. });
  350. describe('#createIconClass()', () => {
  351. it('should create the class name for the button icon', () => {
  352. const value = renderer.createIconClass(data);
  353. expect(value).toContain('jp-Dialog-buttonIcon');
  354. expect(value).toContain(data.iconClass);
  355. });
  356. });
  357. describe('#renderLabel()', () => {
  358. it('should render a label element for a button', () => {
  359. const node = renderer.renderLabel(data);
  360. expect(node.className).toBe('jp-Dialog-buttonLabel');
  361. expect(node.title).toBe(data.caption);
  362. expect(node.textContent).toBe(data.label);
  363. });
  364. });
  365. });
  366. });
  367. describe('showDialog()', () => {
  368. it('should accept zero arguments', async () => {
  369. const dialog = showDialog();
  370. await dismissDialog();
  371. expect((await dialog).button.accept).toBe(false);
  372. });
  373. it('should accept dialog options', async () => {
  374. const node = document.createElement('div');
  375. document.body.appendChild(node);
  376. const prompt = showDialog({
  377. title: 'foo',
  378. body: 'Hello',
  379. host: node,
  380. defaultButton: 0,
  381. buttons: [Dialog.cancelButton(), Dialog.okButton()]
  382. });
  383. await acceptDialog();
  384. const result = await prompt;
  385. expect(result.button.accept).toBe(false);
  386. expect(result.value).toBe(null);
  387. document.body.removeChild(node);
  388. });
  389. it('should accept a virtualdom body', async () => {
  390. const body = (
  391. <div>
  392. <input />
  393. <select />
  394. </div>
  395. );
  396. const prompt = showDialog({ body });
  397. await acceptDialog();
  398. const result = await prompt;
  399. expect(result.button.accept).toBe(true);
  400. expect(result.value).toBe(null);
  401. });
  402. it('should accept a widget body', async () => {
  403. const prompt = showDialog({ body: new Widget() });
  404. await acceptDialog();
  405. const result = await prompt;
  406. expect(result.button.accept).toBe(true);
  407. expect(result.value).toBe(null);
  408. });
  409. it('should give the value from the widget', async () => {
  410. const prompt = showDialog({ body: new ValueWidget() });
  411. await acceptDialog();
  412. const result = await prompt;
  413. expect(result.button.accept).toBe(true);
  414. expect(result.value).toBe('foo');
  415. });
  416. });
  417. });