Skip to content

Commit

Permalink
use expect instead of should
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 8, 2016
1 parent 828d98b commit 252440a
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 113 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"coveralls": "^2.11.9",
"istanbul": "^0.4.3",
"mocha": "^2.4.5",
"sinon": "^1.17.3"
"mock-fs": "^3.11.0",
"mock-require": "^1.3.0",
"sinon": "^1.17.3",
"supertest": "^2.0.0"
}
}
4 changes: 2 additions & 2 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function doRenderById(mid, ctx, req, res) {
return mod.render(req, res, ctx);
}

function loadModule(mpath, config) {
exports.loadModule = function(mpath, config) {
debug(`loading ${mpath}`);

var mod = Object.create(module);
Expand Down Expand Up @@ -72,5 +72,5 @@ exports.loadAll = function(config) {
return fs.statSync(filepath).isDirectory();
})
.map(dir => path.resolve(root, dir))
.map(path => loadModule(path, cfg));
.map(path => exports.loadModule(path, cfg));
};
7 changes: 4 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const Path = require('path');
const assert = require('assert');
const fs = require('fs');
const _ = require('lodash');
const debug = require('debug')('brick:module:parser');
const debug = require('debug')('brick:parser');
const httpStatusMsg = require('./http-status.json');
const BPromise = require('bluebird');

function parseTemplate(path, pkg) {
debug(`parseTemplate:${path}, ${pkg.view}`);
var views = pkg.view instanceof Array ? pkg.view : [pkg.view];
assert(views.length, 'view entry for pkg not found');
for (var i = 0; i < views.length; i++) {
Expand All @@ -19,9 +20,9 @@ function parseTemplate(path, pkg) {
return Path.resolve(path, views[0]);
}

function parsePackageFile(path) {
function parsePackageFile(f) {
try {
return require(path);
return require(f);
} catch (e) {
return {};
}
Expand Down
9 changes: 5 additions & 4 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
const env = require('./utils/env');
const expect = env.expect;
const config = require('../config.js');

describe('config', function() {
it('should have default value', function() {
config.factory().root.should.be.a('string');
expect(config.factory().root).to.be.a('string');
});
it('should respect init config', function() {
config.factory({root: 'xx'}).root.should.equal('xx');
expect(config.factory({root: 'xx'}).root).to.equal('xx');
});
it('set should return this', function() {
var cfg = config.factory();
cfg.set('foo', 'bar').should.equal(cfg);
expect(cfg.set('foo', 'bar')).to.equal(cfg);
});
it('should get correctly after set', function() {
config.factory().set('bar', 'foo').get('bar').should.equal('foo');
expect(config.factory().set('bar', 'foo').get('bar')).to.equal('foo');
});
});
21 changes: 11 additions & 10 deletions test/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const env = require('./utils/env');
const expect = env.expect;
const Render = require('../src/render.js');
const stubs = require('./utils/stubs');
const wmd = require('../src/module');
Expand All @@ -17,8 +18,8 @@ describe('context', function() {
var mod = wmd.get('simple');
var result = _.cloneDeep(stubs.ctx);
result.title = 'am title';
return mod.context(stubs.req, stubs.res, stubs.ctx)
.should.eventually.deep.equal(result);
return expect(mod.context(stubs.req, stubs.res, stubs.ctx))
.to.eventually.deep.equal(result);
});
it('should inherit app.locals', function() {
var req = {
Expand All @@ -32,8 +33,8 @@ describe('context', function() {
title: 'am title',
content: 'am content'
};
return wmd.get('simple').context(req, stubs.res, {})
.should.eventually.deep.equal(result);
return expect(wmd.get('simple').context(req, stubs.res, {}))
.to.eventually.deep.equal(result);
});
it('should inherit res.locals', function() {
var req = {
Expand All @@ -52,8 +53,8 @@ describe('context', function() {
title: 'am title',
content: 'am content from res'
};
return wmd.get('simple').context(req, res, {})
.should.eventually.deep.equal(result);
return expect(wmd.get('simple').context(req, res, {}))
.to.eventually.deep.equal(result);
});

it('parent context should override app.locals', function() {
Expand All @@ -71,8 +72,8 @@ describe('context', function() {
title: 'am title',
content: 'am parent'
};
return wmd.get('simple').context(req, stubs.res, parent)
.should.eventually.deep.equal(result);
return expect(wmd.get('simple').context(req, stubs.res, parent))
.to.eventually.deep.equal(result);
});
it('view controller should override parent context', function() {
var parent = {
Expand All @@ -81,7 +82,7 @@ describe('context', function() {
var result = {
title: 'am title'
};
return wmd.get('simple').context(stubs.req, stubs.res, parent)
.should.eventually.deep.equal(result);
return expect(wmd.get('simple').context(stubs.req, stubs.res, parent))
.to.eventually.deep.equal(result);
});
});
26 changes: 19 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
require('./config');
require('./http');
require('./parser');
require('./loader');
require('./render');
require('./context');
require('./resolver');
var request = require('supertest');
//var brick = require('..');

describe('express', function () {
var server;
beforeEach(function () {
//server = require('./server');
});
afterEach(function () {
//server.close();
});
//it('responds to /', function testSlash(done) {
//request(server)
//.get('/')
//.expect(200, done);
//});
//it('404 everything else', function testPath(done) {
//});
});
24 changes: 12 additions & 12 deletions test/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const env = require('./utils/env');
const expect = env.expect;
const Render = require('../src/render.js');
const should = env.should;
const fs = require('fs');
const Path = require('path');
const sinon = require('sinon');
Expand All @@ -21,8 +21,8 @@ describe('loader', function() {
root: Path.resolve(__dirname, './cases')
};
wmd.loadAll(config.factory(cfg));
wmd.get('incom-plete').template.should.contain('inCom_plete/view.html');
wmd.get('simple').router.url.should.equal('/');
expect(wmd.get('incom-plete').template).to.contain('inCom_plete/view.html');
expect(wmd.get('simple').router.url).to.equal('/');
});
it('config:view should support an array', function() {
var _cfg = {
Expand All @@ -31,27 +31,27 @@ describe('loader', function() {
view: ['view.html', 'index.html']
};
wmd.loadAll(config.factory(_cfg));
wmd.get('index-view-name').template.should.contain('index.html');
expect(wmd.get('index-view-name').template).to.contain('index.html');
});
it('should load all directories as modules', function() {
var mods = wmd.loadAll(cfg);
var n = fs.readdirSync(stubs.root).length;
return mods.length.should.equal(n);
expect(mods.length).to.equal(n);
});
it('should load empty module', function() {
wmd.loadAll(cfg);
var mod = wmd.get('fs');
should.exist(mod.template);
should.not.exist(mod.router.url);
expect(mod.template).to.exist;
expect(mod.router.url).to.not.exist;
});
it('should load a simple module', function() {
wmd.loadAll(cfg);
var mod = wmd.get('simple');
var file = Path.resolve(stubs.brickConfig.root, 'simple/index.css');
mod.should.have.property('id');
mod.should.have.property('template');
mod.should.have.property('router');
mod.router.should.have.property('url');
mod.router.should.have.property('get');
expect(mod).to.have.property('id');
expect(mod).to.have.property('template');
expect(mod).to.have.property('router');
expect(mod.router).to.have.property('url');
expect(mod.router).to.have.property('get');
});
});
50 changes: 50 additions & 0 deletions test/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const env = require('./utils/env');
const expect = env.expect;
const mock = require('mock-fs');
const m = require('../src/module.js');
const config = require('../config.js');
const stubs = require('./utils/stubs.js');
const sinon = require('sinon');

describe('module', function() {
var cfg;
before(function(){
mock({
'/foo/router.js': 'exports.url="/"',
'/bar/view.html': 'bar'
});
cfg = config.factory({
root: '/'
});
});
after(function() {
mock.restore();
});
it('should use dirname as mid by default', function() {
var mod = m.loadModule('/foo', cfg);
expect(mod.id).to.equal('foo');
});
it('should use default context when no router present', function() {
var mod = m.loadModule('/bar', cfg);
return expect(mod.context(stubs.req, stubs.res)).to.be.fullfilled;
});
it('render should call renderer', function() {
var mod = m.loadModule('/bar', cfg);
var spy = sinon.spy(mod, 'renderer');
var ctx = {
foo: 'bar'
};
return mod.render(stubs.req, stubs.res, ctx).then(function() {
var args = spy.args[0];
expect(args[0]).to.equal('/bar/view.html');
expect(args[1]).to.deep.equal(ctx);
expect(args[2]).to.be.a('function');
expect(args[3]).to.equal('bar');
});
});
it('render should be called recursively', function() {
var mod = m.loadModule('/bar', cfg);
return expect(mod.render(stubs.req, stubs.res, stubs.ctx))
.to.eventually.equal('bar');
});
});
45 changes: 34 additions & 11 deletions test/parser.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
const env = require('./utils/env');
const stubs = require('./utils/stubs');
const should = env.should;
const expect = env.expect;
const Path = require('path');
const config = require('../config.js');
const _ = require('lodash');
const parser = require('../src/parser.js');
const mockRequire = require('mock-require');
const mockFs = require('mock-fs');

describe('parser', function() {
var cfg, path;
before(function() {
cfg = config.factory(stubs.brickConfig);
path = Path.resolve(stubs.root, 'sample-module/package.json');
beforeEach(function() {
mockRequire('/package.json', {
"name": "sample-module",
"version": "1.0.0",
"view": "htmls/my-html.hbs",
"router": "svr.js"
});
mockRequire('/router.js', {
url: '/foo',
get: function() {}
});
});
afterEach(function() {
mockRequire.stopAll();
});
it('should parse package.json', function() {
var pkg = parser.parsePackageFile(path);
pkg.name.should.equal("sample-module");
pkg.version.should.equal("1.0.0");
pkg.view.should.equal('htmls/my-html.hbs');
pkg.router.should.equal("svr.js");
var pkg = parser.parsePackageFile('/package.json');
expect(pkg.name).to.equal("sample-module");
expect(pkg.version).to.equal("1.0.0");
expect(pkg.view).to.equal('htmls/my-html.hbs');
expect(pkg.router).to.equal("svr.js");
});

it('should parse normal router', function() {
var router = parser.parseRouter('/router.js');
expect(router.url).to.exist;
expect(router.get).to.exist;
});
});

it('should parse empty router', function() {
var router = parser.parseRouter('/not-exist-router.js');
expect(router.url).to.not.exist;
expect(router).to.exist;
});
});

0 comments on commit 252440a

Please sign in to comment.