{ "name": "app2", "version": "0.0.0", "author": "Evgeny Reznichenko <kusakyky@gmaill.com>", "dependencies": { "express": "3", "jade": "*", "should": "*", "mocha": "*", "supertest": "*" } }
var app = require('./lib/app.js'); app.listen(3000);
var express = require('express'), jade = require('jade'), fs = require('fs'), app = express(), viewOptions = { compileDebug: false, self: true }; //data var db = { users: [ { id: 0, name: 'Jo', age: 20, sex: 'm' }, { id: 1, name: 'Bo', age: 19, sex: 'm' }, { id: 2, name: 'Le', age: 18, sex: 'w' }, { id: 10, name: 'NotFound', age: 18, sex: 'w' } ], titles: { '/users': ' ', '/users/profile': ' ' } }; //utils function merge(a, b) { var key; if (a && b) { for (key in b) { a[key] = b[key]; } } return a; } //App settings app.set('views', __dirname + '/../views'); app.set('view engine', 'jade'); app.set('title', ' '); app.locals.compileDebug = viewOptions.compileDebug; app.locals.self = viewOptions.self; app.use(express.static(__dirname + '/../public')); app.use(app.router); app.use(function (req, res, next) { next('not found'); }); //error app.use(function (err, req, res, next) { if (/not found/i.test(err)) { res.locals.title = ' :('; res.render('/errors/notfound'); } else { res.locals.title = ''; res.render('/errors/error'); } }); app.use(express.errorHandler()); //routes // app.all('*', function replaceRender(req, res, next) { var render = res.render, view = req.path.length > 1 ? req.path.substr(1).split('/'): []; res.render = function(v, o) { var data, title = res.locals.title; res.render = render; res.locals.title = app.get('title') + (title ? ' - ' + title: ''); // // if ('string' === typeof v) { if (/^\/.+/.test(v)) { view = v.substr(1).split('/'); } else { view = view.concat(v.split('/')); } data = o; } else { data = v; } // res.locals // (res.locals.title) data = merge(data || {}, res.locals); if (req.xhr) { // json res.json({ data: data, view: view.join('.') }); } else { // , // ( history api) data.state = JSON.stringify({ data: data, view: view.join('.') }); // . . view[view.length - 1] = '_' + view[view.length - 1]; // res.render(view.join('/'), data); } }; next(); }); // app.all('*', function loadPageTitle(req, res, next) { res.locals.title = db.titles[req.path]; next(); }); app.get('/', function(req, res){ res.render('index'); }); app.get('/users', function(req, res){ var data = { users: db.users }; res.render('index', data); }); app.get('/users/profile', function(req, res, next){ var user = db.users[req.query.id], data = { user: user }; if (user) { res.render(data); } else { next('Not found'); } }); // function loadTemplate(viewpath) { var fpath = app.get('views') + viewpath, str = fs.readFileSync(fpath, 'utf8'); viewOptions.filename = fpath; viewOptions.client = true; return jade.compile(str, viewOptions).toString(); } app.get('/templates', function(req, res) { var str = 'var views = { ' + '"index": (function(){ return ' + loadTemplate('/index.jade') + ' }()),' + '"users.index": (function(){ return ' + loadTemplate('/users/index.jade') + ' }()),' + '"users.profile": (function(){ return ' + loadTemplate('/users/profile.jade') + ' }()),' + '"errors.error": (function(){ return ' + loadTemplate('/errors/error.jade') + ' }()),' + '"errors.notfound": (function(){ return ' + loadTemplate('/errors/notfound.jade') + ' }())' + '};' res.set({ 'Content-type': 'text/javascript' }).send(str); }); module.exports = app;
MOCHA = ./node_modules/.bin/mocha test: @NODE_ENV=test $(MOCHA) \ -r should \ -R spec .PHONY: test
var tools = require('../lib/tools/index.js'); describe('tools', function () { // "" merge it('should be have #merge', function () { tools.should.be.have.property('merge'); tools.merge.should.be.a('function'); }); describe('#merge', function () { // merge it('should merged', function () { var a = { foo: '1' }, b = { bar: '2' }; tools.merge(a, b).should.eql({ foo: '1', bar: '2' }); }); // , it('should be extend', function () { var a = { foo: '1' }, b = { bar: '2' }; tools.merge(a, b); // , // a.should.not.equal({ foo: '1', bar: '2' }); a.should.equal(a); }); // it('should not be extended', function () { var a = { foo: '1' }, b = { bar: '2' }; tools.merge(a, b); b.should.not.eql({ foo: '1', bar: '2' }); }); }); });
MOCHA = ./node_modules/.bin/mocha test: @NODE_ENV=test $(MOCHA) \ -r should \ -R spec test-cov: lib-cov @COVERAGE=1 $(MOCHA) \ -r should \ -R html-cov > coverage.html lib-cov: clear @jscoverage --encoding=utf8 --no-highlight lib lib-cov clear: @rm -rf lib-cov coverage.html .PHONY: test test-cov clear
var tools = require('../lib/tools/index.js');
var tools = process.env.COVERAGE ? require('../lib-cov/tools/index.js') : require('../lib/tools/index.js');
var should = require('should'), db = process.env.COVERAGE ? require('../lib-cov/models/db.js') : require('../lib/models/db.js'), models = process.env.COVERAGE ? require('../lib-cov/models/index.js') : require('../lib/models/index.js'), User = models.User, UserList = models.UserList; describe('models', function () { // // "describe('models')" before(function () { db.regen(); }); // User it('should be have User', function () { models.should.be.have.property('User'); models.User.should.be.a('function'); }); // UserList it('should be have UserList', function () { models.should.be.have.property('UserList'); models.UserList.should.be.a('function'); }); // User describe('User', function () { // User find it('should be have #find', function () { User.should.be.have.property('find'); User.find.should.be.a('function'); }); // User findById it('should be have #findById', function () { User.should.be.have.property('findById'); User.findById.should.be.a('function'); }); // User save it('should be have #save', function () { User.prototype.should.be.have.property('save'); User.prototype.save.should.be.a('function'); }); // User toJSON it('should be have #toJSON', function () { User.prototype.should.be.have.property('toJSON'); User.prototype.toJSON.should.be.a('function'); }); describe('#find', function () { //find UserList it('should be instanceof UserList', function (done) { User.find(function (err, list) { if (err) return done(err); list.should.be.an.instanceOf(UserList); done(); }); }); //find UserList, it('should not be exist', function (done) { // db.drop(); User.find(function (err, list) { // db.generate(); if (err) return done(err); list.should.be.an.instanceOf(UserList); done(); }); }); }); describe('#findById', function () { //findById User it('should be instanceof User', function (done) { User.findById(0, function (err, user) { if (err) return done(err); user.should.be.an.instanceOf(User); done(); }); }); //findById , it('should not be exists', function (done) { User.findById(100, function (err, user) { if (err) return done(err); should.not.exist(user); done(); }); }); }); describe('#save', function () { //save , it('should not be saved', function (done) { var user = new User({ name: 'New user', age: 0, sex: 'w' }); user.save(function (err) { err.should.eql('Invalid age'); done(); }); }); // , it('should be saved', function (done) { var newuser = new User({ name: 'New user', age: 2, sex: 'w' }); newuser.save(function (err) { if (err) return done(err); User.findById(newuser.id, function (err, user) { if (err) return done(err); user.should.eql(newuser); done(); }); }); }); }); describe('#toJSON', function () { //toJSON json it('should be return json', function (done) { User.findById(0, function (err, user) { if (err) return done(err); user.toJSON().should.be.eql({ id: 0, name: 'Jo', age: 20, sex: 'm' }); done(); }); }); }); }); describe('UserList', function () { //UserList toJSON it('should be have #toJSON', function () { UserList.prototype.should.be.have.property('toJSON'); UserList.prototype.toJSON.should.be.a('function'); }); }); });
before(function () { db.regen(); });
... //find UserList it('should be instanceof UserList', function (done) { User.find(function (err, list) { if (err) return done(err); list.should.be.an.instanceOf(UserList); done(); }); }); ...
/* * */ // var users = []; exports.users = users; // exports.regen = function () { exports.drop(); exports.generate(); }; exports.drop = function () { // , // users.splice(0, users.length); }; exports.generate = function () { // users.push({ id: 0, name: 'Jo', age: 20, sex: 'm' }); users.push({ id: 1, name: 'Bo', age: 19, sex: 'm' }); users.push({ id: 2, name: 'Le', age: 18, sex: 'w' }); users.push({ id: 10, name: 'NotFound', age: 18, sex: 'w' }); }; // exports.generate();
var util = require('util'), db = require('./db.js'), UserList = require('./userlist.js'), users = db.users; /* * */ var User = module.exports = function User(opt) { this.id = users.length; this.name = opt.name; this.age = opt.age; this.sex = opt.sex; this.isNew = true; } /* * */ function loadFromObj(obj) { var user = new User(obj); user.id = obj.id; user.isNew = false; return user; } /* * */ User.find = function (fn) { var i, l = users.length, list; if (l) { list = new UserList(); for (i = 0, l; l > i; i += 1) { list.push(loadFromObj(users[i])); } } fn(null, list); }; /* * id */ User.findById = function (id, fn) { var obj = users[id], user; if (obj) { user = loadFromObj(obj); } fn(null, user); }; /* * */ User.prototype.save = function (fn) { var err; // if (Number.isFinite(this.age) && this.age > 0 && this.age < 150) { if (this.isNew) { users.push(this.toJSON()); this.isNew = false; } else { users[this.id] = this.toJSON(); } } else { err = 'Invalid age'; } fn(err); }; User.prototype.toJSON = function () { var json = { id: this.id, name: this.name, age: this.age, sex: this.sex }; return json; };
var util = require('util'); /* * UserList - , Array */ var UserList = module.exports = function UserList() { Array.apply(this) } util.inherits(UserList, Array); UserList.prototype.toJSON = function () { var i, l = this.length, arr = new Array(l); for (i = 0; l > i; i += 1) { arr[i] = this[i].toJSON(); } return arr; };
exports.User = require('./user.js'); exports.UserList = require('./userlist.js');
var ... User = require('./models/index.js').User, ... ... app.get('/users', function(req, res, next){ User.find(function (err, users) { if (err) { next(err); } else { res.render('index', { users: users.toJSON() }); } }); }); app.get('/users/profile', function(req, res, next){ var id = req.query.id; User.findById(id, function(err, user) { if (user) { res.render({ user: user.toJSON() }); } else { next('Not found'); } }); }); ...
var request = require('supertest'), app = process.env.COVERAGE ? require('../lib-cov/app.js') : require('../lib/app.js'); describe('Response html or json', function () { // , // html it('should be responded as html', function (done) { request(app) .get('/') .expect('Content-Type', /text\/html/) .expect(200, done); }); // , json it('should be responded as json', function (done) { request(app) .get('/') .set('X-Requested-With', 'XMLHttpRequest') .expect('Content-Type', /application\/json/) .expect(200, done); }); }); describe('GET /', function () { // title === it('should be included title', function (done) { request(app) .get('/') .end(function (err, res) { if (err) return done(err); res.text.should.include('<title> </title>'); done(); }); }); // title === it('should be included title', function (done) { request(app) .get('/') .set('X-Requested-With', 'XMLHttpRequest') .end(function (err, res) { if (err) return done(err); res.body.should.have.property('data'); res.body.data.should.have.property('title', ' '); done(); }); }); });
Source: https://habr.com/ru/post/162555/