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.
 
 
 

492 lines
14 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. <button type="submit">Submit</button>
  309. </form>
  310. </body>
  311. </html>
  312. `))
  313. it('should parse string values for setFormValues', () => {
  314. utils.test({
  315. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  316. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  317. let isThrown = false;
  318. try {
  319. setFormValues(form, 'foobar=baz');
  320. } catch (e) {
  321. isThrown = true;
  322. }
  323. expect(isThrown).toBe(false);
  324. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  325. },
  326. })
  327. });
  328. it('should parse entries values for setFormValues', () => {
  329. utils.test({
  330. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  331. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  332. let isThrown = false;
  333. try {
  334. setFormValues(form, [['foobar', 'baz']]);
  335. } catch (e) {
  336. isThrown = true;
  337. }
  338. expect(isThrown).toBe(false);
  339. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  340. },
  341. })
  342. });
  343. it('should parse URLSearchParams values for setFormValues', () => {
  344. utils.test({
  345. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  346. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  347. let isThrown = false;
  348. try {
  349. setFormValues(form, new URLSearchParams('foobar=baz'));
  350. } catch (e) {
  351. isThrown = true;
  352. }
  353. expect(isThrown).toBe(false);
  354. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  355. },
  356. })
  357. });
  358. it('should parse object values for setFormValues', () => {
  359. utils.test({
  360. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  361. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  362. let isThrown = false;
  363. try {
  364. setFormValues(form, { foobar: 'baz', });
  365. } catch (e) {
  366. isThrown = true;
  367. }
  368. expect(isThrown).toBe(false);
  369. expect(getFormValues(form)).toEqual({ foobar: 'baz', });
  370. },
  371. })
  372. });
  373. });
  374. describe('clearing values', () => {
  375. beforeEach(utils.setup(`
  376. <!DOCTYPE html>
  377. <html lang="en-PH">
  378. <head>
  379. <meta charset="UTF-8">
  380. <title>Misc/Blank</title>
  381. </head>
  382. <body>
  383. <form>
  384. <input type="text" name="foobar" value="value1" />
  385. <input type="text" name="foobar" value="value2" />
  386. <input type="text" name="baz" value="value3" />
  387. <input type="text" name="bar" value="value1" />
  388. <input type="text" name="bar" value="value2" />
  389. <input type="radio" name="foo" value="value1" checked />
  390. <input type="radio" name="foo" value="value2" />
  391. <input type="radio" name="foo" value="value3" />
  392. <button type="submit">Submit</button>
  393. </form>
  394. </body>
  395. </html>
  396. `));
  397. it('should clear all values given a single key', () => {
  398. utils.test({
  399. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  400. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  401. clearFormValues(form, 'foobar');
  402. expect(getFormValues(form)).toEqual({ foobar: ['', ''], baz: 'value3', bar: ['value1', 'value2'], foo: 'value1', });
  403. },
  404. });
  405. });
  406. it('should clear all values given multiple keys', () => {
  407. utils.test({
  408. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  409. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  410. clearFormValues(form, ['foobar', 'bar', 'foo']);
  411. expect(getFormValues(form)).toEqual({ foobar: ['', ''], baz: 'value3', bar: ['', ''], });
  412. },
  413. });
  414. });
  415. });
  416. describe('duplicates', () => {
  417. beforeEach(utils.setup(`
  418. <!DOCTYPE html>
  419. <html lang="en-PH">
  420. <head>
  421. <meta charset="UTF-8">
  422. <title>Misc/Blank</title>
  423. </head>
  424. <body>
  425. <form>
  426. <input type="text" name="foobar" />
  427. <input type="text" name="foobar" />
  428. <input type="text" name="foobar" />
  429. <button type="submit">Submit</button>
  430. </form>
  431. </body>
  432. </html>
  433. `));
  434. it('should parse duplicates correctly', () => {
  435. utils.test({
  436. onLoaded: (form: HTMLFormElement) => {
  437. setFormValues(form, { foobar: ['foo', 'bar', 'baz']})
  438. },
  439. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  440. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  441. expect(getFormValues(form)).toEqual({ foobar: ['foo', 'bar', 'baz'], });
  442. },
  443. })
  444. });
  445. });
  446. });