Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checkbox.test.ts 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. action: (cy: any) => cy.get('[type="submit"]'),
  26. test: (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. action: (cy: any) => cy.get('[type="submit"]'),
  43. test: (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. action: (cy: any) => cy.get('[type="submit"]'),
  78. test: (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. action: (cy: any) => cy.get('[type="submit"]'),
  124. test: (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. preAction: (form: HTMLFormElement) => {
  140. setFormValues(form, {
  141. enabled: ['hello 3', 'hello 4'],
  142. })
  143. },
  144. action: (cy: any) => cy.get('[type="submit"]'),
  145. test: (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. preAction: (form: HTMLFormElement) => {
  181. setFormValues(form, { enabled: true, })
  182. },
  183. action: (cy: any) => cy.get('[type="submit"]'),
  184. test: (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. preAction: (form: HTMLFormElement) => {
  194. setFormValues(form, { enabled: 'true', })
  195. },
  196. action: (cy: any) => cy.get('[type="submit"]'),
  197. test: (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. preAction: (form: HTMLFormElement) => {
  207. setFormValues(form, { enabled: 'yes', })
  208. },
  209. action: (cy: any) => cy.get('[type="submit"]'),
  210. test: (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. preAction: (form: HTMLFormElement) => {
  220. setFormValues(form, { enabled: 'on', })
  221. },
  222. action: (cy: any) => cy.get('[type="submit"]'),
  223. test: (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. preAction: (form: HTMLFormElement) => {
  233. setFormValues(form, { enabled: false, })
  234. },
  235. action: (cy: any) => cy.get('[type="submit"]'),
  236. test: (form: HTMLFormElement, submitter: any, search: any) => {
  237. const values = getFormValues(form, { submitter })
  238. expect(values['enabled']).not.toBe('on');
  239. },
  240. expectedStaticValue: '',
  241. });
  242. });
  243. it('should uncheck for string "false"', () => {
  244. utils.test({
  245. preAction: (form: HTMLFormElement) => {
  246. setFormValues(form, { enabled: 'false', })
  247. },
  248. action: (cy: any) => cy.get('[type="submit"]'),
  249. test: (form: HTMLFormElement, submitter: any, search: any) => {
  250. const values = getFormValues(form, { submitter })
  251. expect(values['enabled']).not.toBe('on');
  252. },
  253. expectedStaticValue: '',
  254. });
  255. });
  256. it('should uncheck for string "no"', () => {
  257. utils.test({
  258. preAction: (form: HTMLFormElement) => {
  259. setFormValues(form, { enabled: 'no', })
  260. },
  261. action: (cy: any) => cy.get('[type="submit"]'),
  262. test: (form: HTMLFormElement, submitter: any, search: any) => {
  263. const values = getFormValues(form, { submitter })
  264. expect(values['enabled']).not.toBe('on');
  265. },
  266. expectedStaticValue: '',
  267. });
  268. });
  269. it('should uncheck for string "off"', () => {
  270. utils.test({
  271. preAction: (form: HTMLFormElement) => {
  272. setFormValues(form, { enabled: 'off', })
  273. },
  274. action: (cy: any) => cy.get('[type="submit"]'),
  275. test: (form: HTMLFormElement, submitter: any, search: any) => {
  276. const values = getFormValues(form, { submitter })
  277. expect(values['enabled']).not.toBe('on');
  278. },
  279. expectedStaticValue: '',
  280. });
  281. });
  282. it('should check for number "1"', () => {
  283. utils.test({
  284. preAction: (form: HTMLFormElement) => {
  285. setFormValues(form, { enabled: 1, })
  286. },
  287. action: (cy: any) => cy.get('[type="submit"]'),
  288. test: (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. preAction: (form: HTMLFormElement) => {
  298. setFormValues(form, { enabled: '1', })
  299. },
  300. action: (cy: any) => cy.get('[type="submit"]'),
  301. test: (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. preAction: (form: HTMLFormElement) => {
  311. setFormValues(form, { enabled: 0, })
  312. },
  313. action: (cy: any) => cy.get('[type="submit"]'),
  314. test: (form: HTMLFormElement, submitter: any, search: any) => {
  315. const values = getFormValues(form, { submitter })
  316. expect(values['enabled']).not.toBe('on');
  317. },
  318. expectedStaticValue: '',
  319. });
  320. });
  321. it('should uncheck for string "0"', () => {
  322. utils.test({
  323. preAction: (form: HTMLFormElement) => {
  324. setFormValues(form, { enabled: '0', })
  325. },
  326. action: (cy: any) => cy.get('[type="submit"]'),
  327. test: (form: HTMLFormElement, submitter: any, search: any) => {
  328. const values = getFormValues(form, { submitter })
  329. expect(values['enabled']).not.toBe('on');
  330. },
  331. expectedStaticValue: '',
  332. });
  333. });
  334. it('should uncheck for object "null"', () => {
  335. utils.test({
  336. preAction: (form: HTMLFormElement) => {
  337. setFormValues(form, { enabled: null, })
  338. },
  339. action: (cy: any) => cy.get('[type="submit"]'),
  340. test: (form: HTMLFormElement, submitter: any, search: any) => {
  341. const values = getFormValues(form, { submitter })
  342. expect(values['enabled']).not.toBe('on');
  343. },
  344. expectedStaticValue: '',
  345. });
  346. });
  347. it('should uncheck for string "null"', () => {
  348. utils.test({
  349. preAction: (form: HTMLFormElement) => {
  350. setFormValues(form, { enabled: 'null', })
  351. },
  352. action: (cy: any) => cy.get('[type="submit"]'),
  353. test: (form: HTMLFormElement, submitter: any, search: any) => {
  354. const values = getFormValues(form, { submitter })
  355. expect(values['enabled']).not.toBe('on');
  356. },
  357. expectedStaticValue: '',
  358. });
  359. });
  360. });
  361. });