dialog.spec.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. // import { expect } from 'chai';
  4. import { Dialog, showDialog } from '@jupyterlab/apputils';
  5. import { each } from '@lumino/algorithm';
  6. import { Message } from '@lumino/messaging';
  7. import { Widget } from '@lumino/widgets';
  8. import { generate, simulate } from 'simulate-event';
  9. import * as React from 'react';
  10. import {
  11. acceptDialog,
  12. dismissDialog,
  13. waitForDialog
  14. } from '@jupyterlab/testutils';
  15. class TestDialog extends Dialog<any> {
  16. methods: string[] = [];
  17. events: string[] = [];
  18. handleEvent(event: Event): void {
  19. super.handleEvent(event);
  20. this.events.push(event.type);
  21. }
  22. protected onAfterAttach(msg: Message): void {
  23. super.onAfterAttach(msg);
  24. this.methods.push('onAfterAttach');
  25. }
  26. protected onAfterDetach(msg: Message): void {
  27. super.onAfterDetach(msg);
  28. this.methods.push('onAfterDetach');
  29. }
  30. protected onCloseRequest(msg: Message): void {
  31. super.onCloseRequest(msg);
  32. this.methods.push('onCloseRequest');
  33. }
  34. }
  35. class ValueWidget extends Widget {
  36. getValue(): string {
  37. return 'foo';
  38. }
  39. }
  40. describe('@jupyterlab/apputils', () => {
  41. describe('Dialog', () => {
  42. let dialog: TestDialog;
  43. beforeEach(() => {
  44. dialog = new TestDialog();
  45. });
  46. afterEach(() => {
  47. dialog.dispose();
  48. });
  49. describe('#constructor()', () => {
  50. it('should create a new dialog', () => {
  51. expect(dialog).toBeInstanceOf(Dialog);
  52. });
  53. it('should accept options', () => {
  54. const dialog = new TestDialog({
  55. title: 'foo',
  56. body: 'Hello',
  57. buttons: [Dialog.okButton()],
  58. hasClose: false
  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. actions: []
  272. };
  273. describe('#createHeader()', () => {
  274. it('should create the header of the dialog', () => {
  275. const widget = renderer.createHeader('foo');
  276. expect(widget.hasClass('jp-Dialog-header')).toBe(true);
  277. });
  278. });
  279. describe('#createBody()', () => {
  280. it('should create the body from a string', () => {
  281. const widget = renderer.createBody('foo');
  282. expect(widget.hasClass('jp-Dialog-body')).toBe(true);
  283. expect(widget.node.firstChild!.textContent).toBe('foo');
  284. });
  285. it('should create the body from a virtual node', () => {
  286. const vnode = (
  287. <div>
  288. <input type={'text'} />
  289. <select>
  290. <option value={'foo'}>foo</option>
  291. </select>
  292. <button />
  293. </div>
  294. );
  295. const widget = renderer.createBody(vnode);
  296. const button = widget.node.querySelector('button')!;
  297. const input = widget.node.querySelector('input')!;
  298. const select = widget.node.querySelector('select')!;
  299. Widget.attach(widget, document.body);
  300. expect(button.className).toContain('jp-mod-styled');
  301. expect(input.className).toContain('jp-mod-styled');
  302. expect(select.className).toContain('jp-mod-styled');
  303. widget.dispose();
  304. });
  305. it('should create the body from a widget', () => {
  306. const body = new Widget();
  307. renderer.createBody(body);
  308. expect(body.hasClass('jp-Dialog-body')).toBe(true);
  309. });
  310. });
  311. describe('#createFooter()', () => {
  312. it('should create the footer of the dialog', () => {
  313. const buttons = [Dialog.okButton, { label: 'foo' }];
  314. const nodes = buttons.map((button: Dialog.IButton) => {
  315. return renderer.createButtonNode(button);
  316. });
  317. const footer = renderer.createFooter(nodes);
  318. const buttonNodes = footer.node.querySelectorAll('button');
  319. expect(footer.hasClass('jp-Dialog-footer')).toBe(true);
  320. expect(footer.node.contains(nodes[0])).toBe(true);
  321. expect(footer.node.contains(nodes[1])).toBe(true);
  322. expect(buttonNodes.length).toBeGreaterThan(0);
  323. each(buttonNodes, (node: Element) => {
  324. expect(node.className).toContain('jp-mod-styled');
  325. });
  326. });
  327. });
  328. describe('#createButtonNode()', () => {
  329. it('should create a button node for the dialog', () => {
  330. const node = renderer.createButtonNode(data);
  331. expect(node.className).toContain('jp-Dialog-button');
  332. expect(node.querySelector('.jp-Dialog-buttonIcon')).toBeTruthy();
  333. expect(node.querySelector('.jp-Dialog-buttonLabel')).toBeTruthy();
  334. });
  335. });
  336. describe('#renderIcon()', () => {
  337. it('should render an icon element for a dialog item', () => {
  338. const node = renderer.renderIcon(data);
  339. expect(node.className).toContain('jp-Dialog-buttonIcon');
  340. expect(node.textContent).toBe('foo');
  341. });
  342. });
  343. describe('#createItemClass()', () => {
  344. it('should create the class name for the button', () => {
  345. const value = renderer.createItemClass(data);
  346. expect(value).toContain('jp-Dialog-button');
  347. expect(value).toContain('jp-mod-reject');
  348. expect(value).toContain(data.className);
  349. });
  350. });
  351. describe('#createIconClass()', () => {
  352. it('should create the class name for the button icon', () => {
  353. const value = renderer.createIconClass(data);
  354. expect(value).toContain('jp-Dialog-buttonIcon');
  355. expect(value).toContain(data.iconClass);
  356. });
  357. });
  358. describe('#renderLabel()', () => {
  359. it('should render a label element for a button', () => {
  360. const node = renderer.renderLabel(data);
  361. expect(node.className).toBe('jp-Dialog-buttonLabel');
  362. expect(node.title).toBe(data.caption);
  363. expect(node.textContent).toBe(data.label);
  364. });
  365. });
  366. });
  367. });
  368. describe('showDialog()', () => {
  369. it('should accept zero arguments', async () => {
  370. const dialog = showDialog();
  371. await dismissDialog();
  372. expect((await dialog).button.accept).toBe(false);
  373. });
  374. it('should accept dialog options', async () => {
  375. const node = document.createElement('div');
  376. document.body.appendChild(node);
  377. const prompt = showDialog({
  378. title: 'foo',
  379. body: 'Hello',
  380. host: node,
  381. defaultButton: 0,
  382. buttons: [Dialog.cancelButton(), Dialog.okButton()]
  383. });
  384. await acceptDialog();
  385. const result = await prompt;
  386. expect(result.button.accept).toBe(false);
  387. expect(result.value).toBe(null);
  388. document.body.removeChild(node);
  389. });
  390. it('should accept a virtualdom body', async () => {
  391. const body = (
  392. <div>
  393. <input />
  394. <select />
  395. </div>
  396. );
  397. const prompt = showDialog({ body });
  398. await acceptDialog();
  399. const result = await prompt;
  400. expect(result.button.accept).toBe(true);
  401. expect(result.value).toBe(null);
  402. });
  403. it('should accept a widget body', async () => {
  404. const prompt = showDialog({ body: new Widget() });
  405. await acceptDialog();
  406. const result = await prompt;
  407. expect(result.button.accept).toBe(true);
  408. expect(result.value).toBe(null);
  409. });
  410. it('should give the value from the widget', async () => {
  411. const prompt = showDialog({ body: new ValueWidget() });
  412. await acceptDialog();
  413. const result = await prompt;
  414. expect(result.button.accept).toBe(true);
  415. expect(result.value).toBe('foo');
  416. });
  417. it('should not create a close button by default', async () => {
  418. const node = document.createElement('div');
  419. document.body.appendChild(node);
  420. const prompt = showDialog({
  421. title: 'foo',
  422. body: 'Hello',
  423. host: node,
  424. defaultButton: 0,
  425. buttons: [Dialog.cancelButton(), Dialog.okButton()]
  426. });
  427. await waitForDialog();
  428. expect(node.querySelector('.jp-Dialog-close-button')).toBeFalsy();
  429. await acceptDialog();
  430. const result = await prompt;
  431. expect(result.button.accept).toBe(false);
  432. expect(result.button.actions).toEqual([]);
  433. expect(result.value).toBe(null);
  434. document.body.removeChild(node);
  435. });
  436. it('should create a close button', async () => {
  437. const node = document.createElement('div');
  438. document.body.appendChild(node);
  439. const prompt = showDialog({
  440. title: 'foo',
  441. body: 'Hello',
  442. host: node,
  443. defaultButton: 0,
  444. buttons: [Dialog.cancelButton(), Dialog.okButton()],
  445. hasClose: true
  446. });
  447. await waitForDialog();
  448. expect(node.querySelector('.jp-Dialog-close-button')).toBeTruthy();
  449. await acceptDialog();
  450. const result = await prompt;
  451. expect(result.button.accept).toBe(false);
  452. expect(result.button.actions).toEqual([]);
  453. expect(result.value).toBe(null);
  454. document.body.removeChild(node);
  455. });
  456. it('should accept the dialog reload options', async () => {
  457. const node = document.createElement('div');
  458. document.body.appendChild(node);
  459. const prompt = showDialog({
  460. title: 'foo',
  461. body: 'Hello',
  462. host: node,
  463. defaultButton: 0,
  464. buttons: [
  465. Dialog.cancelButton({ actions: ['reload'] }),
  466. Dialog.okButton()
  467. ],
  468. hasClose: true
  469. });
  470. await acceptDialog();
  471. const result = await prompt;
  472. expect(result.button.accept).toBe(false);
  473. expect(result.button.actions).toEqual(['reload']);
  474. expect(result.value).toBe(null);
  475. document.body.removeChild(node);
  476. });
  477. });
  478. });