Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

385 satır
11 KiB

  1. import { getFormValues, setFormValues } from '../../src';
  2. import * as utils from '../utils'
  3. describe('checkbox', () => {
  4. describe('basic', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Checkbox/Basic</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="checkbox" name="enabled" />
  17. </label>
  18. <button type="submit">Submit</button>
  19. </form>
  20. </body>
  21. </html>
  22. `))
  23. it('should have no form values', () => {
  24. utils.test({
  25. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  26. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  27. const values = getFormValues(form, { submitter })
  28. const before = utils.makeSearchParams(values)
  29. .toString();
  30. const after = utils.makeSearchParams(search)
  31. .toString();
  32. expect(values['enabled'])
  33. .toBeUndefined();
  34. expect(before)
  35. .toEqual(after);
  36. },
  37. expectedStaticValue: '',
  38. });
  39. });
  40. it('should have false checked value', () => {
  41. utils.test({
  42. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  43. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  44. const values = getFormValues(form,
  45. {
  46. submitter,
  47. booleanValuelessCheckbox: true
  48. }
  49. )
  50. expect(values['enabled'])
  51. .toBe(false);
  52. }
  53. });
  54. });
  55. });
  56. describe('checked', () => {
  57. beforeEach(utils.setup(`
  58. <!DOCTYPE html>
  59. <html lang="en-PH">
  60. <head>
  61. <meta charset="UTF-8">
  62. <title>Checkbox/Checked</title>
  63. </head>
  64. <body>
  65. <form>
  66. <label>
  67. <span>Hello</span>
  68. <input type="checkbox" name="enabled" checked />
  69. </label>
  70. <button type="submit">Submit</button>
  71. </form>
  72. </body>
  73. </html>
  74. `))
  75. it('should have single form value on a single field', () => {
  76. utils.test({
  77. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  78. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  79. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  80. .toString();
  81. const after = utils.makeSearchParams(search)
  82. .toString();
  83. expect(before)
  84. .toEqual(after);
  85. },
  86. expectedStaticValue: 'enabled=on',
  87. });
  88. });
  89. });
  90. describe('duplicate', () => {
  91. beforeEach(utils.setup(`
  92. <!DOCTYPE html>
  93. <html lang="en-PH">
  94. <head>
  95. <meta charset="UTF-8">
  96. <title>Checkbox/Duplicate</title>
  97. </head>
  98. <body>
  99. <form>
  100. <label>
  101. <span>Hello 1</span>
  102. <input type="checkbox" name="enabled" value="hello 1" checked />
  103. </label>
  104. <label>
  105. <span>Hello 2</span>
  106. <input type="checkbox" name="enabled" value="hello 2" checked />
  107. </label>
  108. <label>
  109. <span>Hello 3</span>
  110. <input type="checkbox" name="enabled" value="hello 3" />
  111. </label>
  112. <label>
  113. <span>Hello 4</span>
  114. <input type="checkbox" name="enabled" value="hello 4" />
  115. </label>
  116. <button type="submit">Submit</button>
  117. </form>
  118. </body>
  119. </html>
  120. `));
  121. it('should get both values', () => {
  122. utils.test({
  123. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  124. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  125. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  126. .toString();
  127. const after = utils.makeSearchParams(search)
  128. .toString();
  129. expect(before)
  130. .toEqual(after);
  131. },
  132. expectedStaticValue: {
  133. enabled: ['hello 1', 'hello 2'],
  134. },
  135. });
  136. });
  137. it('should set both values', () => {
  138. utils.test({
  139. onLoaded: (form: HTMLFormElement) => {
  140. setFormValues(form, {
  141. enabled: ['hello 3', 'hello 4'],
  142. })
  143. },
  144. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  145. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  146. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  147. .toString();
  148. const after = utils.makeSearchParams(search)
  149. .toString();
  150. expect(before)
  151. .toEqual(after);
  152. },
  153. expectedStaticValue: {
  154. enabled: ['hello 3', 'hello 4'],
  155. },
  156. });
  157. });
  158. });
  159. describe('setting values', () => {
  160. beforeEach(utils.setup(`
  161. <!DOCTYPE html>
  162. <html lang="en-PH">
  163. <head>
  164. <meta charset="UTF-8">
  165. <title>Checkbox/Setting Values</title>
  166. </head>
  167. <body>
  168. <form>
  169. <label>
  170. <span>Hello</span>
  171. <input type="checkbox" name="enabled" />
  172. </label>
  173. <button type="submit">Submit</button>
  174. </form>
  175. </body>
  176. </html>
  177. `))
  178. it('should check for boolean "true"', () => {
  179. utils.test({
  180. onLoaded: (form: HTMLFormElement) => {
  181. setFormValues(form, { enabled: true, })
  182. },
  183. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  184. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  185. const values = getFormValues(form, { submitter })
  186. expect(values['enabled']).toBe('on');
  187. },
  188. expectedStaticValue: 'enabled=on',
  189. });
  190. });
  191. it('should check for string "true"', () => {
  192. utils.test({
  193. onLoaded: (form: HTMLFormElement) => {
  194. setFormValues(form, { enabled: 'true', })
  195. },
  196. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  197. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  198. const values = getFormValues(form, { submitter })
  199. expect(values['enabled']).toBe('on');
  200. },
  201. expectedStaticValue: 'enabled=on',
  202. });
  203. });
  204. it('should check for string "yes"', () => {
  205. utils.test({
  206. onLoaded: (form: HTMLFormElement) => {
  207. setFormValues(form, { enabled: 'yes', })
  208. },
  209. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  210. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  211. const values = getFormValues(form, { submitter })
  212. expect(values['enabled']).toBe('on');
  213. },
  214. expectedStaticValue: 'enabled=on',
  215. });
  216. });
  217. it('should check for string "on"', () => {
  218. utils.test({
  219. onLoaded: (form: HTMLFormElement) => {
  220. setFormValues(form, { enabled: 'on', })
  221. },
  222. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  223. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  224. const values = getFormValues(form, { submitter })
  225. expect(values['enabled']).toBe('on');
  226. },
  227. expectedStaticValue: 'enabled=on',
  228. });
  229. });
  230. it('should uncheck for boolean "false"', () => {
  231. utils.test({
  232. onLoaded: (form: HTMLFormElement) => {
  233. setFormValues(form, { enabled: false, })
  234. },
  235. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  236. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  237. const values = getFormValues(form, { submitter })
  238. expect(values['enabled']).toBeUndefined()
  239. },
  240. expectedStaticValue: '',
  241. });
  242. });
  243. it('should uncheck for string "false"', () => {
  244. utils.test({
  245. onLoaded: (form: HTMLFormElement) => {
  246. setFormValues(form, { enabled: 'false', })
  247. },
  248. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  249. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  250. const values = getFormValues(form, { submitter })
  251. expect(values['enabled']).toBeUndefined()
  252. },
  253. expectedStaticValue: '',
  254. });
  255. });
  256. it('should uncheck for string "no"', () => {
  257. utils.test({
  258. onLoaded: (form: HTMLFormElement) => {
  259. setFormValues(form, { enabled: 'no', })
  260. },
  261. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  262. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  263. const values = getFormValues(form, { submitter })
  264. expect(values['enabled']).toBeUndefined()
  265. },
  266. expectedStaticValue: '',
  267. });
  268. });
  269. it('should uncheck for string "off"', () => {
  270. utils.test({
  271. onLoaded: (form: HTMLFormElement) => {
  272. setFormValues(form, { enabled: 'off', })
  273. },
  274. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  275. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  276. const values = getFormValues(form, { submitter })
  277. expect(values['enabled']).toBeUndefined()
  278. },
  279. expectedStaticValue: '',
  280. });
  281. });
  282. it('should check for number "1"', () => {
  283. utils.test({
  284. onLoaded: (form: HTMLFormElement) => {
  285. setFormValues(form, { enabled: 1, })
  286. },
  287. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  288. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  289. const values = getFormValues(form, { submitter })
  290. expect(values['enabled']).toBe('on');
  291. },
  292. expectedStaticValue: 'enabled=on',
  293. });
  294. });
  295. it('should check for string "1"', () => {
  296. utils.test({
  297. onLoaded: (form: HTMLFormElement) => {
  298. setFormValues(form, { enabled: '1', })
  299. },
  300. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  301. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  302. const values = getFormValues(form, { submitter })
  303. expect(values['enabled']).toBe('on');
  304. },
  305. expectedStaticValue: 'enabled=on',
  306. });
  307. });
  308. it('should uncheck for number "0"', () => {
  309. utils.test({
  310. onLoaded: (form: HTMLFormElement) => {
  311. setFormValues(form, { enabled: 0, })
  312. },
  313. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  314. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  315. const values = getFormValues(form, { submitter })
  316. expect(values['enabled']).toBeUndefined()
  317. },
  318. expectedStaticValue: '',
  319. });
  320. });
  321. it('should uncheck for string "0"', () => {
  322. utils.test({
  323. onLoaded: (form: HTMLFormElement) => {
  324. setFormValues(form, { enabled: '0', })
  325. },
  326. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  327. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  328. const values = getFormValues(form, { submitter })
  329. expect(values['enabled']).toBeUndefined()
  330. },
  331. expectedStaticValue: '',
  332. });
  333. });
  334. it('should uncheck for object "null"', () => {
  335. utils.test({
  336. onLoaded: (form: HTMLFormElement) => {
  337. setFormValues(form, { enabled: null, })
  338. },
  339. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  340. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  341. const values = getFormValues(form, { submitter })
  342. expect(values['enabled']).toBeUndefined()
  343. },
  344. expectedStaticValue: '',
  345. });
  346. });
  347. it('should uncheck for string "null"', () => {
  348. utils.test({
  349. onLoaded: (form: HTMLFormElement) => {
  350. setFormValues(form, { enabled: 'null', })
  351. },
  352. querySubmitter: (cy: any) => cy.get('[type="submit"]'),
  353. onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
  354. const values = getFormValues(form, { submitter })
  355. expect(values['enabled']).toBeUndefined()
  356. },
  357. expectedStaticValue: '',
  358. });
  359. });
  360. });
  361. });