Skip to content

Commit

Permalink
test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
madhums committed Nov 24, 2015
1 parent df312bb commit abb0c51
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"strict": [2, "global"],
"quotes": [2, "single", "avoid-escape"],
"semi": [2, "always"],
"space-before-function-paren": [2, "always"],
"space-after-keywords": [2, "always"],
"space-infix-ops": 2,
"spaced-comment": [2, "always"],
"arrow-spacing": 2,
"no-console": 0
},
"globals": {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"test": "NODE_ENV=test ./node_modules/.bin/ava --serial test/test-*.js"
},
"dependencies": {
"async": "~1.5.0",
"body-parser": "~1.14.1",
"co-express": "~1.2.1",
"compression": "~1.6.0",
Expand Down
4 changes: 2 additions & 2 deletions test/test-users-create.js → test/_test-users-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const app = require('../server');
const cleanup = require('./helper').cleanup;
const User = mongoose.model('User');

test.beforeEach(t => cleanup(t.end));
test.beforeEach(cleanup);

test('no email - should respond with errors', t => {
request(app)
Expand Down Expand Up @@ -69,4 +69,4 @@ test('valid signup - should redirect to /', t => {
});
});

test.afterEach(t => cleanup(t.end));
test.afterEach(cleanup);
13 changes: 5 additions & 8 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
*/

const mongoose = require('mongoose');
const async = require('async');
const Article = mongoose.model('Article');
const User = mongoose.model('User');

/**
* Clear database
*
* @param {Function} done
* @param {Object} t<Ava>
* @api public
*/

exports.cleanup = function (done) {
done = done || () => {};
async.parallel([
cb => User.collection.remove(cb),
cb => Article.collection.remove(cb)
], done);
exports.cleanup = function* (t) {
yield User.remove();
yield Article.remove();
t.end();
};
62 changes: 36 additions & 26 deletions test/test-articles-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,42 @@ const app = require('../server');
const cleanup = require('./helper').cleanup;
const User = mongoose.model('User');
const Article = mongoose.model('Article');
const agent = request.agent(app);

const _user = {
email: 'foobar@example.com',
email: 'foo@email.com',
name: 'Foo bar',
username: 'foobar',
password: 'foobar'
};

test.beforeEach(t => cleanup(t.end));

test.before(cleanup);
test.beforeEach(cleanup);


// user login
test.beforeEach(async t => {
const ctx = global.Promise.defer();
const agent = request.agent(app);
const user = new User(_user);

user.save().then(() => {
agent
.post('/users/session')
.field('email', _user.email)
.field('password', _user.password)
.expect('Location', '/')
.expect('Content-Type', /text/)
.end((err, res) => {
ctx.resolve(res.headers['set-cookie']);
});
});

t.context.agent = agent;
t.context.cookie = await ctx.promise;
t.end();
});


test('POST /articles - when not logged in - should redirect to /login', t => {
request(app)
Expand All @@ -32,55 +58,39 @@ test('POST /articles - when not logged in - should redirect to /login', t => {
.end(t.end);
});

// user login
test.before(async t => {
const user = new User(_user);
await user.save();

agent
.post('/users/session')
.field('email', _user.email)
.field('password', _user.password)
.expect('Location', '/')
.expect('Content-Type', /html/)
.end(err => {
console.log(err);
t.ifError(err);
t.end(err);
});
});

test('POST /articles - invalid form - should respond with error', t => {
agent
t.context.agent
.post('/articles')
.field('title', '')
.field('body', 'foo')
.set('cookie', t.context.cookie)
.expect('Content-Type', /text/)
.expect(302)
.expect(/Article title cannot be blank/)
.end(async (err, res) => {
.end(async err => {
const count = await Article.count().exec();
t.ifError(err);
t.same(count, 0, 'Count should be 0');
t.end();
});
});


test('POST /articles - valid form - should redirect to the new article page', t => {
agent
t.context.agent
.post('/articles')
.field('title', 'foo')
.field('body', 'bar')
.set('cookie', t.context.cookie)
.expect('Content-Type', /plain/)
.expect('Location', /\/articles\//)
.expect(302)
.expect(/Redirecting/)
.end(async err => {
const count = await Article.count().exec();
t.ifError(err);
t.same(count, 1, 'Count should be 0');
t.same(count, 1, 'Count should be 1');
t.end();
});
});

test.afterEach(t => cleanup(t.end));

0 comments on commit abb0c51

Please sign in to comment.