test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* global browser, capabilities, expect, $, $$ */
  2. const path = require('path')
  3. const fs = require('fs')
  4. const { selectFakeFile, supportsChooseFile } = require('../utils')
  5. const testURL = 'http://localhost:4567/thumbnails'
  6. const images = [
  7. path.join(__dirname, '../../resources/image.jpg'),
  8. path.join(__dirname, '../../resources/baboon.png'),
  9. path.join(__dirname, '../../resources/kodim23.png'),
  10. path.join(__dirname, '../../resources/invalid.png')
  11. ]
  12. const notImages = [
  13. { type: 'text/javascript', file: __filename }
  14. ]
  15. describe('ThumbnailGenerator', function () {
  16. // Does not work on IE right now
  17. if (capabilities.browserName === 'internet explorer') {
  18. this.skip()
  19. return
  20. }
  21. beforeEach(() => {
  22. browser.url(testURL)
  23. })
  24. it('should generate thumbnails for images', function () {
  25. $('#uppyThumbnails .uppy-FileInput-input').waitForExist()
  26. browser.execute(/* must be valid ES5 for IE */ function () {
  27. window.thumbnailsReady = new Promise(function (resolve) {
  28. window.uppyThumbnails.on('thumbnail:all-generated', resolve)
  29. })
  30. })
  31. if (supportsChooseFile()) {
  32. for (const img of images) {
  33. browser.chooseFile('#uppyThumbnails .uppy-FileInput-input', img)
  34. }
  35. for (const { file } of notImages) {
  36. browser.chooseFile('#uppyThumbnails .uppy-FileInput-input', file)
  37. }
  38. } else {
  39. for (const img of images) {
  40. browser.execute(
  41. selectFakeFile,
  42. 'uppyThumbnails',
  43. path.basename(img), // name
  44. `image/${path.extname(img).slice(1)}`, // type
  45. fs.readFileSync(img, 'base64') // b64
  46. )
  47. }
  48. for (const { type, file } of notImages) {
  49. browser.execute(
  50. selectFakeFile,
  51. 'uppyThumbnails',
  52. path.basename(file), // name
  53. type, // type
  54. fs.readFileSync(file, 'base64') // b64
  55. )
  56. }
  57. }
  58. browser.executeAsync(/* must be valid ES5 for IE */ function (done) {
  59. window.thumbnailsReady.then(done)
  60. })
  61. // const names = $$('p.file-name')
  62. const previews = $$('img.file-preview')
  63. // Names should all be listed before previews--indicates that previews were generated asynchronously.
  64. /* Nevermind this, chooseFile() doesn't accept multiple files so they are added one by one and the thumbnails
  65. * have finished generating by the time we add the next.
  66. const nys = names.map((el) => el.getLocation('y'))
  67. const pys = previews.map((el) => el.getLocation('y'))
  68. for (const ny of nys) {
  69. for (const py of pys) {
  70. expect(ny).to.be.below(py, 'names should be listed before previews')
  71. }
  72. }
  73. */
  74. expect(previews).to.have.lengthOf(3) // ex. the invalid image
  75. for (const p of previews) {
  76. expect(p.getAttribute('src')).to.match(/^blob:/)
  77. expect(p.getElementSize('width')).to.equal(200)
  78. }
  79. })
  80. })