screenshotHandlerTest.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var should = require('chai').should();
  2. var ScreenshotHandler = require('../../lib/screenshotHandler');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('screenshotHandler', function() {
  6. var imagePath = path.join(__dirname, '../fixtures/logo-large.png');
  7. var screenshot, jimpImage;
  8. it('should open an image and return an jimp object', function(done) {
  9. ScreenshotHandler.openImage(imagePath)
  10. .then(function(image) {
  11. jimpImage = image;
  12. jimpImage.should.be.an('object');
  13. jimpImage.bitmap.width.should.equal(620);
  14. jimpImage.bitmap.height.should.equal(104);
  15. done();
  16. })
  17. .fail(function(err) {
  18. done(err);
  19. });
  20. });
  21. it('should resize an jimp image', function(done) {
  22. ScreenshotHandler.resizeImage(jimpImage, 310)
  23. .then(function(image) {
  24. jimpImage = image;
  25. jimpImage.bitmap.width.should.equal(310);
  26. jimpImage.bitmap.height.should.equal(52);
  27. done();
  28. })
  29. .fail(function(err) {
  30. done(err);
  31. });
  32. });
  33. it('should transform a jimp image into a buffer', function(done) {
  34. ScreenshotHandler.toBuffer(jimpImage)
  35. .then(function(buffer) {
  36. buffer.should.be.an.instanceof(Buffer);
  37. done();
  38. })
  39. .fail(function(err) {
  40. done(err);
  41. });
  42. });
  43. it('should optimize an image and return a buffered version', function(done) {
  44. ScreenshotHandler.optimize(imagePath, 200)
  45. .then(function(buffer) {
  46. buffer.should.be.an.instanceof(Buffer);
  47. done();
  48. })
  49. .fail(function(err) {
  50. done(err);
  51. });
  52. });
  53. it('should provide a temporary file object', function() {
  54. screenshot = ScreenshotHandler.getScreenshotTempFile();
  55. screenshot.should.have.a.property('getTmpFolder').that.is.a('function');
  56. screenshot.should.have.a.property('getTmpFilePath').that.is.a('function');
  57. screenshot.should.have.a.property('toThumbnail').that.is.a('function');
  58. screenshot.should.have.a.property('deleteTmpFile').that.is.a('function');
  59. });
  60. it('should have created the temporary folder', function() {
  61. var folder = screenshot.getTmpFolder();
  62. fs.existsSync(folder.path).should.equal(true);
  63. });
  64. it('should respond a temporary file', function() {
  65. var file = screenshot.getTmpFilePath();
  66. file.should.have.string('/screenshot.png');
  67. });
  68. it('should delete the temp folder when there is no file', function(done) {
  69. var tmpFolderPath = screenshot;
  70. screenshot.deleteTmpFile()
  71. .delay(1000)
  72. .then(function() {
  73. fs.existsSync(screenshot.getTmpFolder().path).should.equal(false);
  74. done();
  75. })
  76. .fail(function(err) {
  77. done(err);
  78. });
  79. });
  80. it('should delete the temp folder with the screenshot inside', function(done) {
  81. screenshot = ScreenshotHandler.getScreenshotTempFile();
  82. var tmpFolderPath = screenshot.getTmpFolder().path;
  83. var tmpImagePath = path.join(tmpFolderPath, 'screenshot.png');
  84. // Copy image
  85. var testImage = fs.readFileSync(imagePath);
  86. fs.writeFileSync(tmpImagePath, testImage);
  87. fs.existsSync(tmpImagePath).should.equal(true);
  88. screenshot.deleteTmpFile()
  89. .delay(1000)
  90. .then(function() {
  91. fs.existsSync(tmpImagePath).should.equal(false);
  92. fs.existsSync(tmpFolderPath).should.equal(false);
  93. done();
  94. })
  95. .fail(function(err) {
  96. done(err);
  97. });
  98. });
  99. });