dashboard-compressor.spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function uglierBytes(text) {
  2. const KB = 2 ** 10
  3. const MB = KB * KB
  4. if (text.endsWith(' KB')) {
  5. return Number(text.slice(0, -3)) * KB
  6. }
  7. if (text.endsWith(' MB')) {
  8. return Number(text.slice(0, -3)) * MB
  9. }
  10. if (text.endsWith(' B')) {
  11. return Number(text.slice(0, -2))
  12. }
  13. throw new Error(
  14. `Not what the computer thinks a human-readable size string look like: ${text}`,
  15. )
  16. }
  17. describe('dashboard-compressor', () => {
  18. beforeEach(() => {
  19. cy.visit('/dashboard-compressor')
  20. cy.get('.uppy-Dashboard-input:first').as('file-input')
  21. })
  22. it('should compress images', () => {
  23. const sizeBeforeCompression = []
  24. cy.get('@file-input').selectFile(
  25. [
  26. 'cypress/fixtures/images/cat.jpg',
  27. 'cypress/fixtures/images/traffic.jpg',
  28. ],
  29. { force: true },
  30. )
  31. cy.get('.uppy-Dashboard-Item-statusSize').each((element) => {
  32. const text = element.text()
  33. sizeBeforeCompression.push(uglierBytes(text))
  34. })
  35. cy.window().then(({ uppy }) => {
  36. uppy.on('preprocess-complete', (file) => {
  37. expect(file.extension).to.equal('webp')
  38. expect(file.type).to.equal('image/webp')
  39. cy.get('.uppy-Dashboard-Item-statusSize').should((elements) => {
  40. expect(elements).to.have.length(sizeBeforeCompression.length)
  41. for (let i = 0; i < elements.length; i++) {
  42. expect(sizeBeforeCompression[i]).to.be.greaterThan(
  43. uglierBytes(elements[i].textContent),
  44. )
  45. }
  46. })
  47. })
  48. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  49. })
  50. })
  51. })