Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('url', () => {
  4. describe('basic', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>URL/Basic</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="url" name="hello" value="https://www.example.com" />
  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. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  26. onSubmitted: (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: 'https://www.example.com',
  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>URL/Disabled</title>
  47. </head>
  48. <body>
  49. <form>
  50. <label>
  51. <span>Hello</span>
  52. <input
  53. type="url" name="hello" value="https://www.example.com"
  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. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  65. onSubmitted: (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>URL/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="url" name="hello" value="https://www.example.com" form="form" />
  92. </label>
  93. </body>
  94. </html>
  95. `))
  96. it('should have single form value', () => {
  97. utils.test({
  98. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  99. onSubmitted: (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: 'https://www.example.com',
  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>URL/Readonly</title>
  120. </head>
  121. <body>
  122. <form>
  123. <label>
  124. <span>Hello</span>
  125. <input
  126. type="url" name="hello" value="https://www.example.com"
  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. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  138. onSubmitted: (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: 'https://www.example.com',
  148. },
  149. });
  150. });
  151. });
  152. describe('programmatic value setting', () => {
  153. beforeEach(utils.setup(`
  154. <!DOCTYPE html>
  155. <html lang="en-PH">
  156. <head>
  157. <meta charset="UTF-8">
  158. <title>URL/Programmatic Value Setting</title>
  159. </head>
  160. <body>
  161. <form>
  162. <label>
  163. <span>Hello</span>
  164. <input type="url" 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. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  174. onLoaded: (form: HTMLFormElement) => {
  175. setFormValues(form, { hello: 'https://www.example.com', })
  176. },
  177. onSubmitted: (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: 'https://www.example.com',
  187. },
  188. });
  189. });
  190. });
  191. describe('duplicate', () => {
  192. beforeEach(utils.setup(`
  193. <!DOCTYPE html>
  194. <html lang="en-PH">
  195. <head>
  196. <meta charset="UTF-8">
  197. <title>URL/Duplicate</title>
  198. </head>
  199. <body>
  200. <form>
  201. <label>
  202. <span>Hello 1</span>
  203. <input id="hello1" type="url" value="https://www.example.com" name="hello"/>
  204. </label>
  205. <label>
  206. <span>Hello 2</span>
  207. <input id="hello2" type="url" value="https://acme.example.com" name="hello"/>
  208. </label>
  209. <button type="submit">Submit</button>
  210. </form>
  211. </body>
  212. </html>
  213. `));
  214. it('should get both values', () => {
  215. utils.test({
  216. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  217. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  218. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  219. .toString();
  220. const after = utils.makeSearchParams(search)
  221. .toString();
  222. expect(before)
  223. .toEqual(after);
  224. },
  225. expectedStaticValue: {
  226. hello: ['https://www.example.com', 'https://acme.example.com'],
  227. },
  228. });
  229. });
  230. it('should set both values', () => {
  231. utils.test({
  232. onLoaded: (form: HTMLFormElement) => {
  233. setFormValues(form, {
  234. hello: ['https://foo.example.com', 'https://bar.example.com/context'],
  235. })
  236. },
  237. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  238. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  239. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  240. .toString();
  241. const after = utils.makeSearchParams(search)
  242. .toString();
  243. expect(before)
  244. .toEqual(after);
  245. },
  246. expectedStaticValue: {
  247. hello: ['https://foo.example.com', 'https://bar.example.com/context'],
  248. },
  249. });
  250. });
  251. });
  252. })