0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/test/unit/lib/image/blog-icon_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

169 lines
7 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const _ = require('lodash');
const path = require('path');
const rewire = require('rewire');
const settingsCache = require('../../../../core/server/services/settings/cache');
const storageUtils = require('../../../../core/server/adapters/storage/utils');
const urlUtils = require('../../../utils/urlUtils');
// stuff we are testing
const blogIcon = rewire('../../../../core/server/lib/image/blog-icon');
describe('lib/image: blog icon', function () {
afterEach(function () {
sinon.restore();
rewire('../../../../core/server/lib/image/blog-icon');
});
describe('getIconUrl', function () {
it('custom uploaded ico blog icon', function () {
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
blogIcon.getIconUrl().should.eql('/favicon.ico');
});
it('custom uploaded png blog icon', function () {
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
blogIcon.getIconUrl().should.eql('/favicon.png');
});
it('default ico blog icon', function () {
blogIcon.getIconUrl().should.eql('/favicon.ico');
});
describe('absolute URL', function () {
it('custom uploaded ico blog icon', function () {
blogIcon.__set__('urlUtils', urlUtils.getInstance({url: 'http://my-ghost-blog.com/'}));
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
blogIcon.getIconUrl(true).should.eql('http://my-ghost-blog.com/favicon.ico');
});
it('custom uploaded png blog icon', function () {
blogIcon.__set__('urlUtils', urlUtils.getInstance({url: 'http://my-ghost-blog.com/'}));
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
blogIcon.getIconUrl(true).should.eql('http://my-ghost-blog.com/favicon.png');
});
it('default ico blog icon', function () {
blogIcon.__set__('urlUtils', urlUtils.getInstance({url: 'http://my-ghost-blog.com/'}));
blogIcon.getIconUrl(true).should.eql('http://my-ghost-blog.com/favicon.ico');
});
});
describe('with subdirectory', function () {
it('custom uploaded ico blog icon', function () {
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
blogIcon.__set__('urlUtils', urlUtils.getInstance({url: 'http://my-ghost-blog.com/blog'}));
blogIcon.getIconUrl().should.eql('/blog/favicon.ico');
});
it('custom uploaded png blog icon', function () {
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
blogIcon.__set__('urlUtils', urlUtils.getInstance({url: 'http://my-ghost-blog.com/blog'}));
blogIcon.getIconUrl().should.eql('/blog/favicon.png');
});
it('default ico blog icon', function () {
blogIcon.__set__('urlUtils', urlUtils.getInstance({url: 'http://my-ghost-blog.com/blog'}));
blogIcon.getIconUrl().should.eql('/blog/favicon.ico');
});
});
});
describe('getIconPath', function () {
it('custom uploaded ico blog icon', function () {
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
sinon.stub(storageUtils, 'getLocalFileStoragePath');
blogIcon.getIconPath();
storageUtils.getLocalFileStoragePath.calledOnce.should.be.true();
});
it('custom uploaded png blog icon', function () {
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
sinon.stub(storageUtils, 'getLocalFileStoragePath');
blogIcon.getIconPath();
storageUtils.getLocalFileStoragePath.calledOnce.should.be.true();
});
it('default ico blog icon', function () {
blogIcon.getIconPath().should.eql(path.join(__dirname, '../../../../core/server/public/favicon.ico'));
});
});
describe('isIcoImageType', function () {
it('returns true, if icon is .ico filetype', function () {
blogIcon.isIcoImageType('icon.ico').should.be.true();
});
it('returns false, if icon is not .ico filetype', function () {
blogIcon.isIcoImageType('icon.png').should.be.false();
});
});
describe('getIconType', function () {
it('returns x-icon for ico icons', function () {
blogIcon.getIconType('favicon.ico').should.eql('x-icon');
});
it('returns png for png icon', function () {
blogIcon.getIconType('favicon.png').should.eql('png');
});
});
describe('getIconDimensions', function () {
it('[success] returns .ico dimensions', function (done) {
blogIcon.getIconDimensions(path.join(__dirname, '../../../utils/fixtures/images/favicon.ico'))
.then(function (result) {
should.exist(result);
result.should.eql({
width: 48,
height: 48
});
done();
}).catch(done);
});
it('[success] returns .png dimensions', function (done) {
blogIcon.getIconDimensions(path.join(__dirname, '../../../utils/fixtures/images/favicon.png'))
.then(function (result) {
should.exist(result);
result.should.eql({
width: 100,
height: 100
});
done();
}).catch(done);
});
it('[success] returns .ico dimensions for icon with multiple sizes', function (done) {
blogIcon.getIconDimensions(path.join(__dirname, '../../../utils/fixtures/images/favicon_multi_sizes.ico'))
.then(function (result) {
should.exist(result);
result.should.eql({
width: 64,
height: 64
});
done();
}).catch(done);
});
it('[failure] return error message', function (done) {
const sizeOfStub = sinon.stub();
sizeOfStub.throws({error: 'image-size could not find dimensions'});
blogIcon.__set__('sizeOf', sizeOfStub);
blogIcon.getIconDimensions(path.join(__dirname, '../../../utils/fixtures/images/favicon_multi_sizes.ico'))
.catch(function (error) {
should.exist(error);
error.message.should.eql('Could not fetch icon dimensions.');
done();
});
});
});
});