Extract and set form values through the DOM—no frameworks required! https://github.com/TheoryOfNekomata/formxtra
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

submitter.test.ts 2.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import getFormValues from '../../src'
  2. import * as utils from '../utils'
  3. describe('submitter', () => {
  4. describe('button', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>Submitter/Button</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="text" name="hello" value="Hi" />
  17. </label>
  18. <button name="action" value="Foo" type="submit">Foo</button>
  19. <button name="action" value="Bar" type="submit">Bar</button>
  20. </form>
  21. </body>
  22. </html>
  23. `))
  24. it('should have double form values', () => {
  25. utils.test(
  26. (cy: any) => cy.get('[name="action"][value="Foo"]'),
  27. (form: HTMLFormElement, submitter: any, search: any) => {
  28. const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString();
  29. const after = utils.makeSearchParams(search).toString();
  30. expect(before).toEqual(after);
  31. },
  32. {
  33. hello: 'Hi',
  34. action: 'Foo',
  35. }
  36. );
  37. });
  38. })
  39. describe('input', () => {
  40. beforeEach(utils.setup(`
  41. <!DOCTYPE html>
  42. <html lang="en-PH">
  43. <head>
  44. <meta charset="UTF-8">
  45. <title>Submitter/Input</title>
  46. </head>
  47. <body>
  48. <form>
  49. <label>
  50. <span>Hello</span>
  51. <input type="text" name="hello" value="Hi" />
  52. </label>
  53. <input name="action" value="Foo" type="submit" />
  54. <input name="action" value="Bar" type="submit" />
  55. </form>
  56. </body>
  57. </html>
  58. `))
  59. it('should have double form values', () => {
  60. utils.test(
  61. (cy: any) => cy.get('[name="action"][value="Bar"]'),
  62. (form: HTMLFormElement, submitter: any, search: any) => {
  63. const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString();
  64. const after = utils.makeSearchParams(search).toString();
  65. expect(before).toEqual(after);
  66. },
  67. {
  68. hello: 'Hi',
  69. action: 'Bar',
  70. }
  71. );
  72. });
  73. })
  74. describe('outside', () => {
  75. beforeEach(utils.setup(`
  76. <!DOCTYPE html>
  77. <html lang="en-PH">
  78. <head>
  79. <meta charset="UTF-8">
  80. <title>Submitter/Outside</title>
  81. </head>
  82. <body>
  83. <form id="form"></form>
  84. <label>
  85. <span>Hello</span>
  86. <input type="text" name="hello" value="Hi" form="form" />
  87. </label>
  88. <button type="submit" form="form">Submit</button>
  89. </body>
  90. </html>
  91. `))
  92. it('should have single form value', () => {
  93. utils.test(
  94. (cy: any) => cy.get('[type="submit"]'),
  95. (form: HTMLFormElement, submitter: any, search: any) => {
  96. const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString();
  97. const after = utils.makeSearchParams(search).toString();
  98. expect(before).toEqual(after);
  99. },
  100. {
  101. hello: 'Hi',
  102. }
  103. );
  104. });
  105. })
  106. })