Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

283 rindas
6.9 KiB

  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. actionBeforeSubmit: (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. actionBeforeSubmit: (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. actionBeforeSubmit: (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. actionBeforeSubmit: (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. actionBeforeSubmit: (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. actionBeforeSubmit: (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. actionBeforeSubmit: (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. actionBeforeSubmit: (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. })