Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

252 linhas
7.3 KiB

  1. import { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('misc', () => {
  4. describe('blank', () => {
  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 have blank form value', () => {
  20. utils.test({
  21. action: (cy: any) => cy.get('[type="submit"]'),
  22. test: (form: HTMLFormElement, submitter: any, search: any) => {
  23. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  24. .toString();
  25. const after = utils.makeSearchParams(search)
  26. .toString();
  27. expect(before)
  28. .toEqual(after);
  29. },
  30. expectedStaticValue: {},
  31. });
  32. });
  33. })
  34. describe('everything', () => {
  35. beforeEach(utils.setup(`
  36. <!DOCTYPE html>
  37. <html lang="en-PH">
  38. <head>
  39. <meta charset="UTF-8">
  40. <title>Misc/Everything</title>
  41. </head>
  42. <body>
  43. <article>
  44. <h2></h2>
  45. <form>
  46. <div>
  47. <input type="text" placeholder="First Name" name="first_name" />
  48. </div>
  49. <div>
  50. <input type="text" placeholder="Middle Name" name="middle_name" />
  51. </div>
  52. <div>
  53. <input type="text" placeholder="Last Name" name="last_name" />
  54. </div>
  55. <fieldset>
  56. <legend>Gender</legend>
  57. <div>
  58. <label>
  59. <input type="radio" name="gender" value="m" />
  60. Male
  61. </label>
  62. <label>
  63. <input type="radio" name="gender" value="f" />
  64. Female
  65. </label>
  66. </div>
  67. </fieldset>
  68. <div>
  69. <input type="date" placeholder="Birthday" name="birthday" />
  70. </div>
  71. <div>
  72. <select name="civil_status">
  73. <option value="">Select Civil Status</option>
  74. <option value="single">Single</option>
  75. <option value="married">Married</option>
  76. <option value="divorced">Divorced</option>
  77. <option value="separated">Separated</option>
  78. </select>
  79. </div>
  80. <div>
  81. <label>
  82. <input type="checkbox" name="new_registration" />
  83. New Registration
  84. </label>
  85. </div>
  86. <div>
  87. <input type="datetime-local" placeholder="Appointment Date/Time" name="appointment_datetime" />
  88. </div>
  89. <div>
  90. <label>
  91. <input type="checkbox" value="filipino" name="nationality" />
  92. Filipino
  93. </label>
  94. </div>
  95. <div>
  96. <input type="number" placeholder="Gross Salary" name="gross" />
  97. </div>
  98. <fieldset>
  99. <legend>
  100. Default Dependents
  101. </legend>
  102. <div>
  103. <label>
  104. <input type="radio" value="James" name="dependent" />
  105. James
  106. </label>
  107. <label>
  108. <input type="radio" value="Jun" name="dependent" />
  109. Jun
  110. </label>
  111. </div>
  112. </fieldset>
  113. <div>
  114. <button type="button" class="dependents">
  115. Add Dependents
  116. </button>
  117. </div>
  118. <div>
  119. <textarea name="notes" placeholder="Notes"></textarea>
  120. </div>
  121. <div>
  122. <label>
  123. Quality of Service
  124. <input type="range" min="0" max="10" placeholder="Quality of Service" name="qos" />
  125. </label>
  126. </div>
  127. <div>
  128. <button name="submit" value="Hello" type="submit">Hello</button>
  129. <button name="submit" value="Hi" type="submit">Hi</button>
  130. </div>
  131. </form>
  132. </article>
  133. <script>
  134. Array.from(document.getElementsByClassName('dependents')).forEach(d => {
  135. d.addEventListener('click', e => {
  136. const container = document.createElement('div')
  137. const input = document.createElement('input')
  138. input.name = 'dependent'
  139. input.type = 'text'
  140. input.placeholder = 'Dependent'
  141. container.classList.add('additional-dependent')
  142. container.appendChild(input)
  143. e.target.parentElement.parentElement.insertBefore(container, e.target.parentElement)
  144. })
  145. })
  146. </script>
  147. </body>
  148. </html>
  149. `))
  150. it('should have correct form values', () => {
  151. utils.test({
  152. action: (cy) => {
  153. cy.get('[name="first_name"]')
  154. .type('John')
  155. cy.get('[name="middle_name"]')
  156. .type('Marcelo')
  157. cy.get('[name="last_name"]')
  158. .type('Dela Cruz')
  159. cy.get('[name="gender"][value="m"]')
  160. .check()
  161. cy.get('[name="birthday"]')
  162. .type('1989-06-04')
  163. cy.get('[name="civil_status"]')
  164. .select('Married')
  165. cy.get('[name="new_registration"]')
  166. .check()
  167. cy.get('[name="appointment_datetime"]')
  168. .type('2001-09-11T06:09')
  169. cy.get('[name="nationality"][value="filipino"]')
  170. .check()
  171. cy.get('[name="gross"]')
  172. .type('131072')
  173. cy.get('[name="dependent"][value="Jun"]')
  174. .check()
  175. cy.get('button.dependents')
  176. .click()
  177. cy.get('.additional-dependent [name="dependent"][type="text"]')
  178. .last()
  179. .type('Juana')
  180. cy.get('button.dependents')
  181. .click()
  182. cy.get('.additional-dependent [name="dependent"][type="text"]')
  183. .last()
  184. .type('Jane')
  185. cy.get('button.dependents')
  186. .click()
  187. cy.get('.additional-dependent [name="dependent"][type="text"]')
  188. .last()
  189. .type('Josh')
  190. cy.get('[name="qos"]')
  191. .invoke('val', 10)
  192. .trigger('change')
  193. cy.get('[name="notes"]')
  194. .type('Test content\n\nNew line\n\nAnother line')
  195. return cy.get('[name="submit"][value="Hi"]')
  196. },
  197. test: (form: HTMLFormElement, submitter: any, search: any) => {
  198. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  199. .toString();
  200. const after = utils.makeSearchParams(search)
  201. .toString();
  202. expect(before)
  203. .toEqual(after);
  204. },
  205. expectedStaticValue: 'first_name=John&middle_name=Marcelo&last_name=Dela+Cruz&gender=m&birthday=1989-06-04&civil_status=married&new_registration=on&appointment_datetime=2001-09-11T06%3A09&nationality=filipino&gross=131072&dependent=Jun&notes=Test+content%0D%0A%0D%0ANew+line%0D%0A%0D%0AAnother+line&qos=10&submit=Hi',
  206. });
  207. });
  208. it('should have filled form values', () => {
  209. utils.test({
  210. action: (cy) => cy.wait(3000).get('[name="submit"][value="Hi"]'),
  211. test: (form: HTMLFormElement, submitter: any, search: any) => {
  212. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  213. .toString();
  214. const after = utils.makeSearchParams(search)
  215. .toString();
  216. expect(before)
  217. .toEqual(after);
  218. },
  219. preAction: (form: HTMLFormElement) => {
  220. setFormValues(form, {
  221. first_name: 'John',
  222. middle_name: 'Marcelo',
  223. last_name: 'Dela Cruz',
  224. gender: 'm',
  225. birthday: new Date('1989-06-04'),
  226. civil_status: 'married',
  227. new_registration: 'on',
  228. appointment_datetime: new Date('2001-09-11T06:09:00'),
  229. nationality: 'filipino',
  230. gross: 131072,
  231. dependent: 'Jun',
  232. notes: `Test content
  233. New line
  234. Another line`,
  235. qos: 10,
  236. });
  237. },
  238. expectedStaticValue: 'first_name=John&middle_name=Marcelo&last_name=Dela+Cruz&gender=m&birthday=1989-06-04&civil_status=married&new_registration=on&appointment_datetime=2001-09-11T06%3A09&nationality=filipino&gross=131072&dependent=Jun&notes=Test+content%0D%0A%0D%0ANew+line%0D%0A%0D%0AAnother+line&qos=10&submit=Hi',
  239. });
  240. });
  241. })
  242. })