Skip to content

Commit

Permalink
Added unit tests for Jasmine
Browse files Browse the repository at this point in the history
  • Loading branch information
Alasdair Stalker committed Aug 27, 2013
1 parent c2941fe commit 0527120
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 0 deletions.
93 changes: 93 additions & 0 deletions test/spec/db.spec.js
@@ -0,0 +1,93 @@
var db = require('../../lib/db');

db.connect('mongodb://localhost/test');

describe('Articles', function() {

var currentArticle = null;

//add some test data
beforeEach(function(done) {
db.add('article1', '', 'article1', 'description', 'keywords', 'alias', function(doc) {
done();
});
});
beforeEach(function(done) {
db.add('article2', '', 'article2', 'description', 'keywords', 'alias', function(doc) {
done();
});
});
beforeEach(function(done) {
db.add('article3', '', 'article3', 'description', 'keywords', 'alias', function(doc) {
done();
});
});
beforeEach(function(done) {
db.addpage('page1', '', 'page1', 'content', 'description', 'keywords', 'alias', 0, function(doc) {
done();
});
});
beforeEach(function(done) {
db.addpage('page2', '', 'page2', 'content', 'description', 'keywords', 'alias', 1, function(doc) {
done();
});
});

afterEach(function(done) {
db.remove(function() {
done();
});
});

describe('#countArticles()', function() {
it('should return a count of 3', function(done) {
db.countArticles(function(err, count) {
expect(count).toEqual(3);
done();
});
});
});

describe('#allArticles()', function() {
it('should return articles 3 and 2', function(done) {
db.allArticles(2, function(err, articles) {
expect(articles.length).toEqual(2);
expect(articles[0].title).toEqual('article3');
done();
});
});
});

describe('#allArticlesPaged()', function() {
it('should return 1 article from the second page', function(done) {
db.allArticlesPaged(2, 2, function(err, articles) {
expect(articles.length).toEqual(1);
expect(articles[0].title).toEqual('article1');
done();
});
});
});

describe('#allArticlesPages()', function() {
it('should return 1 article and 2 pages', function(done) {
db.allArticlesPages(1, function(err, articles, pages) {
expect(articles.length).toEqual(1);
expect(articles[0].title).toEqual('article3');
expect(pages.length).toEqual(2);
expect(pages[0].page).toEqual('page2');
done();
});
});
});

describe('#allMenuPages()', function() {
it('should return 1 page', function(done) {
db.allMenuPages(function(err, pages) {
expect(pages.length).toEqual(1);
expect(pages[0].page).toEqual('page2');
done();
});
});
});

});
85 changes: 85 additions & 0 deletions test/spec/helper.spec.js
@@ -0,0 +1,85 @@

var db = require('../../lib/db'),
helper = require('../../lib/helper');

db.connect('mongodb://localhost/test');

describe('helper', function() {
describe('#prefs()', function() {
it('should return a valid prefs object', function(done) {
helper.prefs(function(pref) {
expect(pref.settings).toBeDefined();
expect(pref.pages).toBeDefined();
done();
}, 'getlog');
});
});

describe('#gravatar()', function() {
it('should return a length of 68', function(done) {
helper.gravatar('test@example.com', function(g) {
expect(g.length).toBe(68);
done();
});
});
});

describe('#md5()', function() {
it('should return a length of 32', function(done) {
helper.md5('test', function(m) {
expect(m.length).toBe(32);
done();
});
});
});

describe('#log()', function() {
it('should return a login message', function(done) {
helper.log({}, {}, function(doc) {
expect(doc.msg).toBeDefined();
done();
});
});
});

describe('#data()', function() {
var prefs, server;
beforeEach(function() {
prefs = {
settings: {
name: 'name',
description: 'desc',
keywords: 'keywords',
}
};
server = {
checkLoggedIn: function() {
return false;
}
};
});

it('should build a data object using prefs as defaults', function(done) {
var c = {
field: 'field'
};
helper.init(server);
var data = helper.data(null, null, prefs, c);
expect(data.title).toEqual('name');
expect(data.description).toEqual('desc');
expect(data.keywords).toEqual('keywords');
expect(data.loggedin).toBe(false);
expect(data.field).toEqual('field');
done();
});

it('should build a data object overiding title', function(done) {
var c = {
title: 'title'
};
var data = helper.data(null, null, prefs, c);
expect(data.title).toEqual('title');
done();
});
});
});
60 changes: 60 additions & 0 deletions test/spec/nblog.spec.js
@@ -0,0 +1,60 @@
var nblog = require('../../lib/nblog');

describe('nblog', function() {
describe('#init()', function() {
var params = {
dev: {
user: 'user',
pass: 'password',
port: '3001',
db: 'mongodb://localhost/test'
}
};
params.prod = params.dev;

it('should initialise the app without an error', function(done) {
nblog.init(params);
done();
});
});


describe('#data()', function() {
var prefs;
beforeEach(function() {
prefs = {
settings: {
name: 'name',
description: 'desc',
keywords: 'keywords',
}
};
});

it('should build a data object using prefs as defaults', function(done) {
var c = {
field: 'field'
};
// var data = nblog.data(prefs, false, c);
// console.log(data);
// data.should.be.a('object');
// data.should.have.property('title', 'name');
// data.should.have.property('description', 'desc');
// data.should.have.property('keywords', 'keywords');
// data.should.have.property('loggedin', false);
// data.should.have.property('field', 'field');
done();
});

// it('should build a data object overiding title', function(done) {
// var c = {
// title: 'title'
// };
// var data = nblog.data(prefs, false, c);
// data.should.be.a('object');
// data.should.have.property('title', 'title');
// done();
// });
});

});
24 changes: 24 additions & 0 deletions test/spec/sitemap.spec.js
@@ -0,0 +1,24 @@
var db = require('../../lib/db'),
sitemap = require('../../lib/sitemap');

db.connect('mongodb://localhost/test');

describe('sitemap', function() {
describe('url', function() {
it('should return a url', function(done) {
var response = '<url><loc>www.example.com</loc><lastmod>2013-01-01</lastmod><changefreq>1</changefreq><priority>0.6</priority></url>';
var url = sitemap.url('www.example.com', '2013-01-01', '1', '0.6');
expect(url).toEqual(response);
done();
});
});

describe('create', function() {
it('should create a sitemap', function(done) {
sitemap.create('www.example.com', function(sitemap) {
expect(sitemap.length).not.toBe(0);
done();
});
});
});
});

0 comments on commit 0527120

Please sign in to comment.