dashboard-compressor.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(`Not what the computer thinks a human-readable size string look like: ${text}`)
  14. }
  15. describe('dashboard-compressor', () => {
  16. beforeEach(() => {
  17. cy.visit('/dashboard-compressor')
  18. cy.get('.uppy-Dashboard-input').as('file-input')
  19. })
  20. it('should compress images', () => {
  21. const sizeBeforeCompression = []
  22. cy.get('@file-input').attachFile(['images/cat.jpg', 'images/traffic.jpg'])
  23. cy.get('.uppy-Dashboard-Item-statusSize').each((element) => {
  24. const text = element.text()
  25. sizeBeforeCompression.push(uglierBytes(text))
  26. })
  27. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  28. cy.get('.uppy-Informer p[role="alert"]', {
  29. timeout: 12000,
  30. }).should('be.visible')
  31. cy.window().then(({ uppy }) => {
  32. for (const file of uppy.getFiles()) {
  33. expect(file.extension).to.equal('webp')
  34. expect(file.type).to.equal('image/webp')
  35. }
  36. })
  37. cy.get('.uppy-Dashboard-Item-statusSize').should((elements) => {
  38. expect(elements).to.have.length(sizeBeforeCompression.length)
  39. for (let i = 0; i < elements.length; i++) {
  40. expect(sizeBeforeCompression[i]).to.be.greaterThan(
  41. uglierBytes(elements[i].textContent),
  42. )
  43. }
  44. })
  45. })
  46. })