@academyjs/rover
Version:
Rover allows you to learn programming interactively.
134 lines • 9.39 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const Article = require("../../app/model/article");
const User = require("../../app/model/user");
const { users, articles, assertThrowsAsync } = require("../helper");
describe("Article model", () => {
let user1 = null;
let user2 = null;
let articleTemplate = null;
beforeEach(() => __awaiter(this, void 0, void 0, function* () {
user1 = new User(users[0]);
yield user1.save();
user2 = new User(users[1]);
yield user2.save();
articleTemplate = Object.assign(Object.assign({}, articles[0]), { author: user1._id, narrator: user2._id });
}));
afterEach(() => __awaiter(this, void 0, void 0, function* () {
user1 = null;
user2 = null;
articleTemplate = null;
}));
it("should be created with correct data", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign({}, articleTemplate));
yield article.save();
assert.isFalse(article.isNew, "The article should be persisted to the database.");
}));
it("should not be created with undefined title", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { title: undefined }));
yield assertThrowsAsync(() => article.save(), "The title attribute is required.");
}));
it("should not be created with null title", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { title: null }));
yield assertThrowsAsync(() => article.save(), "The title attribute is required.");
}));
it("should not be created with empty title", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { title: "" }));
yield assertThrowsAsync(() => article.save(), "The title attribute should be at least 10 characters long.");
}));
it("should not be created with empty title, after trimming", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { title: " \t\t " }));
yield assertThrowsAsync(() => article.save(), "The title attribute should be at least 10 characters long.");
}));
it("should not be created with undefined author", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { author: undefined }));
yield assertThrowsAsync(() => article.save(), "The author attribute is required.");
}));
it("should not be created with null author", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { author: null }));
yield assertThrowsAsync(() => article.save(), "The author attribute is required.");
}));
it("should not be created with undefined narrator", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { narrator: undefined }));
yield assertThrowsAsync(() => article.save(), "The narrator attribute is required.");
}));
it("should not be created with null narrator", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { narrator: null }));
yield assertThrowsAsync(() => article.save(), "The narrator attribute is required.");
}));
it("should create when both author and narrator are the same users", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { author: user1._id, narrator: user1._id }));
yield article.save();
assert.isFalse(article.isNew, "The article should be persisted to the database.");
assert.isTrue(article.author.equals(article.narrator), "The author ObjectId should be equal to narrator ObjectId.");
}));
it("when tags is undefined, should be created with default as [].", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { tags: undefined }));
yield article.save();
assert.isFalse(article.isNew, "The article should be persisted to the database.");
assert.strictEqual(article.tags.length, 0, "The tags array should be empty.");
}));
it("should not be created when tags includes an invalid value", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { tags: ["invalid_tag_value"] }));
yield assertThrowsAsync(() => article.save(), "The tags attribute should not contain an invalid value.");
}));
it("should not be created when slug is null", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { slug: null }));
yield assertThrowsAsync(() => article.save(), "The slug attribute is required.");
}));
it("should not be created when slug is undefined", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { slug: undefined }));
yield assertThrowsAsync(() => article.save(), "The slug attribute is required.");
}));
/* This test case is valid because other than the `_id` attribute, the `slug` attribute is the
* only unique attribute.
*/
it("should not be created when slug is not unique", () => __awaiter(this, void 0, void 0, function* () {
const article1 = new Article(Object.assign({}, articleTemplate));
yield article1.save();
assert.isFalse(article1.isNew, "The first article should be persisted to the database.");
/* The `unique` attribute in Mongoose is not a validator. It is a helper to build indexes.
* Two documents with the same values for a unique attribute may successfully be persisted
* depending on whether MongoDB built the index before writing the documents. Therefore, we
* need to wait for MongoDB to finish building the index before continuing.
*
* See the section "The unique Option is Not a Validator" for more information.
* https://mongoosejs.com/docs/validation.html
*/
yield Article.init();
const article2 = new Article(Object.assign({}, articleTemplate));
yield assertThrowsAsync(() => article2.save(), "The slug attribute should be unique.");
}));
it("when audioURL is missing, should be created with default as null.", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { audioURL: undefined }));
yield article.save();
assert.isFalse(article.isNew, "The article should be persisted to the database.");
assert.isNull(article.audioURL, "The audioURL attribute should be null.");
}));
it("when imageURLs is missing, should be created with default as [].", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { imageURLs: undefined }));
yield article.save();
assert.isFalse(article.isNew, "The article should be persisted to the database.");
assert.strictEqual(article.imageURLs.length, 0, "The imageURLs attribute should be an empty list.");
}));
it('when languageCode is missing, should be created with default as "en"', () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { languageCode: undefined }));
yield article.save();
assert.isFalse(article.isNew, "The article should be persisted to the database.");
assert.strictEqual(article.languageCode, "en", "The languageCode attribute should 'en'.");
}));
it("when published is missing, should be created with default as false", () => __awaiter(this, void 0, void 0, function* () {
const article = new Article(Object.assign(Object.assign({}, articleTemplate), { published: undefined }));
yield article.save();
assert.isFalse(article.isNew, "The article should be persisted to the database.");
assert.isFalse(article.published, "The published attribute should false.");
}));
});
//# sourceMappingURL=article.test.js.map