Skip to content

Commit

Permalink
update tests to ava
Browse files Browse the repository at this point in the history
  • Loading branch information
madhums committed Nov 23, 2015
1 parent bbc65cf commit 14c0db6
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 284 deletions.
54 changes: 54 additions & 0 deletions test/_test-articles-get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

/**
* Module dependencies.
*/

const mongoose = require('mongoose');
const test = require('ava');
const request = require('supertest');
const app = require('../server');
const cleanup = require('./helper').cleanup;
const User = mongoose.model('User');
const Article = mongoose.model('Article');
const agent = request.agent(app);

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

test('GET /articles - should respond with Content-Type text/html', t => {
agent
.get('/articles')
.expect('Content-Type', /html/)
.expect(200)
.expect(/Articles/)
.end(t.end);
});

test('GET /articles/new - When not logged in - should redirect to /login', t => {
agent
.get('/articles/new')
.expect('Content-Type', /plain/)
.expect(302)
.expect('Location', '/login')
.expect(/Moved Temporarily/)
.end(t.end);
});

test.before(t => {
// login the user
agent
.post('/users/session')
.field('email', '[email protected]')
.field('password', 'foobar')
.end(t.end);
});

test('GET /articles/new - When logged in - should respond with Content-Type text/html', t => {
agent
.get('/articles/new')
.expect('Content-Type', /html/)
.expect(200)
.expect(/New Article/)
.end(t.end);
});

3 changes: 2 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const User = mongoose.model('User');
* @api public
*/

exports.clearDb = function (done) {
exports.cleanup = function (done) {
done = done || () => {};
async.parallel([
cb => User.collection.remove(cb),
cb => Article.collection.remove(cb)
Expand Down
86 changes: 86 additions & 0 deletions test/test-articles-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

/**
* Module dependencies.
*/

const mongoose = require('mongoose');
const test = require('ava');
const request = require('supertest');
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: '[email protected]',
name: 'Foo bar',
username: 'foobar',
password: 'foobar'
};

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

test('POST /articles - when not logged in - should redirect to /login', t => {
request(app)
.get('/articles/new')
.expect('Content-Type', /plain/)
.expect(302)
.expect('Location', '/login')
.expect(/Redirecting/)
.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
.post('/articles')
.field('title', '')
.field('body', 'foo')
.expect('Content-Type', /text/)
.expect(302)
.expect(/Article title cannot be blank/)
.end(async (err, res) => {
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
.post('/articles')
.field('title', 'foo')
.field('body', 'bar')
.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.end();
});
});

test.afterEach(t => cleanup(t.end));
177 changes: 0 additions & 177 deletions test/test-articles.js

This file was deleted.

Loading

0 comments on commit 14c0db6

Please sign in to comment.