Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

169 lines
4.1 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. 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. });