-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 🚨 Split theme tests, clean config & add tests (#8205)
refs #7491 - split themes_spec up into several files - clean up the code for configuration - ensure its tested
- Loading branch information
Showing
9 changed files
with
615 additions
and
536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.