dialog.spec.tsx 15 KB

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