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.

file.test.ts 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import getFormValues from '../../src'
  2. import * as utils from '../utils'
  3. describe('file', () => {
  4. describe('single', () => {
  5. beforeEach(utils.setup(`
  6. <!DOCTYPE html>
  7. <html lang="en-PH">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>File/Single</title>
  11. </head>
  12. <body>
  13. <form>
  14. <label>
  15. <span>Hello</span>
  16. <input type="file" name="hello" />
  17. </label>
  18. <button type="submit">Submit</button>
  19. </form>
  20. </body>
  21. </html>
  22. `))
  23. it('should have no form values when no file is selected', () => {
  24. utils.test(
  25. (cy: any) => cy.get('[type="submit"]'),
  26. (form: HTMLFormElement, submitter: any, search: any) => {
  27. const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString();
  28. const after = utils.makeSearchParams(search).toString();
  29. expect(before).toEqual(after);
  30. },
  31. {}
  32. );
  33. })
  34. it('should have single form value when a file is selected', () => {
  35. utils.test(
  36. (cy: any) => {
  37. cy
  38. .get('[name="hello"]')
  39. .attachFile('uploads/data.json')
  40. return cy.get('[type="submit"]')
  41. },
  42. (form: HTMLFormElement, submitter: any, search: any) => {
  43. const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString();
  44. const after = utils.makeSearchParams(search).toString();
  45. expect(before).toEqual(after);
  46. },
  47. {
  48. hello: 'data.json',
  49. }
  50. );
  51. })
  52. it('should retrieve the file list upon setting appropriate option', () => {
  53. utils.test(
  54. (cy: any) => {
  55. cy
  56. .get('[name="hello"]')
  57. .attachFile('uploads/data.json')
  58. return cy.get('[type="submit"]')
  59. },
  60. (form: HTMLFormElement, submitter: any) => {
  61. const formValues = getFormValues(form, {submitter, getFileObjects: true})
  62. expect(formValues.hello[0].name).toBe('data.json')
  63. //expect(before).toEqual(after);
  64. },
  65. );
  66. })
  67. })
  68. describe('multiple', () => {
  69. beforeEach(utils.setup(`
  70. <!DOCTYPE html>
  71. <html lang="en-PH">
  72. <head>
  73. <meta charset="UTF-8">
  74. <title>File/Multiple</title>
  75. </head>
  76. <body>
  77. <form>
  78. <label>
  79. <span>Hello</span>
  80. <input type="file" name="hello" multiple />
  81. </label>
  82. <button type="submit">Submit</button>
  83. </form>
  84. </body>
  85. </html>
  86. `))
  87. it('should have no form values when no file is selected', () => {
  88. utils.test(
  89. (cy: any) => cy.get('[type="submit"]'),
  90. (form: HTMLFormElement, submitter: any, search: any) => {
  91. const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString();
  92. const after = utils.makeSearchParams(search).toString();
  93. expect(before).toEqual(after);
  94. },
  95. {}
  96. );
  97. })
  98. it('should have single form value when a file is selected', () => {
  99. utils.test(
  100. (cy: any) => {
  101. cy
  102. .get('[name="hello"]')
  103. .attachFile(['uploads/data.json', 'uploads/data2.json'])
  104. return cy.get('[type="submit"]')
  105. },
  106. (form: HTMLFormElement, submitter: any, search: any) => {
  107. const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString();
  108. const after = utils.makeSearchParams(search).toString();
  109. expect(before).toEqual(after);
  110. },
  111. 'hello=data.json&hello=data2.json',
  112. );
  113. })
  114. it('should retrieve the file list upon setting appropriate option', () => {
  115. utils.test(
  116. (cy: any) => {
  117. cy
  118. .get('[name="hello"]')
  119. .attachFile(['uploads/data.json', 'uploads/data2.json'])
  120. return cy.get('[type="submit"]')
  121. },
  122. (form: HTMLFormElement, submitter: any) => {
  123. const formValues = getFormValues(form, {submitter, getFileObjects: true})
  124. expect(formValues.hello[0].name).toBe('data.json')
  125. expect(formValues.hello[1].name).toBe('data2.json')
  126. },
  127. );
  128. })
  129. })
  130. })