Skip to content
This repository was archived by the owner on Jan 20, 2020. It is now read-only.

Commit 80b1c4d

Browse files
author
Luciano Nooijen
committed
Converted Articles to inject knex instance
1 parent fed74bd commit 80b1c4d

File tree

5 files changed

+85
-67
lines changed

5 files changed

+85
-67
lines changed

controllers/articles.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint max-len: ["error", { "code": 100 }] */
22
/* eslint-disable prettier/prettier */
33

4-
const { knex } = require('../helpers');
4+
// const { knex } = require('../helpers');
55

66
const fieldsBase = [
77
'articles.id',
@@ -41,7 +41,7 @@ const addReadingTimeToArticles = articles => {
4141
return articlesWithReadingTime;
4242
};
4343

44-
const listArticles = async () => {
44+
const listArticles = async knex => {
4545
const fields = [
4646
...fieldsBase,
4747
'article_content.html_content',
@@ -58,7 +58,7 @@ const listArticles = async () => {
5858
return articlesWithReadingTime;
5959
};
6060

61-
const getRelatedArticles = async id => {
61+
const getRelatedArticles = async (knex, id) => {
6262
const fields = [
6363
...fieldsBase,
6464
'article_content.html_content',
@@ -77,8 +77,8 @@ const getRelatedArticles = async id => {
7777
return articlesWithReadingTime;
7878
};
7979

80-
const addRelatedArticlesToArticleObject = async (id, article) => {
81-
const relatedArticles = await getRelatedArticles(id);
80+
const addRelatedArticlesToArticleObject = async (knex, id, article) => {
81+
const relatedArticles = await getRelatedArticles(knex, id);
8282
if (relatedArticles.length === 0) {
8383
return article;
8484
}
@@ -89,7 +89,7 @@ const addRelatedArticlesToArticleObject = async (id, article) => {
8989
return articleWithRelatedArticles;
9090
};
9191

92-
const getArticle = async id => {
92+
const getArticle = async (knex, id)=> {
9393
const fields = [
9494
...fieldsBase,
9595
'article_content.html_content',
@@ -103,19 +103,19 @@ const getArticle = async id => {
103103
.where('articles.id', '=', id);
104104
const articlesWithReadingTime = addReadingTimeToArticles(articles);
105105
const articleBase = articlesWithReadingTime[0];
106-
const article = await addRelatedArticlesToArticleObject(id, articleBase);
106+
const article = await addRelatedArticlesToArticleObject(knex, id, articleBase);
107107
return article;
108108
};
109109

110-
const addToArticlesTable = async articleData => {
110+
const addToArticlesTable = async (knex, articleData) => {
111111
const returning = ['id', 'title', 'subtitle', 'posted_on', 'slug', 'author', 'category'];
112112
const addedArticle = await knex('articles')
113113
.insert([articleData])
114114
.returning(returning);
115115
return addedArticle[0];
116116
};
117117

118-
const addToArticleContentTable = async articleData => {
118+
const addToArticleContentTable = async (knex, articleData) => {
119119
const returning = ['summary', 'image_url', 'html_content'];
120120
const addedArticle = await knex('article_content')
121121
.insert([articleData])
@@ -134,7 +134,7 @@ const generateRelatedArticles = (id, relatedArticles) => {
134134
return relatedArticlesObjects;
135135
};
136136

137-
const addToRelatedArticlesTable = async (id, relatedArticles) => {
137+
const addToRelatedArticlesTable = async (knex, id, relatedArticles) => {
138138
if (!relatedArticles || relatedArticles.length === 0) {
139139
return [];
140140
}
@@ -145,7 +145,7 @@ const addToRelatedArticlesTable = async (id, relatedArticles) => {
145145
return addedRelatedArticles;
146146
};
147147

148-
const addArticle = async article => {
148+
const addArticle = async (knex, article) => {
149149
const articleData = {
150150
title: article.title,
151151
subtitle: article.subtitle,
@@ -155,28 +155,28 @@ const addArticle = async article => {
155155
author: article.author,
156156
category: article.author,
157157
};
158-
const addedArticleData = await addToArticlesTable(articleData);
158+
const addedArticleData = await addToArticlesTable(knex, articleData);
159159
const addedArticleId = addedArticleData.id;
160160
const articleContentData = {
161161
article_id: addedArticleId,
162162
summary: article.summary,
163163
image_url: article.image_url,
164164
html_content: article.html_content,
165165
};
166-
await addToArticleContentTable(articleContentData);
166+
await addToArticleContentTable(knex, articleContentData);
167167
const relatedArticleIds = article.related_articles;
168-
await addToRelatedArticlesTable(addedArticleId, relatedArticleIds);
169-
const createdArticle = await getArticle(addedArticleId);
168+
await addToRelatedArticlesTable(knex, addedArticleId, relatedArticleIds);
169+
const createdArticle = await getArticle(knex, addedArticleId);
170170
return createdArticle;
171171
};
172172

173173
// TODO: fix and test better
174174
// eslint-disable-next-line
175-
const modifyArticle = async (id, article) => {
175+
const modifyArticle = async (knex, id, article) => {
176176
throw new Error('Article modification is not supported');
177177
};
178178

179-
const deleteArticle = async id => {
179+
const deleteArticle = async (knex, id) => {
180180
await knex('related_articles')
181181
.where({ article_id: id })
182182
.orWhere({ related_article_id: id })

tests/config/blog-instance.ts

Whitespace-only changes.

tests/config/blog.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const knexGenerator = require('knex');
2+
const generateKnexfile = require('../../database/generate-knexfile');
3+
4+
const knexConfig = {
5+
client: process.env.DB_CLIENT_TEST,
6+
host: process.env.DB_HOST_TEST,
7+
user: process.env.DB_USER_TEST,
8+
database: process.env.DB_NAME_TEST,
9+
password: process.env.DB_PASS_TEST,
10+
debug: process.env.KNEX_DEBUG === 'true',
11+
};
12+
13+
const knexfile = generateKnexfile(knexConfig);
14+
const blog = knexGenerator(knexfile);
15+
16+
export default blog;

tests/controllers/articles.test.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useTestDatabase } from '../config/index';
2+
import blog from '../config/blog';
23

34
const {
45
listArticles,
@@ -31,13 +32,13 @@ const newArticle = {
3132
describe('Articles Controller', () => {
3233
test('listArticles should return articles array', async () => {
3334
expect.assertions(1);
34-
const articles = await listArticles();
35+
const articles = await listArticles(blog);
3536
expect(articles.length).toBeGreaterThan(0);
3637
});
3738

3839
test('listArticles articles should include reading time', async () => {
3940
expect.assertions(1);
40-
const articles = await listArticles();
41+
const articles = await listArticles(blog);
4142
expect(typeof articles[0].reading_time).toBe('number');
4243
});
4344

@@ -57,12 +58,12 @@ describe('Articles Controller', () => {
5758
expect.assertions(2);
5859
const hidden = { hidden: true };
5960
const newHiddenArticle = { ...newArticle, ...hidden };
60-
const createdArticle = await addArticle(newHiddenArticle);
61-
const articles = await listArticles();
61+
const createdArticle = await addArticle(blog, newHiddenArticle);
62+
const articles = await listArticles(blog);
6263
const articlesWithNewArticleId = await articles.filter(article => {
6364
return article.id === createdArticle.id;
6465
});
65-
expect(await getArticle(createdArticle.id)).toBeDefined();
66+
expect(await getArticle(blog, createdArticle.id)).toBeDefined();
6667
expect(articlesWithNewArticleId.length).toBe(0);
6768
});
6869

@@ -72,18 +73,18 @@ describe('Articles Controller', () => {
7273
const futureDays = date.setDate(date.getDate() + 100);
7374
const future = { posted_on: new Date(futureDays) };
7475
const futureArticle = { ...newArticle, ...future };
75-
const createdArticle = await addArticle(futureArticle);
76-
const articles = await listArticles();
76+
const createdArticle = await addArticle(blog, futureArticle);
77+
const articles = await listArticles(blog);
7778
const articlesWithNewArticleId = await articles.filter(article => {
7879
return article.id === createdArticle.id;
7980
});
80-
expect(await getArticle(createdArticle.id)).toBeDefined();
81+
expect(await getArticle(blog, createdArticle.id)).toBeDefined();
8182
expect(articlesWithNewArticleId.length).toBe(0);
8283
});
8384

8485
test('getArticle should return an article with content', async () => {
8586
expect.assertions(14);
86-
const article = await getArticle(1);
87+
const article = await getArticle(blog, 1);
8788
expect(typeof article.id).toBe('number');
8889
expect(typeof article.title).toBe('string');
8990
expect(typeof article.subtitle).toBe('string');
@@ -132,7 +133,7 @@ describe('Articles Controller', () => {
132133

133134
test('getRelatedArticles should return an article array', async () => {
134135
expect.assertions(14);
135-
const relatedArticles = await getRelatedArticles(1);
136+
const relatedArticles = await getRelatedArticles(blog, 1);
136137
const article = relatedArticles[0];
137138
expect(typeof article.id).toBe('number');
138139
expect(typeof article.title).toBe('string');
@@ -165,20 +166,20 @@ describe('Articles Controller', () => {
165166
test('addToRelatedArticlesTable cant add empty array', async () => {
166167
expect.assertions(1);
167168
const emptyArray = [];
168-
const response = await addToRelatedArticlesTable(1, emptyArray);
169+
const response = await addToRelatedArticlesTable(blog, 1, emptyArray);
169170
expect(response).toEqual(emptyArray);
170171
});
171172

172173
test('addToRelatedArticlesTable works with undefined', async () => {
173174
expect.assertions(1);
174175
const emptyArray = [];
175-
const response = await addToRelatedArticlesTable(1);
176+
const response = await addToRelatedArticlesTable(blog, 1);
176177
expect(response).toEqual(emptyArray);
177178
});
178179

179180
test('addArticle should add an article correctly', async () => {
180181
expect.assertions(15);
181-
const addedArticle = await addArticle(newArticle);
182+
const addedArticle = await addArticle(blog, newArticle);
182183
expect(typeof addedArticle).toBe('object');
183184
expect(typeof addedArticle.id).toBe('number');
184185
expect(typeof addedArticle.title).toBe('string');
@@ -202,7 +203,7 @@ describe('Articles Controller', () => {
202203
// TODO: Fix
203204
xtest('modifyArticle should modify an article correctly', async () => {
204205
expect.assertions(9);
205-
await modifyArticle(1, newArticle);
206+
await modifyArticle(blog, 1, newArticle);
206207
const modifiedArticle = await getArticle(1);
207208
expect(modifiedArticle.title).toBe(newArticle.title);
208209
expect(modifiedArticle.subtitle).toBe(newArticle.subtitle);
@@ -219,11 +220,11 @@ describe('Articles Controller', () => {
219220
// TODO: Fix
220221
xtest('modifyArticle should work with when partly updating', async () => {
221222
expect.assertions(9);
222-
const originalArticle = await getArticle(1);
223+
const originalArticle = await getArticle(blog, 1);
223224
const newArticlePart = {
224225
title: 'updated title',
225226
};
226-
await modifyArticle(1, newArticlePart);
227+
await modifyArticle(blog, 1, newArticlePart);
227228
const modifiedArticle = await getArticle(newArticlePart);
228229
expect(modifiedArticle.title).toBe(newArticlePart.title);
229230
expect(modifiedArticle.subtitle).toBe(originalArticle.subtitle);
@@ -239,8 +240,9 @@ describe('Articles Controller', () => {
239240

240241
test('deleteArticle should delete an article', async () => {
241242
expect.assertions(2);
242-
return deleteArticle(1)
243+
return deleteArticle(blog, 1)
243244
.then(data => expect(data.id).toBe(1))
244-
.then(async () => expect(await getArticle(1)).toBeUndefined());
245+
.then(async () =>
246+
expect(await getArticle(blog, 1)).toBeUndefined());
245247
});
246248
});

tests/index.test.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
const nodeBlog = require('../');
2-
const { authors, auth, users, articles } = require('../');
3-
const { Authors } = require('../controllers');
1+
// const nodeBlog = require('../');
2+
// const { authors, auth, users, articles } = require('../');
3+
// const { Authors } = require('../controllers');
44

5-
const client = process.env.DB_CLIENT_TEST;
6-
const host = process.env.DB_HOST_TEST;
7-
const user = process.env.DB_USER_TEST;
8-
const database = process.env.DB_NAME_TEST;
9-
const password = process.env.DB_PASS_TEST;
10-
const debug = process.env.KNEX_DEBUG;
5+
// const client = process.env.DB_CLIENT_TEST;
6+
// const host = process.env.DB_HOST_TEST;
7+
// const user = process.env.DB_USER_TEST;
8+
// const database = process.env.DB_NAME_TEST;
9+
// const password = process.env.DB_PASS_TEST;
10+
// const debug = process.env.KNEX_DEBUG;
1111

12-
const nodeBlogArguments = { client, host, user, database, password, debug };
13-
const blog = nodeBlog(client, host, user, database, password, debug);
12+
// const nodeBlogArguments = { client, host, user, database, password, debug };
13+
// const blog = nodeBlog(client, host, user, database, password, debug);
1414

15-
describe('NodeBlog NPM module', () => {
16-
test('NodeBlog to create a knex instance', () => {
17-
expect(typeof blog).toBe('function');
18-
});
19-
test('Blog authors should work', async () => {
20-
expect.assertions(3);
21-
expect(typeof await authors.list(blog)).toBe('array');
22-
expect(await authors.list(blog)).toEqual(Authors.list(blog));
23-
expect(typeof await authors.get(blog, 1)).toBe('object');
24-
});
25-
test('Blog users should work', async () => {
26-
expect.assertions(2);
27-
expect(typeof await users.list(blog)).toBe('array');
28-
expect(typeof await users.get(blog, 1)).toBe('object');
29-
});
30-
test('Blog articles should work', async () => {
31-
expect.assertions(2);
32-
expect(typeof await articles.list(blog)).toBe('array');
33-
expect(typeof await articles.get(blog, 1)).toBe('object');
34-
});
35-
});
15+
// describe('NodeBlog NPM module', () => {
16+
// test('NodeBlog to create a knex instance', () => {
17+
// expect(typeof blog).toBe('function');
18+
// });
19+
// test('Blog authors should work', async () => {
20+
// expect.assertions(3);
21+
// expect(typeof await authors.list(blog)).toBe('array');
22+
// expect(await authors.list(blog)).toEqual(Authors.list(blog));
23+
// expect(typeof await authors.get(blog, 1)).toBe('object');
24+
// });
25+
// test('Blog users should work', async () => {
26+
// expect.assertions(2);
27+
// expect(typeof await users.list(blog)).toBe('array');
28+
// expect(typeof await users.get(blog, 1)).toBe('object');
29+
// });
30+
// test('Blog articles should work', async () => {
31+
// expect.assertions(2);
32+
// expect(typeof await articles.list(blog)).toBe('array');
33+
// expect(typeof await articles.get(blog, 1)).toBe('object');
34+
// });
35+
// });

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy