ImageOptimiserTests.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../app/js/ImageOptimiser.js'
  5. const { FailedCommandError } = require('../../../app/js/Errors')
  6. const SandboxedModule = require('sandboxed-module')
  7. describe('ImageOptimiser', function() {
  8. let ImageOptimiser, SafeExec, logger
  9. const sourcePath = '/wombat/potato.eps'
  10. beforeEach(function() {
  11. SafeExec = {
  12. promises: sinon.stub().resolves()
  13. }
  14. logger = {
  15. warn: sinon.stub()
  16. }
  17. ImageOptimiser = SandboxedModule.require(modulePath, {
  18. requires: {
  19. './SafeExec': SafeExec,
  20. 'logger-sharelatex': logger
  21. }
  22. })
  23. })
  24. describe('compressPng', function() {
  25. it('should convert the file', function(done) {
  26. ImageOptimiser.compressPng(sourcePath, err => {
  27. expect(err).not.to.exist
  28. expect(SafeExec.promises).to.have.been.calledWith([
  29. 'optipng',
  30. sourcePath
  31. ])
  32. done()
  33. })
  34. })
  35. it('should return the error', function(done) {
  36. SafeExec.promises.rejects('wombat herding failure')
  37. ImageOptimiser.compressPng(sourcePath, err => {
  38. expect(err.toString()).to.equal('wombat herding failure')
  39. done()
  40. })
  41. })
  42. })
  43. describe('when optimiser is sigkilled', function() {
  44. const expectedError = new FailedCommandError('', 'SIGKILL', '', '')
  45. let error
  46. beforeEach(function(done) {
  47. SafeExec.promises.rejects(expectedError)
  48. ImageOptimiser.compressPng(sourcePath, err => {
  49. error = err
  50. done()
  51. })
  52. })
  53. it('should not produce an error', function() {
  54. expect(error).not.to.exist
  55. })
  56. it('should log a warning', function() {
  57. expect(logger.warn).to.have.been.calledOnce
  58. })
  59. })
  60. })