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

checkbox.test.ts 2.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { getFormValues } 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. })