Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

202 строки
4.5 KiB

  1. import { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('text', () => {
  4. describe('basic', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Text/Basic</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="text" name="hello" value="Hi" />
  17. </label>
  18. <button type="submit">Submit</button>
  19. </form>
  20. </body>
  21. </html>
  22. `))
  23. it('should have single form value', () => {
  24. utils.test({
  25. action: (cy: any) => cy.get('[type="submit"]'),
  26. test: (form: HTMLFormElement, submitter: any, search: any) => {
  27. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  28. .toString();
  29. const after = utils.makeSearchParams(search)
  30. .toString();
  31. expect(before)
  32. .toEqual(after);
  33. },
  34. expectedStaticValue: {
  35. hello: 'Hi',
  36. },
  37. });
  38. });
  39. })
  40. describe('disabled', () => {
  41. beforeEach(utils.setup(`
  42. <!DOCTYPE html>
  43. <html lang="en-PH">
  44. <head>
  45. <meta charset="UTF-8">
  46. <title>Text/Disabled</title>
  47. </head>
  48. <body>
  49. <form>
  50. <label>
  51. <span>Hello</span>
  52. <input
  53. type="text" name="hello" value="Hi"
  54. disabled
  55. />
  56. </label>
  57. <button type="submit">Submit</button>
  58. </form>
  59. </body>
  60. </html>
  61. `))
  62. it('should have blank form value', () => {
  63. utils.test({
  64. action: (cy: any) => cy.get('[type="submit"]'),
  65. test: (form: HTMLFormElement, submitter: any, search: any) => {
  66. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  67. .toString();
  68. const after = utils.makeSearchParams(search)
  69. .toString();
  70. expect(before)
  71. .toEqual(after);
  72. },
  73. expectedStaticValue: {},
  74. });
  75. });
  76. })
  77. describe('outside', () => {
  78. beforeEach(utils.setup(`
  79. <!DOCTYPE html>
  80. <html lang="en-PH">
  81. <head>
  82. <meta charset="UTF-8">
  83. <title>Text/Outside</title>
  84. </head>
  85. <body>
  86. <form id="form">
  87. <button type="submit">Submit</button>
  88. </form>
  89. <label>
  90. <span>Hello</span>
  91. <input type="text" name="hello" value="Hi" form="form" />
  92. </label>
  93. </body>
  94. </html>
  95. `))
  96. it('should have single form value', () => {
  97. utils.test({
  98. action: (cy: any) => cy.get('[type="submit"]'),
  99. test: (form: HTMLFormElement, submitter: any, search: any) => {
  100. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  101. .toString();
  102. const after = utils.makeSearchParams(search)
  103. .toString();
  104. expect(before)
  105. .toEqual(after);
  106. },
  107. expectedStaticValue: {
  108. hello: 'Hi',
  109. },
  110. });
  111. });
  112. });
  113. describe('readonly', () => {
  114. beforeEach(utils.setup(`
  115. <!DOCTYPE html>
  116. <html lang="en-PH">
  117. <head>
  118. <meta charset="UTF-8">
  119. <title>Text/Readonly</title>
  120. </head>
  121. <body>
  122. <form>
  123. <label>
  124. <span>Hello</span>
  125. <input
  126. type="text" name="hello" value="Hi"
  127. readonly
  128. />
  129. </label>
  130. <button type="submit">Submit</button>
  131. </form>
  132. </body>
  133. </html>
  134. `))
  135. it('should have single form value', () => {
  136. utils.test({
  137. action: (cy: any) => cy.get('[type="submit"]'),
  138. test: (form: HTMLFormElement, submitter: any, search: any) => {
  139. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  140. .toString();
  141. const after = utils.makeSearchParams(search)
  142. .toString();
  143. expect(before)
  144. .toEqual(after);
  145. },
  146. expectedStaticValue: {
  147. hello: 'Hi',
  148. },
  149. });
  150. });
  151. });
  152. describe('programmatical value setting', () => {
  153. beforeEach(utils.setup(`
  154. <!DOCTYPE html>
  155. <html lang="en-PH">
  156. <head>
  157. <meta charset="UTF-8">
  158. <title>Text/Basic</title>
  159. </head>
  160. <body>
  161. <form>
  162. <label>
  163. <span>Hello</span>
  164. <input type="text" name="hello" />
  165. </label>
  166. <button type="submit">Submit</button>
  167. </form>
  168. </body>
  169. </html>
  170. `));
  171. it('should have form values set', () => {
  172. utils.test({
  173. action: (cy: any) => cy.get('[type="submit"]'),
  174. preAction: (form: HTMLFormElement) => {
  175. setFormValues(form, { hello: 'Hi', })
  176. },
  177. test: (form: HTMLFormElement, submitter: any, search: any) => {
  178. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  179. .toString();
  180. const after = utils.makeSearchParams(search)
  181. .toString();
  182. expect(before)
  183. .toEqual(after);
  184. },
  185. expectedStaticValue: {
  186. hello: 'Hi',
  187. },
  188. });
  189. });
  190. });
  191. })