inputdialog.spec.ts 7.8 KB

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