dialog.spec.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. hasClose: false
  60. });
  61. expect(dialog).toBeInstanceOf(Dialog);
  62. dialog.dispose();
  63. });
  64. });
  65. describe('#launch()', () => {
  66. it.skip('should attach the dialog to the host', async () => {
  67. const host = document.createElement('div');
  68. const dialog = new TestDialog({ host });
  69. document.body.appendChild(host);
  70. void dialog.launch();
  71. await waitForDialog(host);
  72. expect(host.contains(dialog.node)).toBe(true);
  73. dialog.dispose();
  74. document.body.removeChild(host);
  75. });
  76. it('should resolve with `true` when accepted', async () => {
  77. const prompt = dialog.launch();
  78. await waitForDialog();
  79. dialog.resolve();
  80. expect((await prompt).button.accept).toBe(true);
  81. });
  82. it('should resolve with `false` when rejected', async () => {
  83. const prompt = dialog.launch();
  84. await waitForDialog();
  85. dialog.reject();
  86. expect((await prompt).button.accept).toBe(false);
  87. });
  88. it('should resolve with `false` when closed', async () => {
  89. const prompt = dialog.launch();
  90. await waitForDialog();
  91. dialog.close();
  92. expect((await prompt).button.accept).toBe(false);
  93. });
  94. it('should return focus to the original focused element', async () => {
  95. const input = document.createElement('input');
  96. document.body.appendChild(input);
  97. input.focus();
  98. expect(document.activeElement).toBe(input);
  99. const prompt = dialog.launch();
  100. await waitForDialog();
  101. expect(document.activeElement).not.toBe(input);
  102. dialog.resolve();
  103. await prompt;
  104. expect(document.activeElement).toBe(input);
  105. document.body.removeChild(input);
  106. });
  107. });
  108. describe('#resolve()', () => {
  109. it('should resolve with the default item', async () => {
  110. const prompt = dialog.launch();
  111. await waitForDialog();
  112. dialog.resolve();
  113. expect((await prompt).button.accept).toBe(true);
  114. });
  115. it('should resolve with the item at the given index', async () => {
  116. const prompt = dialog.launch();
  117. await waitForDialog();
  118. dialog.resolve(0);
  119. expect((await prompt).button.accept).toBe(false);
  120. });
  121. });
  122. describe('#reject()', () => {
  123. it('should reject with the default reject item', async () => {
  124. const prompt = dialog.launch();
  125. await waitForDialog();
  126. dialog.reject();
  127. const result = await prompt;
  128. expect(result.button.label).toBe('Cancel');
  129. expect(result.button.accept).toBe(false);
  130. });
  131. });
  132. describe('#handleEvent()', () => {
  133. describe('keydown', () => {
  134. it('should reject on escape key', async () => {
  135. const prompt = dialog.launch();
  136. await waitForDialog();
  137. simulate(dialog.node, 'keydown', { keyCode: 27 });
  138. expect((await prompt).button.accept).toBe(false);
  139. });
  140. it('should accept on enter key', async () => {
  141. const prompt = dialog.launch();
  142. await waitForDialog();
  143. simulate(dialog.node, 'keydown', { keyCode: 13 });
  144. expect((await prompt).button.accept).toBe(true);
  145. });
  146. it('should cycle to the first button on a tab key', async () => {
  147. const prompt = dialog.launch();
  148. await waitForDialog();
  149. expect(document.activeElement!.className).toContain('jp-mod-accept');
  150. simulate(dialog.node, 'keydown', { keyCode: 9 });
  151. expect(document.activeElement!.className).toContain('jp-mod-reject');
  152. simulate(document.activeElement!, 'click');
  153. expect((await prompt).button.accept).toBe(false);
  154. });
  155. });
  156. describe('contextmenu', () => {
  157. it('should cancel context menu events', async () => {
  158. const prompt = dialog.launch();
  159. await waitForDialog();
  160. const canceled = !dialog.node.dispatchEvent(generate('contextmenu'));
  161. expect(canceled).toBe(true);
  162. simulate(dialog.node, 'keydown', { keyCode: 27 });
  163. expect((await prompt).button.accept).toBe(false);
  164. });
  165. });
  166. describe('click', () => {
  167. it('should prevent clicking outside of the content area', async () => {
  168. const prompt = dialog.launch();
  169. await waitForDialog();
  170. const canceled = !dialog.node.dispatchEvent(generate('click'));
  171. expect(canceled).toBe(true);
  172. dialog.resolve();
  173. await prompt;
  174. });
  175. it('should resolve a clicked button', async () => {
  176. const prompt = dialog.launch();
  177. await waitForDialog();
  178. simulate(dialog.node.querySelector('.jp-mod-reject')!, 'click');
  179. expect((await prompt).button.accept).toBe(false);
  180. });
  181. });
  182. describe('focus', () => {
  183. it('should focus the default button when focus leaves the dialog', async () => {
  184. const host = document.createElement('div');
  185. const target = document.createElement('div');
  186. const dialog = new TestDialog({ host });
  187. target.tabIndex = -1;
  188. document.body.appendChild(target);
  189. document.body.appendChild(host);
  190. target.focus();
  191. expect(document.activeElement).toBe(target);
  192. const prompt = dialog.launch();
  193. await waitForDialog();
  194. simulate(target, 'focus');
  195. expect(document.activeElement).not.toBe(target);
  196. expect(document.activeElement!.className).toContain('jp-mod-accept');
  197. dialog.resolve();
  198. await prompt;
  199. dialog.dispose();
  200. });
  201. });
  202. });
  203. describe('#onAfterAttach()', () => {
  204. it('should attach event listeners', () => {
  205. Widget.attach(dialog, document.body);
  206. expect(dialog.methods).toContain('onAfterAttach');
  207. ['keydown', 'contextmenu', 'click', 'focus'].forEach(event => {
  208. simulate(dialog.node, event);
  209. expect(dialog.events).toContain(event);
  210. });
  211. });
  212. it('should focus the default button', () => {
  213. Widget.attach(dialog, document.body);
  214. expect(document.activeElement!.className).toContain('jp-mod-accept');
  215. });
  216. it('should focus the primary element', () => {
  217. const body = (
  218. <div>
  219. <input type={'text'} />
  220. </div>
  221. );
  222. const dialog = new TestDialog({ body, focusNodeSelector: 'input' });
  223. Widget.attach(dialog, document.body);
  224. expect(document.activeElement!.localName).toBe('input');
  225. dialog.dispose();
  226. });
  227. });
  228. describe('#onAfterDetach()', () => {
  229. it('should remove event listeners', () => {
  230. Widget.attach(dialog, document.body);
  231. Widget.detach(dialog);
  232. expect(dialog.methods).toContain('onAfterDetach');
  233. dialog.events = [];
  234. ['keydown', 'contextmenu', 'click', 'focus'].forEach(event => {
  235. simulate(dialog.node, event);
  236. expect(dialog.events).not.toContain(event);
  237. });
  238. });
  239. it('should return focus to the original focused element', () => {
  240. const input = document.createElement('input');
  241. document.body.appendChild(input);
  242. input.focus();
  243. Widget.attach(dialog, document.body);
  244. Widget.detach(dialog);
  245. expect(document.activeElement).toBe(input);
  246. document.body.removeChild(input);
  247. });
  248. });
  249. describe('#onCloseRequest()', () => {
  250. it('should reject an existing promise', async () => {
  251. const prompt = dialog.launch();
  252. await waitForDialog();
  253. dialog.close();
  254. expect((await prompt).button.accept).toBe(false);
  255. });
  256. });
  257. describe('.defaultRenderer', () => {
  258. it('should be an instance of a Renderer', () => {
  259. expect(Dialog.defaultRenderer).toBeInstanceOf(Dialog.Renderer);
  260. });
  261. });
  262. describe('.Renderer', () => {
  263. const renderer = Dialog.defaultRenderer;
  264. const data: Dialog.IButton = {
  265. label: 'foo',
  266. iconClass: 'bar',
  267. iconLabel: 'foo',
  268. caption: 'hello',
  269. className: 'baz',
  270. accept: false,
  271. displayType: 'warn',
  272. actions: []
  273. };
  274. describe('#createHeader()', () => {
  275. it('should create the header of the dialog', () => {
  276. const widget = renderer.createHeader('foo');
  277. expect(widget.hasClass('jp-Dialog-header')).toBe(true);
  278. });
  279. });
  280. describe('#createBody()', () => {
  281. it('should create the body from a string', () => {
  282. const widget = renderer.createBody('foo');
  283. expect(widget.hasClass('jp-Dialog-body')).toBe(true);
  284. expect(widget.node.firstChild!.textContent).toBe('foo');
  285. });
  286. it('should create the body from a virtual node', () => {
  287. const vnode = (
  288. <div>
  289. <input type={'text'} />
  290. <select>
  291. <option value={'foo'}>foo</option>
  292. </select>
  293. <button />
  294. </div>
  295. );
  296. const widget = renderer.createBody(vnode);
  297. const button = widget.node.querySelector('button')!;
  298. const input = widget.node.querySelector('input')!;
  299. const select = widget.node.querySelector('select')!;
  300. Widget.attach(widget, document.body);
  301. expect(button.className).toContain('jp-mod-styled');
  302. expect(input.className).toContain('jp-mod-styled');
  303. expect(select.className).toContain('jp-mod-styled');
  304. widget.dispose();
  305. });
  306. it('should create the body from a widget', () => {
  307. const body = new Widget();
  308. renderer.createBody(body);
  309. expect(body.hasClass('jp-Dialog-body')).toBe(true);
  310. });
  311. });
  312. describe('#createFooter()', () => {
  313. it('should create the footer of the dialog', () => {
  314. const buttons = [Dialog.okButton, { label: 'foo' }];
  315. const nodes = buttons.map((button: Dialog.IButton) => {
  316. return renderer.createButtonNode(button);
  317. });
  318. const footer = renderer.createFooter(nodes);
  319. const buttonNodes = footer.node.querySelectorAll('button');
  320. expect(footer.hasClass('jp-Dialog-footer')).toBe(true);
  321. expect(footer.node.contains(nodes[0])).toBe(true);
  322. expect(footer.node.contains(nodes[1])).toBe(true);
  323. expect(buttonNodes.length).toBeGreaterThan(0);
  324. each(buttonNodes, (node: Element) => {
  325. expect(node.className).toContain('jp-mod-styled');
  326. });
  327. });
  328. });
  329. describe('#createButtonNode()', () => {
  330. it('should create a button node for the dialog', () => {
  331. const node = renderer.createButtonNode(data);
  332. expect(node.className).toContain('jp-Dialog-button');
  333. expect(node.querySelector('.jp-Dialog-buttonIcon')).toBeTruthy();
  334. expect(node.querySelector('.jp-Dialog-buttonLabel')).toBeTruthy();
  335. });
  336. });
  337. describe('#renderIcon()', () => {
  338. it('should render an icon element for a dialog item', () => {
  339. const node = renderer.renderIcon(data);
  340. expect(node.className).toContain('jp-Dialog-buttonIcon');
  341. expect(node.textContent).toBe('foo');
  342. });
  343. });
  344. describe('#createItemClass()', () => {
  345. it('should create the class name for the button', () => {
  346. const value = renderer.createItemClass(data);
  347. expect(value).toContain('jp-Dialog-button');
  348. expect(value).toContain('jp-mod-reject');
  349. expect(value).toContain(data.className);
  350. });
  351. });
  352. describe('#createIconClass()', () => {
  353. it('should create the class name for the button icon', () => {
  354. const value = renderer.createIconClass(data);
  355. expect(value).toContain('jp-Dialog-buttonIcon');
  356. expect(value).toContain(data.iconClass);
  357. });
  358. });
  359. describe('#renderLabel()', () => {
  360. it('should render a label element for a button', () => {
  361. const node = renderer.renderLabel(data);
  362. expect(node.className).toBe('jp-Dialog-buttonLabel');
  363. expect(node.title).toBe(data.caption);
  364. expect(node.textContent).toBe(data.label);
  365. });
  366. });
  367. });
  368. });
  369. describe('showDialog()', () => {
  370. it('should accept zero arguments', async () => {
  371. const dialog = showDialog();
  372. await dismissDialog();
  373. expect((await dialog).button.accept).toBe(false);
  374. });
  375. it('should accept dialog options', async () => {
  376. const node = document.createElement('div');
  377. document.body.appendChild(node);
  378. const prompt = showDialog({
  379. title: 'foo',
  380. body: 'Hello',
  381. host: node,
  382. defaultButton: 0,
  383. buttons: [Dialog.cancelButton(), Dialog.okButton()]
  384. });
  385. await acceptDialog();
  386. const result = await prompt;
  387. expect(result.button.accept).toBe(false);
  388. expect(result.value).toBe(null);
  389. document.body.removeChild(node);
  390. });
  391. it('should accept a virtualdom body', async () => {
  392. const body = (
  393. <div>
  394. <input />
  395. <select />
  396. </div>
  397. );
  398. const prompt = showDialog({ body });
  399. await acceptDialog();
  400. const result = await prompt;
  401. expect(result.button.accept).toBe(true);
  402. expect(result.value).toBe(null);
  403. });
  404. it('should accept a widget body', async () => {
  405. const prompt = showDialog({ body: new Widget() });
  406. await acceptDialog();
  407. const result = await prompt;
  408. expect(result.button.accept).toBe(true);
  409. expect(result.value).toBe(null);
  410. });
  411. it('should give the value from the widget', async () => {
  412. const prompt = showDialog({ body: new ValueWidget() });
  413. await acceptDialog();
  414. const result = await prompt;
  415. expect(result.button.accept).toBe(true);
  416. expect(result.value).toBe('foo');
  417. });
  418. it('should not create a close button by default', async () => {
  419. const node = document.createElement('div');
  420. document.body.appendChild(node);
  421. const prompt = showDialog({
  422. title: 'foo',
  423. body: 'Hello',
  424. host: node,
  425. defaultButton: 0,
  426. buttons: [Dialog.cancelButton(), Dialog.okButton()]
  427. });
  428. await waitForDialog();
  429. expect(node.querySelector('.jp-Dialog-close-button')).toBeFalsy();
  430. await acceptDialog();
  431. const result = await prompt;
  432. expect(result.button.accept).toBe(false);
  433. expect(result.button.actions).toEqual([]);
  434. expect(result.value).toBe(null);
  435. document.body.removeChild(node);
  436. });
  437. it('should create a close button', async () => {
  438. const node = document.createElement('div');
  439. document.body.appendChild(node);
  440. const prompt = showDialog({
  441. title: 'foo',
  442. body: 'Hello',
  443. host: node,
  444. defaultButton: 0,
  445. buttons: [Dialog.cancelButton(), Dialog.okButton()],
  446. hasClose: true
  447. });
  448. await waitForDialog();
  449. expect(node.querySelector('.jp-Dialog-close-button')).toBeTruthy();
  450. await acceptDialog();
  451. const result = await prompt;
  452. expect(result.button.accept).toBe(false);
  453. expect(result.button.actions).toEqual([]);
  454. expect(result.value).toBe(null);
  455. document.body.removeChild(node);
  456. });
  457. it('should accept the dialog reload options', async () => {
  458. const node = document.createElement('div');
  459. document.body.appendChild(node);
  460. const prompt = showDialog({
  461. title: 'foo',
  462. body: 'Hello',
  463. host: node,
  464. defaultButton: 0,
  465. buttons: [
  466. Dialog.cancelButton({ actions: ['reload'] }),
  467. Dialog.okButton()
  468. ],
  469. hasClose: true
  470. });
  471. await acceptDialog();
  472. const result = await prompt;
  473. expect(result.button.accept).toBe(false);
  474. expect(result.button.actions).toEqual(['reload']);
  475. expect(result.value).toBe(null);
  476. document.body.removeChild(node);
  477. });
  478. });
  479. });