ensl_gathers/spec/messages.integration.js

155 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-12-27 19:42:32 +00:00
"use strict";
2015-12-28 13:21:35 +00:00
const async = require("async");
const request = require("supertest");
const assert = require("chai").assert;
const helper = require("./helpers/index.js");
const Message = helper.Message;
const app = helper.app;
let user, messages;
2015-12-27 19:42:32 +00:00
describe("Messages", () => {
2015-12-28 13:21:35 +00:00
before(done => {
user = helper.createUser();
helper.clearDb(done);
});
2015-12-27 19:42:32 +00:00
beforeEach(done => {
2015-12-28 13:21:35 +00:00
messages = [];
async.timesSeries(6, (n, next) => {
Message.create({
author: {
username: user.username,
avatar: user.avatar
},
content: "Message " + n
}, (error, message) => {
if (error) return next(error);
messages.push(message);
next();
});
}, done);
2015-12-27 19:42:32 +00:00
});
afterEach(done => {
2015-12-28 13:21:35 +00:00
helper.clearDb(done);
2015-12-27 19:42:32 +00:00
});
2015-12-28 13:21:35 +00:00
2015-12-27 19:42:32 +00:00
describe("#Index", () => {
describe("JSON Api", () => {
2015-12-28 13:21:35 +00:00
it ("returns most recent messages", done => {
request(app)
2015-12-29 02:20:34 +00:00
.get("/api/messages")
2015-12-28 13:21:35 +00:00
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 6);
assert.equal(result.limit, 250);
assert.equal(result.page, 0);
done();
});
});
it ("is sensitive to limit", done => {
request(app)
2015-12-29 02:20:34 +00:00
.get("/api/messages")
2015-12-28 13:21:35 +00:00
.query({
limit: 1
})
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 1);
assert.equal(result.limit, 1);
assert.equal(result.page, 0);
assert.equal(result.messages[0].content, "Message 5");
done();
});
});
2015-12-29 02:23:56 +00:00
it ("returns a maximum of last 250 messages", done => {
async.times(250, (n, next) => {
Message.create({
author: {
username: user.username,
avatar: user.avatar
},
content: "Message " + n
}, next);
}, (error) => {
if (error) return done(error);
request(app)
.get("/api/messages")
.query({
limit: 251
})
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 250);
assert.equal(result.limit, 250);
done();
});
});
});
2015-12-28 13:21:35 +00:00
it ("is sensitive to pagination", done => {
request(app)
2015-12-29 02:20:34 +00:00
.get("/api/messages")
2015-12-28 13:21:35 +00:00
.query({
limit: 1,
page: 2
})
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 1);
assert.equal(result.limit, 1);
assert.equal(result.page, 2);
assert.equal(result.messages[0].content, "Message 3");
done();
});
});
it ("is sensitive to search terms", done => {
request(app)
2015-12-29 02:20:34 +00:00
.get("/api/messages")
2015-12-28 13:21:35 +00:00
.query({
query: "5"
})
.expect("Content-Type", /json/)
.expect(200)
.end((error, response) => {
if (error) return done(error);
let result = response.body;
assert.equal(result.messages.length, 1);
assert.equal(result.limit, 250);
assert.equal(result.page, 0);
assert.equal(result.messages[0].content, "Message 5");
done();
});
});
2015-12-27 19:42:32 +00:00
});
2015-12-28 13:21:35 +00:00
2015-12-27 19:42:32 +00:00
describe("HTML Browser", () => {
it ("renders message browser", done => {
request(app)
.get("/messages")
2015-12-28 13:21:35 +00:00
.expect("Content-Type", /html/)
2015-12-27 19:42:32 +00:00
.expect(200)
.end((error, response) => {
if (error) return done(error);
assert.include(response.text, "Message Browser");
done();
});
})
});
});
});