This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luciano Nooijen
committed
Dec 6, 2018
1 parent
a321eca
commit c60d2c3
Showing
3 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const listCategories = async knex => { | ||
const Categories = await knex.select('*').from('categories'); | ||
return Categories; | ||
}; | ||
|
||
const getCategory = async (knex, id) => { | ||
const category = await knex | ||
.select('*') | ||
.from('categories') | ||
.where({ id }); | ||
return category[0]; | ||
}; | ||
|
||
const addCategory = async (knex, category) => { | ||
const newCategoryData = { | ||
name: category.name, | ||
slug: category.slug, | ||
}; | ||
const returning = ['id', 'name', 'slug']; | ||
const newCategory = await knex('categories') | ||
.insert([newCategoryData]) | ||
.returning(returning); | ||
return newCategory[0]; | ||
}; | ||
|
||
const modifyCategory = async (knex, id, category) => { | ||
const { name, slug } = category; | ||
const newCategoryData = { name, slug }; | ||
const returning = ['id', 'name', 'slug']; | ||
const oldCategoryData = getCategory(knex, id); | ||
const newCategory = Object.assign( | ||
{}, | ||
{ ...oldCategoryData, ...newCategoryData }, | ||
); | ||
const modifiedCategory = await knex('categories') | ||
.returning(returning) | ||
.where('id', '=', id) | ||
.update(newCategory); | ||
return modifiedCategory[0]; | ||
}; | ||
|
||
const deleteCategory = async (knex, id) => | ||
new Promise(resolve => | ||
knex('categories') | ||
.returning(['id']) | ||
.where({ id }) | ||
.delete() | ||
.then(data => resolve(data[0])), | ||
); // eslint-disable-line | ||
|
||
module.exports = { | ||
listCategories, | ||
getCategory, | ||
addCategory, | ||
modifyCategory, | ||
deleteCategory, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useTestDatabase } from '../config/index'; | ||
import blog from '../config/blog'; | ||
|
||
const { authHelper } = require('../../helpers'); | ||
|
||
const { | ||
listCategories, | ||
getCategory, | ||
addCategory, | ||
modifyCategory, | ||
deleteCategory, | ||
} = require('../../controllers/categories'); | ||
|
||
useTestDatabase(); | ||
|
||
const newCategory = { | ||
name: 'The new category name', | ||
slug: 'the-new-slug', | ||
}; | ||
|
||
describe('Test if Categories CRUD operations are working correctly', () => { | ||
test('Listing all Categories should return rows', async () => { | ||
expect.assertions(1); | ||
const categories = await listCategories(blog); | ||
expect(categories.length).toBeGreaterThan(0); | ||
}); | ||
|
||
test('Fetching a single Category should return an Categorie', async () => { | ||
expect.assertions(3); | ||
const category = await getCategory(blog, 1); | ||
expect(category.id).toBe(1); | ||
expect(typeof category.name).toBe('string'); | ||
expect(typeof category.slug).toBe('string'); | ||
}); | ||
|
||
test('Adding a new Category should add a single row', async () => { | ||
expect.assertions(1); | ||
const categoriesBefore = await listCategories(blog); | ||
const categorieLengthBefore = categoriesBefore.length; | ||
await addCategory(blog, newCategory); | ||
const categoriesAfter = await listCategories(blog); | ||
const categorieLengthAfter = categoriesAfter.length; | ||
expect(categorieLengthAfter).toBe(categorieLengthBefore + 1); | ||
}); | ||
|
||
test('Adding a new Category should return the new Category', async () => { | ||
expect.assertions(3); | ||
const addedCategory = await addCategory(blog, newCategory); | ||
expect(typeof addedCategory.id).toBe('number'); | ||
expect(addedCategory.name).toBe(newCategory.name); | ||
expect(addedCategory.slug).toBe(newCategory.slug); | ||
}); | ||
|
||
test('Updating an Category should return the modified data', async () => { | ||
expect.assertions(7); | ||
const originalCategory = await getCategory(blog, 1); | ||
expect(originalCategory.id).toBe(1); | ||
expect(originalCategory.name).not.toBe(newCategory.name); | ||
expect(originalCategory.slug).not.toBe(newCategory.slug); | ||
const modifiedCategory = await modifyCategory(blog, 1, newCategory); | ||
expect(modifiedCategory.id).toBeDefined(); | ||
expect(typeof modifiedCategory.id).toBe('number'); | ||
expect(modifiedCategory.name).toBe(newCategory.name); | ||
expect(modifiedCategory.slug).toBe(newCategory.slug); | ||
}); | ||
|
||
test('Deleting an Category should return undefined', async () => { | ||
expect.assertions(2); | ||
return deleteCategory(blog, 2) | ||
.then(data => expect(data.id).toBe(2)) | ||
.then(async () => | ||
expect(await getCategory(blog, 2)).toBeUndefined()); | ||
}); | ||
}); |