test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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', () => {
  16. beforeEach(async () => {
  17. await browser.url(testURL)
  18. })
  19. it('should generate thumbnails for images', async function () {
  20. // Does not work on IE right now
  21. if (capabilities.browserName === 'internet explorer') {
  22. this.skip()
  23. return
  24. }
  25. const input = await $('#uppyThumbnails .uppy-FileInput-input')
  26. await input.waitForExist()
  27. await browser.execute(/* must be valid ES5 for IE */ function () {
  28. window.thumbnailsReady = new Promise(function (resolve) {
  29. window.uppyThumbnails.on('thumbnail:all-generated', resolve)
  30. })
  31. })
  32. if (supportsChooseFile()) {
  33. for (const file of images) {
  34. await input.setValue(file)
  35. }
  36. for (const { file } of notImages) {
  37. await input.setValue(file)
  38. }
  39. } else {
  40. for (const img of images) {
  41. await browser.execute(
  42. selectFakeFile,
  43. 'uppyThumbnails',
  44. path.basename(img), // name
  45. `image/${path.extname(img).slice(1)}`, // type
  46. fs.readFileSync(img, 'base64') // b64
  47. )
  48. }
  49. for (const { type, file } of notImages) {
  50. await browser.execute(
  51. selectFakeFile,
  52. 'uppyThumbnails',
  53. path.basename(file), // name
  54. type, // type
  55. fs.readFileSync(file, 'base64') // b64
  56. )
  57. }
  58. }
  59. await browser.executeAsync(/* must be valid ES5 for IE */ function (done) {
  60. window.thumbnailsReady.then(done)
  61. })
  62. // const names = $$('p.file-name')
  63. const previews = await $$('img.file-preview')
  64. // Names should all be listed before previews--indicates that previews were generated asynchronously.
  65. /* Nevermind this, setValue() doesn't accept multiple files so they are added one by one and the thumbnails
  66. * have finished generating by the time we add the next.
  67. const nys = names.map((el) => el.getLocation('y'))
  68. const pys = previews.map((el) => el.getLocation('y'))
  69. for (const ny of nys) {
  70. for (const py of pys) {
  71. expect(ny).to.be.below(py, 'names should be listed before previews')
  72. }
  73. }
  74. */
  75. expect(previews).to.have.lengthOf(3) // ex. the invalid image
  76. for (const p of previews) {
  77. expect(await p.getAttribute('src')).to.match(/^blob:/)
  78. // Doesn't appear to work in Chrome 67 on Android 6.0
  79. if (capabilities.platformName !== 'Android') {
  80. expect(await getWidth(p)).to.equal(200)
  81. }
  82. }
  83. })
  84. })
  85. async function getWidth (ref) {
  86. try {
  87. return await ref.getSize('width')
  88. } catch (err) {
  89. return browser.execute(function (el) {
  90. return el.getBoundingClientRect().width
  91. }, ref)
  92. }
  93. }