drag-n-drop.cy.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { getRowForFile } from './FilesUtils.ts'
  2. describe('files: Drag and Drop', { testIsolation: true }, () => {
  3. beforeEach(() => {
  4. cy.createRandomUser().then((user) => {
  5. cy.login(user)
  6. })
  7. cy.visit('/apps/files')
  8. })
  9. it('can drop a file', () => {
  10. const dataTransfer = new DataTransfer()
  11. dataTransfer.items.add(new File([], 'single-file.txt'))
  12. cy.intercept('PUT', /\/remote.php\/dav\/files\//).as('uploadFile')
  13. // Make sure the drop notice is not visible
  14. cy.get('[data-cy-files-drag-drop-area]').should('not.be.visible')
  15. // Trigger the drop notice
  16. cy.get('main.app-content').trigger('dragover', { dataTransfer })
  17. cy.get('[data-cy-files-drag-drop-area]').should('be.visible')
  18. // Upload drop a file
  19. cy.get('[data-cy-files-drag-drop-area]').selectFile({
  20. fileName: 'single-file.txt',
  21. contents: ['hello '.repeat(1024)],
  22. }, { action: 'drag-drop' })
  23. cy.wait('@uploadFile')
  24. // Make sure the upload is finished
  25. cy.get('[data-cy-files-drag-drop-area]').should('not.be.visible')
  26. cy.get('[data-cy-upload-picker] progress').should('not.be.visible')
  27. cy.get('@uploadFile.all').should('have.length', 1)
  28. getRowForFile('single-file.txt').should('be.visible')
  29. getRowForFile('single-file.txt').find('[data-cy-files-list-row-size]').should('contain', '6 KB')
  30. })
  31. it('can drop multiple files', () => {
  32. const dataTransfer = new DataTransfer()
  33. dataTransfer.items.add(new File([], 'first.txt'))
  34. dataTransfer.items.add(new File([], 'second.txt'))
  35. cy.intercept('PUT', /\/remote.php\/dav\/files\//).as('uploadFile')
  36. // Make sure the drop notice is not visible
  37. cy.get('[data-cy-files-drag-drop-area]').should('not.be.visible')
  38. // Trigger the drop notice
  39. cy.get('main.app-content').trigger('dragover', { dataTransfer })
  40. cy.get('[data-cy-files-drag-drop-area]').should('be.visible')
  41. // Upload drop a file
  42. cy.get('[data-cy-files-drag-drop-area]').selectFile([
  43. {
  44. fileName: 'first.txt',
  45. contents: ['Hello'],
  46. },
  47. {
  48. fileName: 'second.txt',
  49. contents: ['World'],
  50. },
  51. ], { action: 'drag-drop' })
  52. cy.wait('@uploadFile')
  53. // Make sure the upload is finished
  54. cy.get('[data-cy-files-drag-drop-area]').should('not.be.visible')
  55. cy.get('[data-cy-upload-picker] progress').should('not.be.visible')
  56. cy.get('@uploadFile.all').should('have.length', 2)
  57. getRowForFile('first.txt').should('be.visible')
  58. getRowForFile('second.txt').should('be.visible')
  59. })
  60. it('will ignore legacy Folders', () => {
  61. cy.window().then((win) => {
  62. // Remove the Filesystem API to force the legacy File API
  63. // See how cypress mocks the Filesystem API in https://github.com/cypress-io/cypress/blob/74109094a92df3bef073dda15f17194f31850d7d/packages/driver/src/cy/commands/actions/selectFile.ts#L24-L37
  64. Object.defineProperty(win.DataTransferItem.prototype, 'getAsEntry', { get: undefined })
  65. Object.defineProperty(win.DataTransferItem.prototype, 'webkitGetAsEntry', { get: undefined })
  66. })
  67. const dataTransfer = new DataTransfer()
  68. dataTransfer.items.add(new File([], 'first.txt'))
  69. dataTransfer.items.add(new File([], 'second.txt'))
  70. // Legacy File API (not FileSystem API), will treat Folders as Files
  71. // with empty type and empty content
  72. dataTransfer.items.add(new File([], 'Foo', { type: 'httpd/unix-directory' }))
  73. dataTransfer.items.add(new File([], 'Bar'))
  74. cy.intercept('PUT', /\/remote.php\/dav\/files\//).as('uploadFile')
  75. // Make sure the drop notice is not visible
  76. cy.get('[data-cy-files-drag-drop-area]').should('not.be.visible')
  77. // Trigger the drop notice
  78. cy.get('main.app-content').trigger('dragover', { dataTransfer })
  79. cy.get('[data-cy-files-drag-drop-area]').should('be.visible')
  80. // Upload drop a file
  81. cy.get('[data-cy-files-drag-drop-area]').selectFile([
  82. {
  83. fileName: 'first.txt',
  84. contents: ['Hello'],
  85. },
  86. {
  87. fileName: 'second.txt',
  88. contents: ['World'],
  89. },
  90. {
  91. fileName: 'Foo',
  92. contents: {},
  93. },
  94. {
  95. fileName: 'Bar',
  96. contents: { mimeType: 'httpd/unix-directory' },
  97. },
  98. ], { action: 'drag-drop' })
  99. cy.wait('@uploadFile')
  100. // Make sure the upload is finished
  101. cy.get('[data-cy-files-drag-drop-area]').should('not.be.visible')
  102. cy.get('[data-cy-upload-picker] progress').should('not.be.visible')
  103. cy.get('@uploadFile.all').should('have.length', 2)
  104. getRowForFile('first.txt').should('be.visible')
  105. getRowForFile('second.txt').should('be.visible')
  106. getRowForFile('Foo').should('not.exist')
  107. getRowForFile('Bar').should('not.exist')
  108. })
  109. })