Refactored cache image size test suite

refs https://github.com/TryGhost/Toolbox/issues/364

- It was using an outdated syntax and relied on Bluebird depencency. Updated the syntax to async/await and dropped the Bluebird dependency.
This commit is contained in:
Naz 2022-08-05 13:14:42 +01:00
parent 7d3b678d4e
commit 38c76847e9

View File

@ -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();
});
});