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.
 
 
 

524 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. <button type="submit">Submit</button>
  231. </form>
  232. </body>
  233. </html>
  234. `));
  235. it('should check for valid field elements value', () => {
  236. utils.test({
  237. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  238. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  239. const meter = document.getElementById('meter');
  240. expect(getValue(meter)).toBe(5);
  241. },
  242. });
  243. });
  244. it('should check for invalid field elements value', () => {
  245. utils.test({
  246. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  247. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  248. expect(getValue(document.body)).toBe(null);
  249. },
  250. });
  251. });
  252. it('should check for elements as included fields', () => {
  253. utils.test({
  254. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  255. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  256. const input = document.getElementById('input');
  257. expect(isElementValueIncludedInFormSubmit(input)).toBe(true);
  258. },
  259. });
  260. });
  261. it('should check for elements as excluded fields', () => {
  262. utils.test({
  263. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  264. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  265. const notField = document.getElementById('notField');
  266. expect(isElementValueIncludedInFormSubmit(notField)).toBe(false);
  267. const disabled = document.getElementById('disabled');
  268. expect(isElementValueIncludedInFormSubmit(disabled)).toBe(false);
  269. const meter = document.getElementById('meter');
  270. expect(isElementValueIncludedInFormSubmit(meter)).toBe(false);
  271. },
  272. });
  273. });
  274. it('should check for elements as valid for fields', () => {
  275. utils.test({
  276. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  277. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  278. const input = document.getElementById('input');
  279. expect(isFieldElement(input)).toBe(true);
  280. const disabled = document.getElementById('disabled');
  281. expect(isFieldElement(disabled)).toBe(true);
  282. },
  283. });
  284. });
  285. it('should check for elements as invalid for fields', () => {
  286. utils.test({
  287. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  288. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  289. const meter = document.getElementById('meter');
  290. expect(isFieldElement(meter)).toBe(false);
  291. const notField = document.getElementById('notField');
  292. expect(isFieldElement(notField)).toBe(false);
  293. },
  294. });
  295. });
  296. });
  297. describe('setting values', () => {
  298. beforeEach(utils.setup(`
  299. <!DOCTYPE html>
  300. <html lang="en-PH">
  301. <head>
  302. <meta charset="UTF-8">
  303. <title>Misc/Blank</title>
  304. </head>
  305. <body>
  306. <form>
  307. <input type="text" name="foobar" />
  308. <input type="text" name="disabled" disabled />
  309. <button type="submit">Submit</button>
  310. </form>
  311. </body>
  312. </html>
  313. `))
  314. it('should parse string values for setFormValues', () => {
  315. utils.test({
  316. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  317. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  318. let isThrown = false;
  319. try {
  320. setFormValues(form, 'foobar=baz');
  321. } catch (e) {
  322. isThrown = true;
  323. }
  324. expect(isThrown).toBe(false);
  325. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  326. },
  327. })
  328. });
  329. it('should parse entries values for setFormValues', () => {
  330. utils.test({
  331. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  332. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  333. let isThrown = false;
  334. try {
  335. setFormValues(form, [['foobar', 'baz']]);
  336. } catch (e) {
  337. isThrown = true;
  338. }
  339. expect(isThrown).toBe(false);
  340. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  341. },
  342. })
  343. });
  344. it('should parse URLSearchParams values for setFormValues', () => {
  345. utils.test({
  346. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  347. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  348. let isThrown = false;
  349. try {
  350. setFormValues(form, new URLSearchParams('foobar=baz'));
  351. } catch (e) {
  352. isThrown = true;
  353. }
  354. expect(isThrown).toBe(false);
  355. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  356. },
  357. })
  358. });
  359. it('should parse object values for setFormValues', () => {
  360. utils.test({
  361. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  362. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  363. let isThrown = false;
  364. try {
  365. setFormValues(form, { foobar: 'baz', });
  366. } catch (e) {
  367. isThrown = true;
  368. }
  369. expect(isThrown).toBe(false);
  370. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  371. },
  372. })
  373. });
  374. it('should allow setting values for disabled elements', () => {
  375. utils.test({
  376. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  377. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  378. let isThrown = false;
  379. try {
  380. setFormValues(form, { foobar: 'baz', disabled: 'new value' }, { includeDisabled: true });
  381. } catch (e) {
  382. isThrown = true;
  383. }
  384. expect(isThrown).toBe(false);
  385. expect(getFormValues(form, { includeDisabled: true })).toEqual({ foobar: 'baz', disabled: 'new value', });
  386. },
  387. })
  388. });
  389. });
  390. describe('clearing values', () => {
  391. beforeEach(utils.setup(`
  392. <!DOCTYPE html>
  393. <html lang="en-PH">
  394. <head>
  395. <meta charset="UTF-8">
  396. <title>Misc/Blank</title>
  397. </head>
  398. <body>
  399. <form>
  400. <input type="text" name="foobar" value="value1" />
  401. <input type="text" name="foobar" value="value2" />
  402. <input type="text" name="baz" value="value3" />
  403. <input type="text" name="bar" value="value1" />
  404. <input type="text" name="bar" value="value2" />
  405. <input type="radio" name="foo" value="value1" checked />
  406. <input type="radio" name="foo" value="value2" />
  407. <input type="radio" name="foo" value="value3" />
  408. <input type="text" name="disabled" value="disabled" disabled />
  409. <button type="submit">Submit</button>
  410. </form>
  411. </body>
  412. </html>
  413. `));
  414. it('should clear all values given a single key', () => {
  415. utils.test({
  416. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  417. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  418. clearFormValues(form, 'foobar');
  419. expect(getFormValues(form)).toEqual({ foobar: ['', ''], baz: 'value3', bar: ['value1', 'value2'], foo: 'value1', });
  420. },
  421. });
  422. });
  423. it('should clear all values given multiple keys', () => {
  424. utils.test({
  425. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  426. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  427. clearFormValues(form, ['foobar', 'bar', 'foo']);
  428. expect(getFormValues(form)).toEqual({ foobar: ['', ''], baz: 'value3', bar: ['', ''], });
  429. },
  430. });
  431. });
  432. it('should clear all values given disabled option', () => {
  433. utils.test({
  434. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  435. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  436. clearFormValues(form, ['foobar', 'bar', 'foo', 'disabled'], { includeDisabled: true });
  437. expect(getFormValues(form, { includeDisabled: true })).toEqual({
  438. foobar: ['', ''], baz: 'value3', bar: ['', ''],
  439. disabled: '',
  440. });
  441. },
  442. });
  443. });
  444. });
  445. describe('duplicates', () => {
  446. beforeEach(utils.setup(`
  447. <!DOCTYPE html>
  448. <html lang="en-PH">
  449. <head>
  450. <meta charset="UTF-8">
  451. <title>Misc/Blank</title>
  452. </head>
  453. <body>
  454. <form>
  455. <input type="text" name="foobar" />
  456. <input type="text" name="foobar" />
  457. <input type="text" name="foobar" />
  458. <button type="submit">Submit</button>
  459. </form>
  460. </body>
  461. </html>
  462. `));
  463. it('should parse duplicates correctly', () => {
  464. utils.test({
  465. onLoaded: (form: HTMLFormElement) => {
  466. setFormValues(form, { foobar: ['foo', 'bar', 'baz']})
  467. },
  468. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  469. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  470. expect(getFormValues(form)).toEqual({ foobar: ['foo', 'bar', 'baz'], });
  471. },
  472. })
  473. });
  474. });
  475. });