uppy.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* global browser, expect, capabilities */
  2. var path = require('path')
  3. var http = require('http')
  4. var tempWrite = require('temp-write')
  5. var testURL = 'http://localhost:4567'
  6. function uppySelectFakeFile (uppyID) {
  7. var blob = new Blob(
  8. ['data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTIwIDEyMCI+CiAgPGNpcmNsZSBjeD0iNjAiIGN5PSI2MCIgcj0iNTAiLz4KPC9zdmc+Cg=='],
  9. { type: 'image/svg+xml' }
  10. )
  11. window[uppyID].addFile({
  12. source: 'test',
  13. name: 'test-file',
  14. type: blob.type,
  15. data: blob
  16. })
  17. }
  18. function browserSupportsChooseFile (capabilities) {
  19. // Webdriver for Safari and Edge doesn’t support .chooseFile
  20. return capabilities.browserName !== 'safari' &&
  21. capabilities.browserName !== 'MicrosoftEdge' &&
  22. capabilities.platformName !== 'Android'
  23. }
  24. describe('File upload with DragDrop + Tus, DragDrop + XHRUpload, i18n translated string', () => {
  25. beforeEach(() => {
  26. browser.url(testURL)
  27. })
  28. it('should upload a file with Tus and set progressbar to 100%', () => {
  29. if (browserSupportsChooseFile(capabilities)) {
  30. browser.chooseFile('#uppyDragDrop .uppy-DragDrop-input', path.join(__dirname, '../fixtures/image.jpg'))
  31. } else {
  32. browser.execute(uppySelectFakeFile, 'uppyDragDrop')
  33. }
  34. browser.pause(3000)
  35. var html = browser.getHTML('#uppyDragDrop-progress .uppy-ProgressBar-percentage', false)
  36. expect(parseInt(html)).to.be.equal(100)
  37. })
  38. it('should upload a file with XHRUpload and set progressbar to 100%', () => {
  39. if (browserSupportsChooseFile(capabilities)) {
  40. browser.chooseFile('#uppyi18n .uppy-DragDrop-input', path.join(__dirname, '../fixtures/image.jpg'))
  41. } else {
  42. browser.execute(uppySelectFakeFile, 'uppyi18n')
  43. }
  44. browser.pause(3000)
  45. var html = browser.getHTML('#uppyi18n-progress .uppy-ProgressBar-percentage', false)
  46. expect(parseInt(html)).to.be.equal(100)
  47. })
  48. it('should translate text strings into Russian', () => {
  49. var text = browser.getText('#uppyi18n .uppy-DragDrop-label')
  50. expect(text.trim()).to.be.equal('Перенесите файлы сюда или выберите')
  51. })
  52. })
  53. // it('another test', function () {
  54. // return browser
  55. // .url(uppyTestURL)
  56. // .chooseFile('#uppyDragDrop .uppy-DragDrop-input', path.join(__dirname, 'image.jpg'))
  57. // .pause(3000)
  58. // .getHTML('#uppyDragDrop-progress .UppyProgressBar-percentage', false).then(val => {
  59. // console.log(val)
  60. // expect(parseInt(val)).toBe(100)
  61. // })
  62. // })
  63. // })
  64. describe.skip('XHRUpload with `limit`', () => {
  65. let server = null
  66. before(() => {
  67. server = http.createServer((req, res) => {
  68. res.writeHead(200, {
  69. 'content-type': 'application/json',
  70. 'access-control-allow-origin': '*'
  71. })
  72. req.pause()
  73. setTimeout(() => {
  74. req.resume()
  75. }, 3000)
  76. req.on('end', () => {
  77. res.end('{"status":"ok"}')
  78. })
  79. }).listen()
  80. })
  81. after(() => {
  82. server.close()
  83. server = null
  84. })
  85. beforeEach(() => {
  86. browser.url(testURL)
  87. })
  88. it('should start counting progress for all files', () => {
  89. const files = [
  90. makeFile(1000),
  91. makeFile(1000),
  92. makeFile(1000),
  93. makeFile(1000),
  94. makeFile(1000),
  95. makeFile(1000),
  96. makeFile(1000),
  97. makeFile(1000),
  98. makeFile(1000),
  99. makeFile(1000)
  100. ]
  101. const endpoint = `http://localhost:${server.address().port}`
  102. browser.execute((endpoint) => {
  103. window.startXHRLimitTest(endpoint)
  104. }, endpoint)
  105. if (browserSupportsChooseFile(capabilities)) {
  106. files.forEach((file) => {
  107. browser.chooseFile('#uppyXhrLimit .uppy-DragDrop-input', file.path)
  108. })
  109. } else {
  110. browser.execute((files) => {
  111. files.forEach((data, i) => {
  112. window.uppyXhrLimit.addFile({
  113. source: 'test',
  114. name: `testfile${i}`,
  115. type: 'text/plain',
  116. data: new Blob([data], { type: 'text/plain' })
  117. })
  118. })
  119. }, files.map((file) => file.content.toString('hex')))
  120. }
  121. browser.execute(() => {
  122. window.uppyXhrLimit.upload()
  123. })
  124. browser.pause(5000)
  125. const status = browser.execute(() => ({
  126. started: window.uppyXhrLimit.uploadsStarted,
  127. complete: window.uppyXhrLimit.uploadsComplete
  128. })).value
  129. expect(status.started).to.be.equal(files.length)
  130. expect(status.complete).to.be.equal(2)
  131. })
  132. })
  133. function makeFile (size) {
  134. const content = Buffer.allocUnsafe(size)
  135. for (let i = 0; i < size; i++) {
  136. content[i] = Math.floor(Math.random() * 255)
  137. }
  138. return { path: tempWrite.sync(content), content }
  139. }