Skip to content

Commit

Permalink
feat(testing): added testing
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Sep 1, 2021
1 parent 39977c1 commit 32aa44a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ src/icons
.env
index.js
.eslintignore
testing.md
test/db.js
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ After completing the instructions to run locally click here: [Local Server](http

### Logger

- [Winston](https://github.com/winstonjs/winston)
This project uses Winston to log events into our log manager. Visit https://github.com/winstonjs/winston for details.

This project uses Morgan to log events inside of the terminal. Visit https://github.com/expressjs/morgan for details.

## Development

Expand Down
62 changes: 62 additions & 0 deletions test/article.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const Article = require('../models/article');
const { getArticle, createArticle, deleteArticle, updateArticle} = require('../controllers/article');
const {MongoClient} = require('mongodb');
const request = require('supertest');
const mongoose = require('mongoose')
const express = require('express');
const app = express();
require('./db');

const mockArticleData = {
article_id: "1", title: "Title",
subtitle: "Subtitle", markdown: "Markdown", description: "Description",
images: "Image", category: "Category", createdAt: Date.now(),
sanitizedHtml: '<p>Markdown</p>\n', updatedAt: Date.now(), __v: 0}

const mockArticleData2 = {_id: mongoose.Types.ObjectId(),
article_id: "2", title: "Title",
subtitle: "Subtitle", markdown: "Markdown", description: "Description",
images: "Image", category: "Category"}

// Initial setup and tear down
beforeEach(async () => {
await Article.deleteMany()
await Article(mockArticleData2).save()
})

// Setting up testing enviroment with test code
describe('Create article to database',() => {

it("should create article in database", async () => {
const mockArticle = new Article(mockArticleData);
console.log(mockArticle)

await mockArticle.save();
// expect(mockArticle.article_id).toEqual("1")
// expect(mockArticle.title).toEqual("Title")
// expect(mockArticle.subtitle).toEqual("Subtitle")
// expect(mockArticle.markdown).toEqual("Markdown")
// expect(mockArticle.description).toEqual("Description")
// expect(mockArticle.images).toEqual("Image")
// expect(mockArticle.category).toEqual("Category")
const insertedArticle = await Article.findOne({article_id: "1"});
console.log(insertedArticle)
// expect(insertedArticle).toEqual(mockArticle);
});

it("should verify create route", async () => {
const response = await request(app).post('/api/articles').send({
mockArticleData
});

expect(response.body).not.toBeNull();
});

it("should get article from database", async () => {
const article = await Article.findById(mockArticleData2._id);

console.log(article)
expect(mockArticleData2.title).toBe("Title")
})
});

0 comments on commit 32aa44a

Please sign in to comment.