Skip to content

Commit

Permalink
task(core): fix tests, patch joigoose [#56]
Browse files Browse the repository at this point in the history
  • Loading branch information
Drapegnik committed Aug 31, 2019
1 parent 12e1d9e commit c6ce516
Show file tree
Hide file tree
Showing 37 changed files with 4,112 additions and 6,188 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
// prettier-ignore
'mocha/valid-test-description': ['warn', /^(\[.+\] )?should/],

'import/no-cycle': 'warn',
// we use named export in utils
'import/prefer-default-export': 'off',
// allow `console.error` & `console.warning`
Expand Down
9,421 changes: 3,653 additions & 5,768 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"test:with-coverage": "cross-env NODE_ENV=testing nyc npm run test && npm run coverage",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once",
"deploy-dev-from-local": "sh bin/dev/deploy-from-local.sh"
"deploy-dev-from-local": "sh bin/dev/deploy-from-local.sh",
"postinstall": "patch-package"
},
"dependencies": {
"@hapi/joi": "^15.1.1",
Expand Down Expand Up @@ -75,7 +76,7 @@
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"@babel/register": "^7.0.0",
"@commitlint/cli": "^7.2.1",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^7.1.2",
"@semantic-release/changelog": "^3.0.0",
"@semantic-release/exec": "^3.1.3",
Expand All @@ -101,7 +102,8 @@
"mocha": "^5.2.0",
"mocha-lcov-reporter": "^1.3.0",
"nodemon": "^1.17.5",
"nyc": "^13.3.0",
"nyc": "^14.1.1",
"patch-package": "^6.1.2",
"prettier": "^1.14.3",
"semantic-release": "^15.13.19",
"supertest": "^3.0.0",
Expand Down
15 changes: 15 additions & 0 deletions patches/joigoose+4.0.8.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/joigoose/lib/index.js b/node_modules/joigoose/lib/index.js
index 9bc235f..20fc9b8 100644
--- a/node_modules/joigoose/lib/index.js
+++ b/node_modules/joigoose/lib/index.js
@@ -142,6 +142,10 @@ internals.typeDeterminer = joiObject => {
return Boolean;
}

+ if (joiObject._type === "objectId") {
+ return "object";
+ }
+
var types = {};
var type = [];
var i = 0;
12 changes: 6 additions & 6 deletions src/api/article/article.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ const joiArticleSchema = Joi.object({
type: Joi.string()
.valid(['text', 'video'])
.required(),
collectionId: Joi.string()
collectionId: Joi.objectId()
.allow(null)
.meta({ type: 'ObjectId', ref: 'ArticleCollection' }),
.meta({ ref: 'ArticleCollection' }),

// FIXME:
// ValidationError: locales.0: Validator failed for path `locales` with value `{ active: true, keywords: [], _id: 5d59905f08e579144c779faa }`
locales: Joi.array().items(Joi.string().meta({ type: 'ObjectId', ref: 'LocalizedArticle' })),
locales: Joi.array().items(Joi.objectId().meta({ ref: 'LocalizedArticle' })),

metadata: Joi.metadata().required(),
// publishAt contains date and time for the article to be published.
Expand All @@ -56,7 +54,9 @@ const joiArticleSchema = Joi.object({
.valid(['light', 'dark'])
.default('light'),
// Authors and Brands are also just Tags.
tags: Joi.array().items(Joi.string().meta({ type: 'ObjectId', ref: 'Tag' })),
tags: Joi.array().items(Joi.objectId().meta({ ref: 'Tag' })),
// Keywords are for SEO optimization and search engines.
keywords: Joi.array().items(Joi.string()),
});

const ArticleSchema = joiToMongoose(joiArticleSchema, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'db/connect';
import HttpStatus from 'http-status-codes';

import {
Expand All @@ -10,9 +11,8 @@ import {
} from 'utils/testing';

import app from 'server';
import 'db/connect';

import Article from 'api/article/article.model';
import { mapIds } from 'utils/getters';

import ArticleCollection from './model';

Expand All @@ -29,7 +29,6 @@ describe('Collections API', () => {
await dropData();

sessionCookie = await loginTestAdmin();

const defaultMetadata = await defaultObjectMetadata();

// Populating DB with Collections.
Expand All @@ -46,7 +45,7 @@ describe('Collections API', () => {
await Promise.all(articlePromises);

const articles = await Article.find().exec();
const articlesList = articles.map(({ _id }) => _id);
const articlesIds = mapIds(articles);

const promises = [];
for (let i = 1; i <= 5; i += 1) {
Expand All @@ -55,7 +54,7 @@ describe('Collections API', () => {
name: { be: `Калекцыя ${i}`, en: `Collection ${i}` },
description: { be: `апісанне`, en: `a description` },
slug: `collection-${i}`,
articles: articlesList[i - 1],
articles: articlesIds[i - 1],
imageUrl: NEW_IMAGE_URL,
}).save()
);
Expand Down Expand Up @@ -133,8 +132,8 @@ describe('Collections API', () => {
be: {
title: 'title',
subtitle: 'subtitle',
content: 'text',
slug: 'slug-new1-be',
text: { content: 'some text' },
},
},
})
Expand All @@ -160,7 +159,7 @@ describe('Collections API', () => {
be: {
title: 'title',
subtitle: 'subtitle',
content: 'text',
text: { content: 'text' },
slug: 'slug-new2-be',
},
},
Expand Down
6 changes: 2 additions & 4 deletions src/api/article/collection/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ const joiArticleCollectionSchema = Joi.object({
name: Joi.localizedText().required(),
description: Joi.localizedText(),
// The order of articles below is essential and defines the structure of the collection.
articles: Joi.array().items(Joi.string().meta({ type: 'ObjectId', ref: 'Article' })),
slug: Joi.slug()
.meta({ unique: true })
.required(),
articles: Joi.array().items(Joi.objectId().meta({ ref: 'Article' })),
slug: Joi.slug(),
active: Joi.boolean().default(true),
imageUrl: Joi.image(),
createdAt: Joi.date()
Expand Down
30 changes: 30 additions & 0 deletions src/api/article/collection/model.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'db/connect';
import mongoose from 'mongoose';

import { expect, dropData, spy } from 'utils/testing';

import ArticleCollection from './model';

describe('ArticleCollection model', () => {
const data = {
name: { be: 'Калекцыя' },
description: { be: 'Цыкл лекцый' },
articles: [mongoose.Types.ObjectId(), mongoose.Types.ObjectId()],
imageUrl: 'https://collection.jpg',
};

before(dropData);

it('should fail to save collection | no slug', async () => {
const errorHandler = spy(({ message }) => {
expect(message).to.not.empty();
expect(message.slug).to.include('required');
});

await ArticleCollection(data)
.save()
.catch(errorHandler);

expect(errorHandler).to.have.been.called();
});
});
7 changes: 2 additions & 5 deletions src/api/article/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const getOne = ({ params: { slugOrId }, user }, res, next) =>
.catch(next);

const handleArticleLocalizationError = locale => err => {
// TODO: check this, maybe redundant
if (err.code === 11000) {
// This is a duplication error. For some reasons it has a slightly different
// structure which makes us to distinguish it as a special case.
Expand All @@ -61,11 +62,7 @@ const handleArticleLocalizationError = locale => err => {
if (err.name === 'MongoError') {
throw err;
}
const msg = {};
Object.values(err.errors).forEach(({ path, message }) => {
set(msg, ['locales', locale, path], message);
});
throw new ValidationError(msg);
throw new ValidationError({ locales: { [locale]: err.message } });
};

export const create = async ({ body, user }, res, next) => {
Expand Down
Loading

0 comments on commit c6ce516

Please sign in to comment.