test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* global browser, 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', () => {
  16. beforeEach(() => {
  17. browser.url(testURL)
  18. })
  19. it('should generate thumbnails for images', function () {
  20. // FIXME why isn't the selectFakeFile alternative below working?
  21. if (!supportsChooseFile()) {
  22. return this.skip()
  23. }
  24. $('#uppyThumbnails .uppy-FileInput-input').waitForExist()
  25. browser.execute(/* must be valid ES5 for IE */ function () {
  26. window.thumbnailsReady = new Promise(function (resolve) {
  27. window.uppyThumbnails.on('thumbnail:all-generated', resolve)
  28. })
  29. })
  30. if (supportsChooseFile()) {
  31. for (const img of images) {
  32. browser.chooseFile('#uppyThumbnails .uppy-FileInput-input', img)
  33. }
  34. for (const { file } of notImages) {
  35. browser.chooseFile('#uppyThumbnails .uppy-FileInput-input', file)
  36. }
  37. } else {
  38. for (const img of images) {
  39. browser.execute(
  40. selectFakeFile,
  41. 'uppyThumbnails',
  42. path.basename(img), // name
  43. `image/${path.extname(img).slice(1)}`, // type
  44. fs.readFileSync(img, 'base64') // b64
  45. )
  46. }
  47. for (const { type, file } of notImages) {
  48. browser.execute(
  49. selectFakeFile,
  50. 'uppyThumbnails',
  51. path.basename(file), // name
  52. type, // type
  53. fs.readFileSync(file, 'base64') // b64
  54. )
  55. }
  56. }
  57. browser.executeAsync(/* must be valid ES5 for IE */ function (done) {
  58. window.thumbnailsReady.then(done)
  59. })
  60. // const names = $$('p.file-name')
  61. const previews = $$('img.file-preview')
  62. // Names should all be listed before previews--indicates that previews were generated asynchronously.
  63. /* Nevermind this, chooseFile() doesn't accept multiple files so they are added one by one and the thumbnails
  64. * have finished generating by the time we add the next.
  65. const nys = names.map((el) => el.getLocation('y'))
  66. const pys = previews.map((el) => el.getLocation('y'))
  67. for (const ny of nys) {
  68. for (const py of pys) {
  69. expect(ny).to.be.below(py, 'names should be listed before previews')
  70. }
  71. }
  72. */
  73. expect(previews).to.have.lengthOf(3) // ex. the invalid image
  74. for (const p of previews) {
  75. expect(p.getAttribute('src')).to.match(/^blob:/)
  76. expect(p.getElementSize('width')).to.equal(200)
  77. }
  78. })
  79. })