diff --git a/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js b/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js index 550c9b2b37..8ec39c5887 100644 --- a/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js +++ b/ghost/core/test/unit/server/lib/image/cached-image-size-from-url.test.js @@ -1,6 +1,5 @@ const should = require('should'); const sinon = require('sinon'); -const Promise = require('bluebird'); const CachedImageSizeFromUrl = require('../../../../../core/server/lib/image/cached-image-size-from-url'); describe('lib/image: image size cache', function () { @@ -15,16 +14,15 @@ describe('lib/image: image size cache', function () { sinon.restore(); }); - it('should read from cache, if dimensions for image are fetched already', function (done) { + it('should read from cache, if dimensions for image are fetched already', async function () { const url = 'http://mysite.com/content/image/mypostcoverimage.jpg'; - let cachedImagedSizeResult; let imageSizeSpy; - sizeOfStub.returns(new Promise.resolve({ + sizeOfStub.resolves({ width: 50, height: 50, type: 'jpg' - })); + }); const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({ imageSize: { @@ -35,41 +33,37 @@ describe('lib/image: image size cache', function () { imageSizeSpy = sizeOfStub; - cachedImagedSizeResult = Promise.resolve(cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url)); - cachedImagedSizeResult.then(function () { - // first call to get result from `getImageSizeFromUrl` - cachedImagedSize = cachedImageSizeFromUrl.cache; - should.exist(cachedImagedSize); - cachedImagedSize.has(url).should.be.true; - const image = cachedImagedSize.get(url); - should.exist(image.width); - image.width.should.be.equal(50); - should.exist(image.height); - image.height.should.be.equal(50); + await cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url); - // second call to check if values get returned from cache - cachedImagedSizeResult = Promise.resolve(cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url)); - cachedImagedSizeResult.then(function () { - imageSizeSpy.calledOnce.should.be.true(); - imageSizeSpy.calledTwice.should.be.false(); - cachedImagedSize = cachedImageSizeFromUrl.cache; - should.exist(cachedImagedSize); - cachedImagedSize.has(url).should.be.true; - const image2 = cachedImagedSize.get(url); - should.exist(image2.width); - image2.width.should.be.equal(50); - should.exist(image2.height); - image2.height.should.be.equal(50); - done(); - }); - }).catch(done); + // first call to get result from `getImageSizeFromUrl` + cachedImagedSize = cachedImageSizeFromUrl.cache; + should.exist(cachedImagedSize); + cachedImagedSize.has(url).should.be.true; + const image = cachedImagedSize.get(url); + should.exist(image.width); + image.width.should.be.equal(50); + should.exist(image.height); + image.height.should.be.equal(50); + + // second call to check if values get returned from cache + await cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url); + + imageSizeSpy.calledOnce.should.be.true(); + imageSizeSpy.calledTwice.should.be.false(); + cachedImagedSize = cachedImageSizeFromUrl.cache; + should.exist(cachedImagedSize); + cachedImagedSize.has(url).should.be.true; + const image2 = cachedImagedSize.get(url); + should.exist(image2.width); + image2.width.should.be.equal(50); + should.exist(image2.height); + image2.height.should.be.equal(50); }); - it('can handle image-size errors', function (done) { + it('can handle image-size errors', async function () { const url = 'http://mysite.com/content/image/mypostcoverimage.jpg'; - let cachedImageSizeResult; - sizeOfStub.returns(new Promise.reject('error')); + sizeOfStub.rejects('error'); const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({ imageSize: { @@ -78,19 +72,17 @@ describe('lib/image: image size cache', function () { cache: new Map() }); - cachedImageSizeResult = Promise.resolve(cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url)); - cachedImageSizeResult.then(function () { - cachedImagedSize = cachedImageSizeFromUrl.cache; - should.exist(cachedImagedSize); - cachedImagedSize.has(url).should.be.true; - const image = cachedImagedSize.get(url); - should.not.exist(image.width); - should.not.exist(image.height); - done(); - }).catch(done); + await cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url); + + cachedImagedSize = cachedImageSizeFromUrl.cache; + should.exist(cachedImagedSize); + cachedImagedSize.has(url).should.be.true; + const image = cachedImagedSize.get(url); + should.not.exist(image.width); + should.not.exist(image.height); }); - it('should return null if url is undefined', function (done) { + it('should return null if url is undefined', async function () { const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({ imageSize: {}, cache: new Map() @@ -98,9 +90,8 @@ describe('lib/image: image size cache', function () { const url = null; let result; - result = cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url); + result = await cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url); should.not.exist(result); - done(); }); });