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.
 
 
 

465 lines
13 KiB

  1. import getFormValuesDeprecated, { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('misc', () => {
  4. describe('core', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Misc/Blank</title>
  11. </head>
  12. <body>
  13. <form>
  14. <button type="submit">Submit</button>
  15. </form>
  16. </body>
  17. </html>
  18. `))
  19. it('should call console.warn for deprecated default import usage', () => {
  20. utils.test({
  21. action: (cy: any) => cy.get('[type="submit"]'),
  22. test: (form: HTMLFormElement, submitter: any, search: any) => {
  23. let consoleWarnCalled = false
  24. const defaultConsoleWarn = console.warn
  25. console.warn = (...args: unknown[]) => {
  26. consoleWarnCalled = true
  27. };
  28. getFormValuesDeprecated(form, { submitter });
  29. expect(consoleWarnCalled).toBe(true);
  30. console.warn = defaultConsoleWarn;
  31. },
  32. });
  33. });
  34. it('should throw an error when providing invalid argument type as form to getFormValues', () => {
  35. utils.test({
  36. action: (cy: any) => cy.get('[type="submit"]'),
  37. test: (form: HTMLFormElement, submitter: any, search: any) => {
  38. let isThrown = false;
  39. try {
  40. getFormValues(0 as unknown as HTMLFormElement, {});
  41. } catch {
  42. isThrown = true;
  43. }
  44. expect(isThrown).toBe(true);
  45. },
  46. });
  47. });
  48. it('should throw an error when providing null as form to getFormValues', () => {
  49. utils.test({
  50. action: (cy: any) => cy.get('[type="submit"]'),
  51. test: (form: HTMLFormElement, submitter: any, search: any) => {
  52. let isThrown = false;
  53. try {
  54. getFormValues(null as unknown as HTMLFormElement, {});
  55. } catch {
  56. isThrown = true;
  57. }
  58. expect(isThrown).toBe(true);
  59. },
  60. });
  61. });
  62. it('should throw an error when providing a different element type as form to getFormValues', () => {
  63. utils.test({
  64. action: (cy: any) => cy.get('[type="submit"]'),
  65. test: (form: HTMLFormElement, submitter: any, search: any) => {
  66. let isThrown = false;
  67. try {
  68. getFormValues(document.body as unknown as HTMLFormElement, {});
  69. } catch {
  70. isThrown = true;
  71. }
  72. expect(isThrown).toBe(true);
  73. },
  74. });
  75. });
  76. it('should throw an error when providing invalid argument type as form to setFormValues', () => {
  77. utils.test({
  78. action: (cy: any) => cy.get('[type="submit"]'),
  79. test: (form: HTMLFormElement, submitter: any, search: any) => {
  80. let isThrown = false;
  81. try {
  82. setFormValues(0 as unknown as HTMLFormElement, {});
  83. } catch {
  84. isThrown = true;
  85. }
  86. expect(isThrown).toBe(true);
  87. },
  88. });
  89. });
  90. it('should throw an error when providing null as form to setFormValues', () => {
  91. utils.test({
  92. action: (cy: any) => cy.get('[type="submit"]'),
  93. test: (form: HTMLFormElement, submitter: any, search: any) => {
  94. let isThrown = false;
  95. try {
  96. setFormValues(null as unknown as HTMLFormElement, {});
  97. } catch {
  98. isThrown = true;
  99. }
  100. expect(isThrown).toBe(true);
  101. },
  102. });
  103. });
  104. it('should throw an error when providing a different element type as form to setFormValues', () => {
  105. utils.test({
  106. action: (cy: any) => cy.get('[type="submit"]'),
  107. test: (form: HTMLFormElement, submitter: any, search: any) => {
  108. let isThrown = false;
  109. try {
  110. setFormValues(document.body as unknown as HTMLFormElement, {});
  111. } catch {
  112. isThrown = true;
  113. }
  114. expect(isThrown).toBe(true);
  115. },
  116. });
  117. });
  118. it('should throw an error when providing invalid argument type as values to setFormValues', () => {
  119. utils.test({
  120. action: (cy: any) => cy.get('[type="submit"]'),
  121. test: (form: HTMLFormElement, submitter: any, search: any) => {
  122. let isThrown = false;
  123. try {
  124. setFormValues(form, 0);
  125. } catch {
  126. isThrown = true;
  127. }
  128. expect(isThrown).toBe(true);
  129. },
  130. });
  131. });
  132. it('should not throw an error when providing null as form to setFormValues', () => {
  133. utils.test({
  134. action: (cy: any) => cy.get('[type="submit"]'),
  135. test: (form: HTMLFormElement, submitter: any, search: any) => {
  136. let isThrown = false;
  137. try {
  138. setFormValues(form, null);
  139. } catch (e) {
  140. isThrown = true;
  141. }
  142. expect(isThrown).toBe(false);
  143. },
  144. });
  145. });
  146. it('should throw an error when providing undefined as form to setFormValues', () => {
  147. utils.test({
  148. action: (cy: any) => cy.get('[type="submit"]'),
  149. test: (form: HTMLFormElement, submitter: any, search: any) => {
  150. let isThrown = false;
  151. try {
  152. setFormValues(form, undefined);
  153. } catch (e) {
  154. isThrown = true;
  155. }
  156. expect(isThrown).toBe(true);
  157. },
  158. });
  159. });
  160. })
  161. describe('blank', () => {
  162. beforeEach(utils.setup(`
  163. <!DOCTYPE html>
  164. <html lang="en-PH">
  165. <head>
  166. <meta charset="UTF-8">
  167. <title>Misc/Blank</title>
  168. </head>
  169. <body>
  170. <form>
  171. <button type="submit">Submit</button>
  172. </form>
  173. </body>
  174. </html>
  175. `))
  176. it('should have blank form value', () => {
  177. utils.test({
  178. action: (cy: any) => cy.get('[type="submit"]'),
  179. test: (form: HTMLFormElement, submitter: any, search: any) => {
  180. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  181. .toString();
  182. const after = utils.makeSearchParams(search)
  183. .toString();
  184. expect(before)
  185. .toEqual(after);
  186. },
  187. expectedStaticValue: {},
  188. });
  189. });
  190. })
  191. describe('everything', () => {
  192. beforeEach(utils.setup(`
  193. <!DOCTYPE html>
  194. <html lang="en-PH">
  195. <head>
  196. <meta charset="UTF-8">
  197. <title>Misc/Everything</title>
  198. </head>
  199. <body>
  200. <article>
  201. <h2></h2>
  202. <form>
  203. <fieldset>
  204. <legend>Name</legend>
  205. <div>
  206. <input type="text" placeholder="First Name" name="first_name" />
  207. </div>
  208. <div>
  209. <input type="text" placeholder="Middle Name" name="middle_name" />
  210. </div>
  211. <div>
  212. <input type="text" placeholder="Last Name" name="last_name" />
  213. </div>
  214. </fieldset>
  215. <fieldset>
  216. <legend>Gender</legend>
  217. <div>
  218. <label>
  219. <input type="radio" name="gender" value="m" />
  220. Male
  221. </label>
  222. <label>
  223. <input type="radio" name="gender" value="f" />
  224. Female
  225. </label>
  226. </div>
  227. </fieldset>
  228. <fieldset>
  229. <legend>Birthday</legend>
  230. <div>
  231. <input type="date" placeholder="Birthday" name="birthday" />
  232. </div>
  233. </fieldset>
  234. <fieldset>
  235. <legend>Civil Status</legend>
  236. <div>
  237. <select name="civil_status">
  238. <option value="">Select Civil Status</option>
  239. <option value="single">Single</option>
  240. <option value="married">Married</option>
  241. <option value="divorced">Divorced</option>
  242. <option value="separated">Separated</option>
  243. </select>
  244. </div>
  245. </fieldset>
  246. <fieldset>
  247. <legend>New Registration</legend>
  248. <div>
  249. <label>
  250. <input type="checkbox" name="new_registration" />
  251. New Registration
  252. </label>
  253. </div>
  254. </fieldset>
  255. <fieldset>
  256. <legend>Last Appointment Date</legend>
  257. <div>
  258. <input type="datetime-local" placeholder="Last Appointment Date" name="last_appointment_datetime" />
  259. </div>
  260. </fieldset>
  261. <fieldset>
  262. <legend>New Appointment Week</legend>
  263. <div>
  264. <input type="week" placeholder="New Appointment Week" name="new_appointment_week" />
  265. </div>
  266. </fieldset>
  267. <fieldset>
  268. <legend>Start Month</legend>
  269. <div>
  270. <input type="month" placeholder="Start Month" name="start_month" />
  271. </div>
  272. </fieldset>
  273. <div>
  274. <label>
  275. <input type="checkbox" value="filipino" name="nationality" />
  276. Filipino
  277. </label>
  278. </div>
  279. <div>
  280. <input type="number" placeholder="Gross Salary" name="gross" />
  281. </div>
  282. <fieldset>
  283. <legend>
  284. Default Dependents
  285. </legend>
  286. <div>
  287. <label>
  288. <input type="radio" value="James" name="dependent" />
  289. James
  290. </label>
  291. <label>
  292. <input type="radio" value="Jun" name="dependent" />
  293. Jun
  294. </label>
  295. </div>
  296. </fieldset>
  297. <div>
  298. <button type="button" class="dependents">
  299. Add Dependents
  300. </button>
  301. </div>
  302. <div>
  303. <textarea name="notes" placeholder="Notes"></textarea>
  304. </div>
  305. <fieldset>
  306. <legend>
  307. Quality of Service
  308. </legend>
  309. <input type="range" min="0" max="10" step="0.5" placeholder="Quality of Service" name="qos" />
  310. </fieldset>
  311. <div>
  312. <button name="submit" value="Hello" type="submit">Hello</button>
  313. <button name="submit" value="Hi" type="submit">Hi</button>
  314. </div>
  315. </form>
  316. </article>
  317. <script>
  318. Array.from(document.getElementsByClassName('dependents')).forEach(d => {
  319. d.addEventListener('click', e => {
  320. const container = document.createElement('div')
  321. const input = document.createElement('input')
  322. input.name = 'dependent'
  323. input.type = 'text'
  324. input.placeholder = 'Dependent'
  325. container.classList.add('additional-dependent')
  326. container.appendChild(input)
  327. e.target.parentElement.parentElement.insertBefore(container, e.target.parentElement)
  328. })
  329. })
  330. </script>
  331. </body>
  332. </html>
  333. `))
  334. it('should have correct form values', () => {
  335. utils.test({
  336. action: (cy) => {
  337. cy.get('[name="first_name"]')
  338. .type('John')
  339. cy.get('[name="middle_name"]')
  340. .type('Marcelo')
  341. cy.get('[name="last_name"]')
  342. .type('Dela Cruz')
  343. cy.get('[name="gender"][value="m"]')
  344. .check()
  345. cy.get('[name="birthday"]')
  346. .type('1989-06-04')
  347. cy.get('[name="civil_status"]')
  348. .select('Married')
  349. cy.get('[name="new_registration"]')
  350. .check()
  351. cy.get('[name="last_appointment_datetime"]')
  352. .type('2001-09-11T06:09')
  353. cy.get('[name="new_appointment_week"]')
  354. .type('2001-W51')
  355. cy.get('[name="start_month"]')
  356. .type('2002-03')
  357. cy.get('[name="nationality"][value="filipino"]')
  358. .check()
  359. cy.get('[name="gross"]')
  360. .type('131072')
  361. cy.get('[name="dependent"][value="Jun"]')
  362. .check()
  363. cy.get('button.dependents')
  364. .click()
  365. cy.get('.additional-dependent [name="dependent"][type="text"]')
  366. .last()
  367. .type('Juana')
  368. cy.get('button.dependents')
  369. .click()
  370. cy.get('.additional-dependent [name="dependent"][type="text"]')
  371. .last()
  372. .type('Jane')
  373. cy.get('button.dependents')
  374. .click()
  375. cy.get('.additional-dependent [name="dependent"][type="text"]')
  376. .last()
  377. .type('Josh')
  378. cy.get('[name="qos"]')
  379. .invoke('val', 9.5)
  380. .trigger('change')
  381. cy.get('[name="notes"]')
  382. .type('Test content\n\nNew line\n\nAnother line')
  383. return cy.get('[name="submit"][value="Hi"]')
  384. },
  385. test: (form: HTMLFormElement, submitter: any, search: any) => {
  386. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  387. .toString();
  388. const after = utils.makeSearchParams(search)
  389. .toString();
  390. expect(before)
  391. .toEqual(after);
  392. },
  393. expectedStaticValue: 'first_name=John&middle_name=Marcelo&last_name=Dela+Cruz&gender=m&birthday=1989-06-04&civil_status=married&new_registration=on&last_appointment_datetime=2001-09-11T06%3A09&new_appointment_week=2001-W51&start_month=2002-03&nationality=filipino&gross=131072&dependent=Jun&notes=Test+content%0D%0A%0D%0ANew+line%0D%0A%0D%0AAnother+line&qos=9.5&submit=Hi',
  394. });
  395. });
  396. it('should have filled form values', () => {
  397. utils.test({
  398. action: (cy) => cy.wait(3000).get('[name="submit"][value="Hi"]'),
  399. test: (form: HTMLFormElement, submitter: any, search: any) => {
  400. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  401. .toString();
  402. const after = utils.makeSearchParams(search)
  403. .toString();
  404. expect(before)
  405. .toEqual(after);
  406. },
  407. preAction: (form: HTMLFormElement) => {
  408. setFormValues(form, {
  409. first_name: 'John',
  410. middle_name: 'Marcelo',
  411. last_name: 'Dela Cruz',
  412. gender: 'm',
  413. birthday: new Date('1989-06-04'),
  414. civil_status: 'married',
  415. new_registration: 'on',
  416. last_appointment_datetime: new Date('2001-09-11T06:09:00'),
  417. new_appointment_week: '2001-W51',
  418. start_month: '2002-03',
  419. nationality: 'filipino',
  420. gross: 131072,
  421. dependent: 'Jun',
  422. notes: `Test content
  423. New line
  424. Another line`,
  425. qos: 9.5,
  426. });
  427. },
  428. expectedStaticValue: 'first_name=John&middle_name=Marcelo&last_name=Dela+Cruz&gender=m&birthday=1989-06-04&civil_status=married&new_registration=on&last_appointment_datetime=2001-09-11T06%3A09&new_appointment_week=2001-W51&start_month=2002-03&nationality=filipino&gross=131072&dependent=Jun&notes=Test+content%0D%0A%0D%0ANew+line%0D%0A%0D%0AAnother+line&qos=9.5&submit=Hi',
  429. });
  430. });
  431. });
  432. // TODO implement tests for multiple values
  433. });