uppy.test.js 4.5 KB

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