Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

527 lines
15 KiB

  1. import getFormValuesDeprecated, {
  2. getFormValues,
  3. setFormValues,
  4. isFieldElement,
  5. isElementValueIncludedInFormSubmit,
  6. getValue, clearFormValues,
  7. } from '../../src';
  8. import * as utils from '../utils'
  9. describe('misc', () => {
  10. describe('core', () => {
  11. beforeEach(utils.setup(`
  12. <!DOCTYPE html>
  13. <html lang="en-PH">
  14. <head>
  15. <meta charset="UTF-8">
  16. <title>Misc/Core</title>
  17. </head>
  18. <body>
  19. <form>
  20. <button type="submit">Submit</button>
  21. </form>
  22. </body>
  23. </html>
  24. `))
  25. it('should call console.warn for deprecated default import usage', () => {
  26. utils.test({
  27. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  28. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  29. let consoleWarnCalled = false
  30. const defaultConsoleWarn = console.warn
  31. console.warn = (...args: unknown[]) => {
  32. consoleWarnCalled = true
  33. };
  34. getFormValuesDeprecated(form, { submitter });
  35. expect(consoleWarnCalled).toBe(true);
  36. console.warn = defaultConsoleWarn;
  37. },
  38. });
  39. });
  40. it('should call noop for deprecated default import usage when console is not found', () => {
  41. utils.test({
  42. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  43. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  44. let consoleWarnCalled = false
  45. const defaultConsoleWarn = console.warn
  46. console.warn = (...args: unknown[]) => {
  47. consoleWarnCalled = true
  48. };
  49. const globalConsole = global.console;
  50. global.console = undefined;
  51. getFormValuesDeprecated(form, { submitter });
  52. expect(consoleWarnCalled).toBe(false);
  53. global.console = globalConsole;
  54. console.warn = defaultConsoleWarn;
  55. },
  56. });
  57. });
  58. it('should throw an error when providing invalid argument type as form to getFormValues', () => {
  59. utils.test({
  60. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  61. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  62. let isThrown = false;
  63. try {
  64. getFormValues(0 as unknown as HTMLFormElement, {});
  65. } catch {
  66. isThrown = true;
  67. }
  68. expect(isThrown).toBe(true);
  69. },
  70. });
  71. });
  72. it('should throw an error when providing null as form to getFormValues', () => {
  73. utils.test({
  74. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  75. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  76. let isThrown = false;
  77. try {
  78. getFormValues(null as unknown as HTMLFormElement, {});
  79. } catch {
  80. isThrown = true;
  81. }
  82. expect(isThrown).toBe(true);
  83. },
  84. });
  85. });
  86. it('should throw an error when providing a different element type as form to getFormValues', () => {
  87. utils.test({
  88. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  89. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  90. let isThrown = false;
  91. try {
  92. getFormValues(document.body as unknown as HTMLFormElement, {});
  93. } catch {
  94. isThrown = true;
  95. }
  96. expect(isThrown).toBe(true);
  97. },
  98. });
  99. });
  100. it('should throw an error when providing invalid argument type as form to setFormValues', () => {
  101. utils.test({
  102. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  103. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  104. let isThrown = false;
  105. try {
  106. setFormValues(0 as unknown as HTMLFormElement, {});
  107. } catch {
  108. isThrown = true;
  109. }
  110. expect(isThrown).toBe(true);
  111. },
  112. });
  113. });
  114. it('should throw an error when providing null as form to setFormValues', () => {
  115. utils.test({
  116. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  117. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  118. let isThrown = false;
  119. try {
  120. setFormValues(null as unknown as HTMLFormElement, {});
  121. } catch {
  122. isThrown = true;
  123. }
  124. expect(isThrown).toBe(true);
  125. },
  126. });
  127. });
  128. it('should throw an error when providing a different element type as form to setFormValues', () => {
  129. utils.test({
  130. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  131. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  132. let isThrown = false;
  133. try {
  134. setFormValues(document.body as unknown as HTMLFormElement, {});
  135. } catch {
  136. isThrown = true;
  137. }
  138. expect(isThrown).toBe(true);
  139. },
  140. });
  141. });
  142. it('should throw an error when providing invalid argument type as values to setFormValues', () => {
  143. utils.test({
  144. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  145. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  146. let isThrown = false;
  147. try {
  148. setFormValues(form, 0);
  149. } catch {
  150. isThrown = true;
  151. }
  152. expect(isThrown).toBe(true);
  153. },
  154. });
  155. });
  156. it('should not throw an error when providing null as form to setFormValues', () => {
  157. utils.test({
  158. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  159. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  160. let isThrown = false;
  161. try {
  162. setFormValues(form, null);
  163. } catch (e) {
  164. isThrown = true;
  165. }
  166. expect(isThrown).toBe(false);
  167. },
  168. });
  169. });
  170. it('should throw an error when providing undefined as form to setFormValues', () => {
  171. utils.test({
  172. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  173. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  174. let isThrown = false;
  175. try {
  176. setFormValues(form, undefined);
  177. } catch (e) {
  178. isThrown = true;
  179. }
  180. expect(isThrown).toBe(true);
  181. },
  182. });
  183. });
  184. });
  185. describe('isindex', () => {
  186. beforeEach(utils.setup(`
  187. <!DOCTYPE html>
  188. <html lang="en-PH">
  189. <head>
  190. <meta charset="UTF-8">
  191. <title>Misc/Isindex</title>
  192. </head>
  193. <body>
  194. <form>
  195. <input name="isindex" type="text" value="value" />
  196. <button type="submit">Submit</button>
  197. </form>
  198. </body>
  199. </html>
  200. `))
  201. it('should have blank form value', () => {
  202. utils.test({
  203. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  204. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  205. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  206. .toString();
  207. const after = utils.makeSearchParams(search)
  208. .toString();
  209. expect(before)
  210. .toEqual(after);
  211. },
  212. expectedStaticValue: {},
  213. });
  214. });
  215. })
  216. describe('utilities', () => {
  217. beforeEach(utils.setup(`
  218. <!DOCTYPE html>
  219. <html lang="en-PH">
  220. <head>
  221. <meta charset="UTF-8">
  222. <title>Misc/Utilities</title>
  223. </head>
  224. <body>
  225. <form>
  226. <input id="input" type="text" name="foobar" />
  227. <input id="notField" type="text" />
  228. <input id="disabled" disabled type="text" name="disabled" />
  229. <meter id="meter" min="1" max="10" value="5" />
  230. <output id="output" name="random">5</output>
  231. <button type="submit">Submit</button>
  232. </form>
  233. </body>
  234. </html>
  235. `));
  236. it('should check for valid field elements value', () => {
  237. utils.test({
  238. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  239. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  240. const meter = document.getElementById('meter');
  241. expect(getValue(meter)).toBe(5);
  242. const output = document.getElementById('output');
  243. expect(getValue(output)).toBe('5');
  244. },
  245. });
  246. });
  247. it('should check for invalid field elements value', () => {
  248. utils.test({
  249. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  250. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  251. expect(getValue(document.body)).toBe(null);
  252. },
  253. });
  254. });
  255. it('should check for elements as included fields', () => {
  256. utils.test({
  257. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  258. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  259. const input = document.getElementById('input');
  260. expect(isElementValueIncludedInFormSubmit(input)).toBe(true);
  261. },
  262. });
  263. });
  264. it('should check for elements as excluded fields', () => {
  265. utils.test({
  266. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  267. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  268. const notField = document.getElementById('notField');
  269. expect(isElementValueIncludedInFormSubmit(notField)).toBe(false);
  270. const disabled = document.getElementById('disabled');
  271. expect(isElementValueIncludedInFormSubmit(disabled)).toBe(false);
  272. const meter = document.getElementById('meter');
  273. expect(isElementValueIncludedInFormSubmit(meter)).toBe(false);
  274. },
  275. });
  276. });
  277. it('should check for elements as valid for fields', () => {
  278. utils.test({
  279. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  280. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  281. const input = document.getElementById('input');
  282. expect(isFieldElement(input)).toBe(true);
  283. const disabled = document.getElementById('disabled');
  284. expect(isFieldElement(disabled)).toBe(true);
  285. },
  286. });
  287. });
  288. it('should check for elements as invalid for fields', () => {
  289. utils.test({
  290. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  291. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  292. const meter = document.getElementById('meter');
  293. expect(isFieldElement(meter)).toBe(false);
  294. const notField = document.getElementById('notField');
  295. expect(isFieldElement(notField)).toBe(false);
  296. },
  297. });
  298. });
  299. });
  300. describe('setting values', () => {
  301. beforeEach(utils.setup(`
  302. <!DOCTYPE html>
  303. <html lang="en-PH">
  304. <head>
  305. <meta charset="UTF-8">
  306. <title>Misc/Blank</title>
  307. </head>
  308. <body>
  309. <form>
  310. <input type="text" name="foobar" />
  311. <input type="text" name="disabled" disabled />
  312. <button type="submit">Submit</button>
  313. </form>
  314. </body>
  315. </html>
  316. `))
  317. it('should parse string values for setFormValues', () => {
  318. utils.test({
  319. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  320. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  321. let isThrown = false;
  322. try {
  323. setFormValues(form, 'foobar=baz');
  324. } catch (e) {
  325. isThrown = true;
  326. }
  327. expect(isThrown).toBe(false);
  328. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  329. },
  330. })
  331. });
  332. it('should parse entries values for setFormValues', () => {
  333. utils.test({
  334. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  335. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  336. let isThrown = false;
  337. try {
  338. setFormValues(form, [['foobar', 'baz']]);
  339. } catch (e) {
  340. isThrown = true;
  341. }
  342. expect(isThrown).toBe(false);
  343. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  344. },
  345. })
  346. });
  347. it('should parse URLSearchParams values for setFormValues', () => {
  348. utils.test({
  349. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  350. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  351. let isThrown = false;
  352. try {
  353. setFormValues(form, new URLSearchParams('foobar=baz'));
  354. } catch (e) {
  355. isThrown = true;
  356. }
  357. expect(isThrown).toBe(false);
  358. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  359. },
  360. })
  361. });
  362. it('should parse object values for setFormValues', () => {
  363. utils.test({
  364. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  365. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  366. let isThrown = false;
  367. try {
  368. setFormValues(form, { foobar: 'baz', });
  369. } catch (e) {
  370. isThrown = true;
  371. }
  372. expect(isThrown).toBe(false);
  373. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  374. },
  375. })
  376. });
  377. it('should allow setting values for disabled elements', () => {
  378. utils.test({
  379. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  380. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  381. let isThrown = false;
  382. try {
  383. setFormValues(form, { foobar: 'baz', disabled: 'new value' }, { includeDisabled: true });
  384. } catch (e) {
  385. isThrown = true;
  386. }
  387. expect(isThrown).toBe(false);
  388. expect(getFormValues(form, { includeDisabled: true })).toEqual({ foobar: 'baz', disabled: 'new value', });
  389. },
  390. })
  391. });
  392. });
  393. describe('clearing values', () => {
  394. beforeEach(utils.setup(`
  395. <!DOCTYPE html>
  396. <html lang="en-PH">
  397. <head>
  398. <meta charset="UTF-8">
  399. <title>Misc/Blank</title>
  400. </head>
  401. <body>
  402. <form>
  403. <input type="text" name="foobar" value="value1" />
  404. <input type="text" name="foobar" value="value2" />
  405. <input type="text" name="baz" value="value3" />
  406. <input type="text" name="bar" value="value1" />
  407. <input type="text" name="bar" value="value2" />
  408. <input type="radio" name="foo" value="value1" checked />
  409. <input type="radio" name="foo" value="value2" />
  410. <input type="radio" name="foo" value="value3" />
  411. <input type="text" name="disabled" value="disabled" disabled />
  412. <button type="submit">Submit</button>
  413. </form>
  414. </body>
  415. </html>
  416. `));
  417. it('should clear all values given a single key', () => {
  418. utils.test({
  419. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  420. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  421. clearFormValues(form, 'foobar');
  422. expect(getFormValues(form)).toEqual({ foobar: ['', ''], baz: 'value3', bar: ['value1', 'value2'], foo: 'value1', });
  423. },
  424. });
  425. });
  426. it('should clear all values given multiple keys', () => {
  427. utils.test({
  428. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  429. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  430. clearFormValues(form, ['foobar', 'bar', 'foo']);
  431. expect(getFormValues(form)).toEqual({ foobar: ['', ''], baz: 'value3', bar: ['', ''], });
  432. },
  433. });
  434. });
  435. it('should clear all values given disabled option', () => {
  436. utils.test({
  437. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  438. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  439. clearFormValues(form, ['foobar', 'bar', 'foo', 'disabled'], { includeDisabled: true });
  440. expect(getFormValues(form, { includeDisabled: true })).toEqual({
  441. foobar: ['', ''], baz: 'value3', bar: ['', ''],
  442. disabled: '',
  443. });
  444. },
  445. });
  446. });
  447. });
  448. describe('duplicates', () => {
  449. beforeEach(utils.setup(`
  450. <!DOCTYPE html>
  451. <html lang="en-PH">
  452. <head>
  453. <meta charset="UTF-8">
  454. <title>Misc/Blank</title>
  455. </head>
  456. <body>
  457. <form>
  458. <input type="text" name="foobar" />
  459. <input type="text" name="foobar" />
  460. <input type="text" name="foobar" />
  461. <button type="submit">Submit</button>
  462. </form>
  463. </body>
  464. </html>
  465. `));
  466. it('should parse duplicates correctly', () => {
  467. utils.test({
  468. onLoaded: (form: HTMLFormElement) => {
  469. setFormValues(form, { foobar: ['foo', 'bar', 'baz']})
  470. },
  471. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  472. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  473. expect(getFormValues(form)).toEqual({ foobar: ['foo', 'bar', 'baz'], });
  474. },
  475. })
  476. });
  477. });
  478. });