0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/unit/lib/image/cached-image-size-from-url_spec.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

94 lines
3.5 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const Promise = require('bluebird');
const rewire = require('rewire');
// Stuff we are testing
const getCachedImageSizeFromUrl = rewire('../../../../core/server/lib/image/cached-image-size-from-url');
describe('lib/image: image size cache', function () {
let sizeOfStub;
let cachedImagedSize;
beforeEach(function () {
sizeOfStub = sinon.stub();
});
afterEach(function () {
sinon.restore();
getCachedImageSizeFromUrl.__set__('cache', {});
});
it('should read from cache, if dimensions for image are fetched already', function (done) {
const url = 'http://mysite.com/content/image/mypostcoverimage.jpg';
let cachedImagedSizeResult;
let imageSizeSpy;
sizeOfStub.returns(new Promise.resolve({
width: 50,
height: 50,
type: 'jpg'
}));
getCachedImageSizeFromUrl.__set__('imageSize.getImageSizeFromUrl', sizeOfStub);
imageSizeSpy = getCachedImageSizeFromUrl.__get__('imageSize.getImageSizeFromUrl');
cachedImagedSizeResult = Promise.resolve(getCachedImageSizeFromUrl(url));
cachedImagedSizeResult.then(function () {
// first call to get result from `getImageSizeFromUrl`
cachedImagedSize = getCachedImageSizeFromUrl.__get__('cache');
should.exist(cachedImagedSize);
cachedImagedSize.should.have.property(url);
should.exist(cachedImagedSize[url].width);
cachedImagedSize[url].width.should.be.equal(50);
should.exist(cachedImagedSize[url].height);
cachedImagedSize[url].height.should.be.equal(50);
// second call to check if values get returned from cache
cachedImagedSizeResult = Promise.resolve(getCachedImageSizeFromUrl(url));
cachedImagedSizeResult.then(function () {
cachedImagedSize = getCachedImageSizeFromUrl.__get__('cache');
imageSizeSpy.calledOnce.should.be.true();
imageSizeSpy.calledTwice.should.be.false();
should.exist(cachedImagedSize);
cachedImagedSize.should.have.property(url);
should.exist(cachedImagedSize[url].width);
cachedImagedSize[url].width.should.be.equal(50);
should.exist(cachedImagedSize[url].height);
cachedImagedSize[url].height.should.be.equal(50);
done();
});
}).catch(done);
});
it('can handle image-size errors', function (done) {
const url = 'http://mysite.com/content/image/mypostcoverimage.jpg';
let cachedImagedSizeResult;
sizeOfStub.returns(new Promise.reject('error'));
getCachedImageSizeFromUrl.__set__('imageSize.getImageSizeFromUrl', sizeOfStub);
cachedImagedSizeResult = Promise.resolve(getCachedImageSizeFromUrl(url));
cachedImagedSizeResult.then(function () {
cachedImagedSize = getCachedImageSizeFromUrl.__get__('cache');
should.exist(cachedImagedSize);
cachedImagedSize.should.have.property(url);
should.not.exist(cachedImagedSize[url].width);
should.not.exist(cachedImagedSize[url].height);
done();
}).catch(done);
});
it('should return null if url is undefined', function (done) {
const url = null;
let result;
result = getCachedImageSizeFromUrl(url);
should.not.exist(result);
done();
});
});