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

file.test.ts 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. action: (cy: any) => cy.get('[type="submit"]'),
  26. test: (form: HTMLFormElement, submitter: any, search: any) => {
  27. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  28. .toString();
  29. const after = utils.makeSearchParams(search)
  30. .toString();
  31. expect(before)
  32. .toEqual(after);
  33. },
  34. expectedStaticValue: {
  35. hello: ''
  36. },
  37. });
  38. })
  39. it('should have single form value when a file is selected', () => {
  40. utils.test({
  41. action: (cy: any) => {
  42. cy
  43. .get('[name="hello"]')
  44. .attachFile('uploads/data.json')
  45. return cy.get('[type="submit"]')
  46. },
  47. test: (form: HTMLFormElement, submitter: any, search: any) => {
  48. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  49. .toString();
  50. const after = utils.makeSearchParams(search)
  51. .toString();
  52. expect(before)
  53. .toEqual(after);
  54. },
  55. expectedStaticValue: {
  56. hello: 'data.json',
  57. },
  58. });
  59. })
  60. it('should retrieve the file list upon setting appropriate option', () => {
  61. utils.test({
  62. action: (cy: any) => {
  63. cy
  64. .get('[name="hello"]')
  65. .attachFile('uploads/data.json')
  66. return cy.get('[type="submit"]')
  67. },
  68. test: (form: HTMLFormElement, submitter: any) => {
  69. const formValues = getFormValues(form,
  70. {
  71. submitter,
  72. getFileObjects: true
  73. }
  74. )
  75. expect(formValues.hello[0].name)
  76. .toBe('data.json')
  77. },
  78. });
  79. })
  80. })
  81. describe('multiple', () => {
  82. beforeEach(utils.setup(`
  83. <!DOCTYPE html>
  84. <html lang="en-PH">
  85. <head>
  86. <meta charset="UTF-8">
  87. <title>File/Multiple</title>
  88. </head>
  89. <body>
  90. <form>
  91. <label>
  92. <span>Hello</span>
  93. <input type="file" name="hello" multiple />
  94. </label>
  95. <button type="submit">Submit</button>
  96. </form>
  97. </body>
  98. </html>
  99. `))
  100. it('should have no form values when no file is selected', () => {
  101. utils.test({
  102. action: (cy: any) => cy.get('[type="submit"]'),
  103. test: (form: HTMLFormElement, submitter: any, search: any) => {
  104. const before = utils.makeSearchParams(getFormValues(form, { submitter }))
  105. .toString();
  106. const after = utils.makeSearchParams(search)
  107. .toString();
  108. expect(before)
  109. .toEqual(after);
  110. },
  111. expectedStaticValue: {
  112. hello: '',
  113. },
  114. });
  115. })
  116. it('should have single form value when a file is selected', () => {
  117. utils.test({
  118. action: (cy: any) => {
  119. cy
  120. .get('[name="hello"]')
  121. .attachFile(['uploads/data.json', 'uploads/data2.json'])
  122. return cy.get('[type="submit"]')
  123. },
  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: 'hello=data.json&hello=data2.json',
  133. });
  134. })
  135. it('should retrieve the file list upon setting appropriate option', () => {
  136. utils.test({
  137. action: (cy: any) => {
  138. cy
  139. .get('[name="hello"]')
  140. .attachFile(['uploads/data.json', 'uploads/data2.json'])
  141. return cy.get('[type="submit"]')
  142. },
  143. test: (form: HTMLFormElement, submitter: any) => {
  144. const formValues = getFormValues(form,
  145. {
  146. submitter,
  147. getFileObjects: true
  148. }
  149. )
  150. expect(formValues.hello[0].name)
  151. .toBe('data.json')
  152. expect(formValues.hello[1].name)
  153. .toBe('data2.json')
  154. },
  155. });
  156. })
  157. })
  158. })