test.js 2.7 KB

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