screenshotHandlerTest.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, lwipImage;
  8. it('should open an image and return an lwip object', function(done) {
  9. ScreenshotHandler.openImage(imagePath)
  10. .then(function(image) {
  11. lwipImage = image;
  12. lwipImage.should.be.an('object');
  13. lwipImage.width().should.equal(620);
  14. lwipImage.height().should.equal(104);
  15. done();
  16. })
  17. .fail(function(err) {
  18. done(err);
  19. });
  20. });
  21. it('should resize an lwip image', function(done) {
  22. ScreenshotHandler.resizeImage(lwipImage, 310)
  23. .then(function(image) {
  24. lwipImage = image;
  25. lwipImage.width().should.equal(310);
  26. lwipImage.height().should.equal(52);
  27. done();
  28. })
  29. .fail(function(err) {
  30. done(err);
  31. });
  32. });
  33. it('should transform a lwip image into a buffer', function(done) {
  34. ScreenshotHandler.toBuffer(lwipImage)
  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.jpg');
  67. });
  68. it('should delete the temp folder when there is no file', function(done) {
  69. var tmpFolderPath = screenshot;
  70. screenshot.deleteTmpFile()
  71. .then(function() {
  72. fs.existsSync(screenshot.getTmpFolder().path).should.equal(false);
  73. done();
  74. })
  75. .fail(function(err) {
  76. done(err);
  77. });
  78. });
  79. it('should delete the temp folder with the screenshot inside', function(done) {
  80. screenshot = ScreenshotHandler.getScreenshotTempFile();
  81. var tmpFolderPath = screenshot.getTmpFolder().path;
  82. var tmpImagePath = path.join(tmpFolderPath, 'screenshot.jpg');
  83. // Copy image
  84. var testImage = fs.readFileSync(imagePath);
  85. fs.writeFileSync(tmpImagePath, testImage);
  86. fs.existsSync(tmpImagePath).should.equal(true);
  87. screenshot.deleteTmpFile()
  88. .then(function() {
  89. fs.existsSync(tmpImagePath).should.equal(false);
  90. fs.existsSync(tmpFolderPath).should.equal(false);
  91. done();
  92. })
  93. .fail(function(err) {
  94. done(err);
  95. });
  96. });
  97. });