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.
 
 
 

143 lines
3.7 KiB

  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. })