Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

hidden.test.ts 6.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('hidden', () => {
  4. describe('basic', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Hidden/Basic</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="hidden" 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. 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: 'Hi',
  36. },
  37. });
  38. });
  39. });
  40. describe('charset', () => {
  41. beforeEach(utils.setup(`
  42. <!DOCTYPE html>
  43. <html lang="en-PH">
  44. <head>
  45. <meta charset="UTF-8">
  46. <title>Hidden/Charset</title>
  47. </head>
  48. <body>
  49. <form>
  50. <label>
  51. <span>Hello</span>
  52. <input type="hidden" name="_charset_" />
  53. </label>
  54. <button type="submit">Submit</button>
  55. </form>
  56. </body>
  57. </html>
  58. `))
  59. it('should have extra value for character set', () => {
  60. utils.test({
  61. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  62. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  63. const before = utils.makeSearchParams(getFormValues(form, { submitter, includeCharset: true, }))
  64. .toString();
  65. const after = utils.makeSearchParams(search)
  66. .toString();
  67. expect(before)
  68. .toEqual(after);
  69. },
  70. expectedStaticValue: {
  71. _charset_: 'UTF-8',
  72. },
  73. });
  74. });
  75. it('should not be able to set extra value for character set', () => {
  76. utils.test({
  77. onLoaded: (form: HTMLFormElement) => {
  78. setFormValues(form, { _charset_: 'Shift-JIS' });
  79. },
  80. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  81. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  82. const before = utils.makeSearchParams(getFormValues(form, { submitter, includeCharset: true, }))
  83. .toString();
  84. const after = utils.makeSearchParams(search)
  85. .toString();
  86. expect(before)
  87. .toEqual(after);
  88. },
  89. expectedStaticValue: {
  90. _charset_: 'UTF-8',
  91. },
  92. });
  93. });
  94. });
  95. describe('disabled', () => {
  96. beforeEach(utils.setup(`
  97. <!DOCTYPE html>
  98. <html lang="en-PH">
  99. <head>
  100. <meta charset="UTF-8">
  101. <title>Hidden/Disabled</title>
  102. </head>
  103. <body>
  104. <form>
  105. <label>
  106. <span>Hello</span>
  107. <input
  108. type="hidden" name="hello" value="Hi"
  109. disabled
  110. />
  111. </label>
  112. <button type="submit">Submit</button>
  113. </form>
  114. </body>
  115. </html>
  116. `))
  117. it('should have blank form value', () => {
  118. utils.test({
  119. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  120. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  121. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  122. .toString();
  123. const after = utils.makeSearchParams(search)
  124. .toString();
  125. expect(before)
  126. .toEqual(after);
  127. },
  128. expectedStaticValue: {},
  129. });
  130. });
  131. })
  132. describe('outside', () => {
  133. beforeEach(utils.setup(`
  134. <!DOCTYPE html>
  135. <html lang="en-PH">
  136. <head>
  137. <meta charset="UTF-8">
  138. <title>Hidden/Outside</title>
  139. </head>
  140. <body>
  141. <form id="form">
  142. <button type="submit">Submit</button>
  143. </form>
  144. <label>
  145. <span>Hello</span>
  146. <input type="hidden" name="hello" value="Hi" form="form" />
  147. </label>
  148. </body>
  149. </html>
  150. `))
  151. it('should have single form value', () => {
  152. utils.test({
  153. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  154. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  155. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  156. .toString();
  157. const after = utils.makeSearchParams(search)
  158. .toString();
  159. expect(before)
  160. .toEqual(after);
  161. },
  162. expectedStaticValue: {
  163. hello: 'Hi',
  164. },
  165. });
  166. });
  167. });
  168. describe('programmatic value setting', () => {
  169. beforeEach(utils.setup(`
  170. <!DOCTYPE html>
  171. <html lang="en-PH">
  172. <head>
  173. <meta charset="UTF-8">
  174. <title>Hidden/Programmatic Value Setting</title>
  175. </head>
  176. <body>
  177. <form>
  178. <label>
  179. <span>Hello</span>
  180. <input type="hidden" name="hello" />
  181. </label>
  182. <button type="submit">Submit</button>
  183. </form>
  184. </body>
  185. </html>
  186. `));
  187. it('should have form values set', () => {
  188. utils.test({
  189. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  190. onLoaded: (form: HTMLFormElement) => {
  191. setFormValues(form, { hello: 'Hi', })
  192. },
  193. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  194. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  195. .toString();
  196. const after = utils.makeSearchParams(search)
  197. .toString();
  198. expect(before)
  199. .toEqual(after);
  200. },
  201. expectedStaticValue: {
  202. hello: 'Hi',
  203. },
  204. });
  205. });
  206. });
  207. describe('duplicate', () => {
  208. beforeEach(utils.setup(`
  209. <!DOCTYPE html>
  210. <html lang="en-PH">
  211. <head>
  212. <meta charset="UTF-8">
  213. <title>Hidden/Duplicate</title>
  214. </head>
  215. <body>
  216. <form>
  217. <label>
  218. <span>Hello 1</span>
  219. <input id="hello1" type="hidden" value="value" name="hello"/>
  220. </label>
  221. <label>
  222. <span>Hello 2</span>
  223. <input id="hello2" type="hidden" value="another value" name="hello"/>
  224. </label>
  225. <button type="submit">Submit</button>
  226. </form>
  227. </body>
  228. </html>
  229. `));
  230. it('should get both values', () => {
  231. utils.test({
  232. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  233. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  234. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  235. .toString();
  236. const after = utils.makeSearchParams(search)
  237. .toString();
  238. expect(before)
  239. .toEqual(after);
  240. },
  241. expectedStaticValue: {
  242. hello: ['value', 'another value'],
  243. },
  244. });
  245. });
  246. it('should set both values', () => {
  247. utils.test({
  248. onLoaded: (form: HTMLFormElement) => {
  249. setFormValues(form, {
  250. hello: ['new value 1', 'another value 2'],
  251. })
  252. },
  253. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  254. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  255. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  256. .toString();
  257. const after = utils.makeSearchParams(search)
  258. .toString();
  259. expect(before)
  260. .toEqual(after);
  261. },
  262. expectedStaticValue: {
  263. hello: ['new value 1', 'another value 2'],
  264. },
  265. });
  266. });
  267. });
  268. })