Skip to content

Commit

Permalink
🎨 🚨 Split theme tests, clean config & add tests (#8205)
Browse files Browse the repository at this point in the history
refs #7491

- split themes_spec up into several files
- clean up the code for configuration
- ensure its tested
  • Loading branch information
ErisDS authored and kirrg001 committed Mar 22, 2017
1 parent e903be6 commit 317daf5
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 536 deletions.
18 changes: 3 additions & 15 deletions core/server/themes/active.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,13 @@
*/
var _ = require('lodash'),
join = require('path').join,
defaultConfig = require('./defaults.json'),
themeConfig = require('./config'),
config = require('../config'),
// @TODO: remove this require
hbs = require('express-hbs'),
// Current instance of ActiveTheme
currentActiveTheme;

// @TODO: will clean this code up later, honest! (and add tests)
function tempConfigHandler(packageJson) {
var config = _.cloneDeep(defaultConfig),
allowedKeys = ['posts_per_page'];

if (packageJson && packageJson.hasOwnProperty('config')) {
config = _.assign(config, _.pick(packageJson.config, allowedKeys));
}

return config;
}

class ActiveTheme {
/**
* @TODO this API needs to be simpler, but for now should work!
Expand All @@ -61,8 +49,8 @@ class ActiveTheme {
return templates;
}, []);

// Do something with config here
this._config = tempConfigHandler(this._packageInfo);
// Create a theme config object
this._config = themeConfig.create(this._packageInfo);
}

get name() {
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions core/server/themes/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var _ = require('lodash'),
defaultConfig = require('./defaults'),
allowedKeys = ['posts_per_page'];

module.exports.create = function configLoader(packageJson) {
var config = _.cloneDeep(defaultConfig);

if (packageJson && packageJson.hasOwnProperty('config')) {
config = _.assign(config, _.pick(packageJson.config, allowedKeys));
}

return config;
};
104 changes: 104 additions & 0 deletions core/test/unit/themes/active_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var should = require('should'), // jshint ignore:line
sinon = require('sinon'),
hbs = require('express-hbs'),

config = require('../../../server/config'),
// is only exposed via themes.getActive()
activeTheme = require('../../../server/themes/active'),

sandbox = sinon.sandbox.create();

describe('Themes', function () {
afterEach(function () {
sandbox.restore();
});

describe('Active', function () {
describe('Mount', function () {
var hbsStub, configStub,
fakeBlogApp, fakeLoadedTheme, fakeCheckedTheme;

beforeEach(function () {
hbsStub = sandbox.stub(hbs, 'express3');
configStub = sandbox.stub(config, 'set');

fakeBlogApp = {
cache: ['stuff'],
set: sandbox.stub(),
engine: sandbox.stub()
};

fakeLoadedTheme = {
name: 'casper',
path: 'my/fake/theme/path'
};
fakeCheckedTheme = {};
});

it('should mount active theme with partials', function () {
// setup partials
fakeCheckedTheme.partials = ['loop', 'navigation'];

var theme = activeTheme.set(fakeLoadedTheme, fakeCheckedTheme);

// Check the theme is not yet mounted
activeTheme.get().mounted.should.be.false();

// Call mount!
theme.mount(fakeBlogApp);

// Check the asset hash gets reset
configStub.calledOnce.should.be.true();
configStub.calledWith('assetHash', null).should.be.true();

// Check te view cache was cleared
fakeBlogApp.cache.should.eql({});

// Check the views were set correctly
fakeBlogApp.set.calledOnce.should.be.true();
fakeBlogApp.set.calledWith('views', 'my/fake/theme/path').should.be.true();

// Check handlebars was initialised correctly
hbsStub.calledOnce.should.be.true();
hbsStub.firstCall.args[0].should.be.an.Object().and.have.property('partialsDir');
hbsStub.firstCall.args[0].partialsDir.should.be.an.Array().with.lengthOf(2);
hbsStub.firstCall.args[0].partialsDir[1].should.eql('my/fake/theme/path/partials');

// Check the theme is now mounted
activeTheme.get().mounted.should.be.true();
});

it('should mount active theme without partials', function () {
// setup partials
fakeCheckedTheme.partials = [];

var theme = activeTheme.set(fakeLoadedTheme, fakeCheckedTheme);

// Check the theme is not yet mounted
activeTheme.get().mounted.should.be.false();

// Call mount!
theme.mount(fakeBlogApp);

// Check the asset hash gets reset
configStub.calledOnce.should.be.true();
configStub.calledWith('assetHash', null).should.be.true();

// Check te view cache was cleared
fakeBlogApp.cache.should.eql({});

// Check the views were set correctly
fakeBlogApp.set.calledOnce.should.be.true();
fakeBlogApp.set.calledWith('views', 'my/fake/theme/path').should.be.true();

// Check handlebars was initialised correctly
hbsStub.calledOnce.should.be.true();
hbsStub.firstCall.args[0].should.be.an.Object().and.have.property('partialsDir');
hbsStub.firstCall.args[0].partialsDir.should.have.lengthOf(1);

// Check the theme is now mounted
activeTheme.get().mounted.should.be.true();
});
});
});
});
38 changes: 38 additions & 0 deletions core/test/unit/themes/config_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var should = require('should'), // jshint ignore:line
sinon = require('sinon'),

themeConfig = require('../../../server/themes/config'),

sandbox = sinon.sandbox.create();

describe('Themes', function () {
afterEach(function () {
sandbox.restore();
});

describe('Config', function () {
it('handles no package.json', function () {
var config = themeConfig.create();

config.should.eql({posts_per_page: 5});
});

it('handles package.json without config', function () {
var config = themeConfig.create({name: 'casper'});

config.should.eql({posts_per_page: 5});
});

it('handles allows package.json to overrideg default', function () {
var config = themeConfig.create({name: 'casper', config: {posts_per_page: 3}});

config.should.eql({posts_per_page: 3});
});

it('handles ignores non-allowed config', function () {
var config = themeConfig.create({name: 'casper', config: {magic: 'roundabout'}});

config.should.eql({posts_per_page: 5});
});
});
});
83 changes: 83 additions & 0 deletions core/test/unit/themes/list_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var should = require('should'), // jshint ignore:line
sinon = require('sinon'),
_ = require('lodash'),

themes = require('../../../server/themes'),
themeList = themes.list,

sandbox = sinon.sandbox.create();

describe('Themes', function () {
afterEach(function () {
sandbox.restore();
});

describe('List', function () {
beforeEach(function () {
themeList.init({
casper: {foo: 'bar'},
'not-casper': {bar: 'baz'}
});
});

it('get() allows getting a single theme', function () {
themeList.get('casper').should.eql({foo: 'bar'});
});

it('get() with no args should do nothing', function () {
should.not.exist(themeList.get());
});

it('getAll() returns all themes', function () {
themeList.getAll().should.be.an.Object().with.properties('casper', 'not-casper');
Object.keys(themeList.getAll()).should.have.length(2);
});

it('set() updates an existing theme', function () {
var origCasper = _.cloneDeep(themeList.get('casper'));
themeList.set('casper', {magic: 'update'});

themeList.get('casper').should.not.eql(origCasper);
themeList.get('casper').should.eql({magic: 'update'});
});

it('set() can add a new theme', function () {
themeList.set('rasper', {color: 'red'});
themeList.get('rasper').should.eql({color: 'red'});
});

it('del() removes a key from the list', function () {
should.exist(themeList.get('casper'));
should.exist(themeList.get('not-casper'));
themeList.del('casper');
should.not.exist(themeList.get('casper'));
should.exist(themeList.get('not-casper'));
});

it('del() with no argument does nothing', function () {
should.exist(themeList.get('casper'));
should.exist(themeList.get('not-casper'));
themeList.del();
should.exist(themeList.get('casper'));
should.exist(themeList.get('not-casper'));
});

it('init() calls set for each theme', function () {
var setSpy = sandbox.spy(themeList, 'set');

themeList.init({test: {a: 'b'}, casper: {c: 'd'}});
setSpy.calledTwice.should.be.true();
setSpy.firstCall.calledWith('test', {a: 'b'}).should.be.true();
setSpy.secondCall.calledWith('casper', {c: 'd'}).should.be.true();
});

it('init() with empty object resets the list', function () {
themeList.init();
var result = themeList.getAll();
should.exist(result);
result.should.be.an.Object();
result.should.eql({});
Object.keys(result).should.have.length(0);
});
});
});
Loading

0 comments on commit 317daf5

Please sign in to comment.