ensl_gathers/spec/message.js

86 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-08-05 00:41:43 +00:00
"use strict";
var helper = require("./helpers/index.js");
var Message = helper.Message;
var assert = require("chai").assert;
2015-08-07 00:06:40 +00:00
var async = require("async");
2015-08-05 00:41:43 +00:00
describe("Message Model", function () {
var user;
before(function (done) {
2015-08-07 00:06:40 +00:00
user = helper.createUser();
2015-08-05 00:41:43 +00:00
helper.clearDb(done);
});
2015-08-07 00:06:40 +00:00
afterEach(function (done) {
2015-08-05 00:41:43 +00:00
helper.clearDb(done);
});
describe(".create", function () {
it ("creates a new message with an author", function (done) {
var content = "Foo";
Message.create({
author: {
username: user.username,
avatar: user.avatar
},
content: content
}, function (error, result) {
if (error) return done(error);
assert.equal(result.author.username, user.username);
assert.equal(result.author.avatar, user.avatar);
assert.equal(result.content, content);
assert.isDefined(result.createdAt);
done();
});
});
});
2015-08-07 00:06:40 +00:00
describe(".list", function () {
2015-08-07 00:47:23 +00:00
beforeEach(function (done) {
2015-08-09 15:29:15 +00:00
var instructions = [helper.clearDb.bind(null)];
2015-08-07 00:06:40 +00:00
for (var i = 0; i < 31; i++) {
2015-08-07 00:47:23 +00:00
instructions.push(function (content) {
return function (callback) {
return helper.createMessage({content: content}, callback);
}
}(i));
2015-08-07 00:06:40 +00:00
}
async.series(instructions, done);
});
it ("lists last 30 messages with oldest first", function (done) {
2015-08-07 00:14:36 +00:00
Message.list({}, function (error, messages) {
2015-08-07 00:06:40 +00:00
if (error) return done(error);
assert.equal(messages.length, 30);
assert.isTrue(messages.reduce(function (acc, message, index, arr) {
if (index === 0) return true;
if (acc === false) return false;
2015-08-09 15:29:15 +00:00
return arr[index - 1].createdAt <= message.createdAt;
2015-08-07 00:47:23 +00:00
}));
assert.isTrue(messages.some(function (message) {
return message.content === "30";
2015-08-07 00:06:40 +00:00
}));
done();
});
});
2015-08-07 00:47:23 +00:00
it ("does not list deleted messages", function (done) {
helper.createMessage({
content: "FOOBAR"
}, function(error, message) {
if (error) return done(error);
message.deleted = true;
message.save(function (error, message) {
if (error) return done(error);
assert.isTrue(message.deleted);
Message.list({}, function (error, messages) {
assert.isTrue(messages.every(function (elem) {
return elem.id !== message.id;
}));
done();
});
});
});
});
2015-08-07 00:06:40 +00:00
});
2015-08-05 00:41:43 +00:00
});