inputdialog.spec.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. // Distributed under the terms of the Modified BSD License.
  5. import { InputDialog } from '@jupyterlab/apputils';
  6. import {
  7. acceptDialog,
  8. dismissDialog,
  9. waitForDialog
  10. } from '@jupyterlab/testutils';
  11. describe('@jupyterlab/apputils', () => {
  12. describe('InputDialog', () => {
  13. describe('getBoolean()', () => {
  14. it('should accept at least the title argument', async () => {
  15. const dialog = InputDialog.getBoolean({
  16. title: 'Check or not'
  17. });
  18. await dismissDialog();
  19. expect((await dialog).button.accept).toBe(false);
  20. });
  21. it('should be false by default', async () => {
  22. const dialog = InputDialog.getBoolean({
  23. title: 'Check or not'
  24. });
  25. await acceptDialog();
  26. const result = await dialog;
  27. expect(result.button.accept).toBe(true);
  28. expect(result.value).toBe(false);
  29. });
  30. it('should accept options', async () => {
  31. const dialog = InputDialog.getBoolean({
  32. title: 'Check or not',
  33. value: true
  34. });
  35. await acceptDialog();
  36. const result = await dialog;
  37. expect(result.button.accept).toBe(true);
  38. expect(result.value).toBe(true);
  39. });
  40. it('should be editable', async () => {
  41. const node = document.createElement('div');
  42. document.body.appendChild(node);
  43. const prompt = InputDialog.getBoolean({
  44. title: 'Check or not',
  45. host: node
  46. });
  47. await waitForDialog(node);
  48. const body = node.getElementsByClassName('jp-Input-Dialog').item(0)!;
  49. const input = body.getElementsByTagName('input').item(0)!;
  50. input.checked = true;
  51. await acceptDialog();
  52. const result = await prompt;
  53. expect(result.button.accept).toBe(true);
  54. expect(result.value).toBe(true);
  55. document.body.removeChild(node);
  56. });
  57. });
  58. describe('getItem()', () => {
  59. it('should accept at least two arguments', async () => {
  60. const dialog = InputDialog.getItem({
  61. title: 'list',
  62. items: ['item1']
  63. });
  64. await dismissDialog();
  65. expect((await dialog).button.accept).toBe(false);
  66. });
  67. it('should be the first item by default', async () => {
  68. const dialog = InputDialog.getItem({
  69. items: ['item1', 'item2'],
  70. title: 'Pick a choice'
  71. });
  72. await acceptDialog();
  73. const result = await dialog;
  74. expect(result.button.accept).toBe(true);
  75. expect(result.value).toBe('item1');
  76. });
  77. it('should accept options', async () => {
  78. const dialog = InputDialog.getItem({
  79. label: 'list',
  80. items: ['item1', 'item2'],
  81. current: 1,
  82. editable: false,
  83. title: 'Pick a choice',
  84. placeholder: 'item'
  85. });
  86. await acceptDialog();
  87. const result = await dialog;
  88. expect(result.button.accept).toBe(true);
  89. expect(result.value).toBe('item2');
  90. });
  91. it('should be editable', async () => {
  92. const node = document.createElement('div');
  93. document.body.appendChild(node);
  94. const prompt = InputDialog.getItem({
  95. label: 'list',
  96. items: ['item1', 'item2'],
  97. title: 'Pick a choice',
  98. placeholder: 'item',
  99. editable: true,
  100. host: node
  101. });
  102. await waitForDialog(node);
  103. const body = node.getElementsByClassName('jp-Input-Dialog').item(0)!;
  104. const input = body.getElementsByTagName('input').item(0)!;
  105. input.value = 'item3';
  106. await acceptDialog();
  107. const result = await prompt;
  108. expect(result.button.accept).toBe(true);
  109. expect(result.value).toBe('item3');
  110. document.body.removeChild(node);
  111. });
  112. });
  113. describe('getText()', () => {
  114. it('should accept at least one argument', async () => {
  115. const dialog = InputDialog.getText({
  116. title: 'text'
  117. });
  118. await dismissDialog();
  119. expect((await dialog).button.accept).toBe(false);
  120. });
  121. it('should be an empty string by default', async () => {
  122. const dialog = InputDialog.getText({
  123. title: 'Give a text'
  124. });
  125. await acceptDialog();
  126. const result = await dialog;
  127. expect(result.button.accept).toBe(true);
  128. expect(result.value).toBe('');
  129. });
  130. it('should accept options', async () => {
  131. const dialog = InputDialog.getText({
  132. label: 'text',
  133. title: 'Give a text',
  134. placeholder: 'your text',
  135. text: 'answer'
  136. });
  137. await acceptDialog();
  138. const result = await dialog;
  139. expect(result.button.accept).toBe(true);
  140. expect(result.value).toBe('answer');
  141. });
  142. it('should be editable', async () => {
  143. const node = document.createElement('div');
  144. document.body.appendChild(node);
  145. const prompt = InputDialog.getText({
  146. title: 'text',
  147. host: node
  148. });
  149. await waitForDialog(node);
  150. const body = node.getElementsByClassName('jp-Input-Dialog').item(0)!;
  151. const input = body.getElementsByTagName('input').item(0)!;
  152. input.value = 'my answer';
  153. await acceptDialog();
  154. const result = await prompt;
  155. expect(result.button.accept).toBe(true);
  156. expect(result.value).toBe('my answer');
  157. document.body.removeChild(node);
  158. });
  159. });
  160. describe('getNumber()', () => {
  161. it('should accept at least one argument', async () => {
  162. const dialog = InputDialog.getNumber({
  163. title: 'number'
  164. });
  165. await dismissDialog();
  166. expect((await dialog).button.accept).toBe(false);
  167. });
  168. it('should be 0 by default', async () => {
  169. const dialog = InputDialog.getNumber({
  170. title: 'Pick a number'
  171. });
  172. await acceptDialog();
  173. const result = await dialog;
  174. expect(result.button.accept).toBe(true);
  175. expect(result.value).toBe(0);
  176. });
  177. it('should accept options', async () => {
  178. const dialog = InputDialog.getNumber({
  179. label: 'number',
  180. title: 'Pick a number',
  181. value: 10
  182. });
  183. await acceptDialog();
  184. const result = await dialog;
  185. expect(result.button.accept).toBe(true);
  186. expect(result.value).toBe(10);
  187. });
  188. it('should be editable', async () => {
  189. const node = document.createElement('div');
  190. document.body.appendChild(node);
  191. const prompt = InputDialog.getNumber({
  192. label: 'text',
  193. title: 'Pick a number',
  194. host: node
  195. });
  196. await waitForDialog(node);
  197. const body = node.getElementsByClassName('jp-Input-Dialog').item(0)!;
  198. const input = body.getElementsByTagName('input').item(0)!;
  199. input.value = '25';
  200. await acceptDialog();
  201. const result = await prompt;
  202. expect(result.button.accept).toBe(true);
  203. expect(result.value).toBe(25);
  204. document.body.removeChild(node);
  205. });
  206. it('should return NaN if empty', async () => {
  207. const node = document.createElement('div');
  208. document.body.appendChild(node);
  209. const prompt = InputDialog.getNumber({
  210. label: 'text',
  211. title: 'Pick a number',
  212. host: node
  213. });
  214. await waitForDialog(node);
  215. const body = node.getElementsByClassName('jp-Input-Dialog').item(0)!;
  216. const input = body.getElementsByTagName('input').item(0)!;
  217. input.value = '';
  218. await acceptDialog();
  219. const result = await prompt;
  220. expect(result.button.accept).toBe(true);
  221. expect(result.value).toBeNaN();
  222. document.body.removeChild(node);
  223. });
  224. });
  225. });
  226. });