|
| 1 | +import { useTestDatabase } from '../config/index'; |
| 2 | +import blog from '../config/blog'; |
| 3 | + |
| 4 | +const { authHelper } = require('../../helpers'); |
| 5 | + |
| 6 | +const { |
| 7 | + listCategories, |
| 8 | + getCategory, |
| 9 | + addCategory, |
| 10 | + modifyCategory, |
| 11 | + deleteCategory, |
| 12 | +} = require('../../controllers/categories'); |
| 13 | + |
| 14 | +useTestDatabase(); |
| 15 | + |
| 16 | +const newCategory = { |
| 17 | + name: 'The new category name', |
| 18 | + slug: 'the-new-slug', |
| 19 | +}; |
| 20 | + |
| 21 | +describe('Test if Categories CRUD operations are working correctly', () => { |
| 22 | + test('Listing all Categories should return rows', async () => { |
| 23 | + expect.assertions(1); |
| 24 | + const categories = await listCategories(blog); |
| 25 | + expect(categories.length).toBeGreaterThan(0); |
| 26 | + }); |
| 27 | + |
| 28 | + test('Fetching a single Category should return an Categorie', async () => { |
| 29 | + expect.assertions(3); |
| 30 | + const category = await getCategory(blog, 1); |
| 31 | + expect(category.id).toBe(1); |
| 32 | + expect(typeof category.name).toBe('string'); |
| 33 | + expect(typeof category.slug).toBe('string'); |
| 34 | + }); |
| 35 | + |
| 36 | + test('Adding a new Category should add a single row', async () => { |
| 37 | + expect.assertions(1); |
| 38 | + const categoriesBefore = await listCategories(blog); |
| 39 | + const categorieLengthBefore = categoriesBefore.length; |
| 40 | + await addCategory(blog, newCategory); |
| 41 | + const categoriesAfter = await listCategories(blog); |
| 42 | + const categorieLengthAfter = categoriesAfter.length; |
| 43 | + expect(categorieLengthAfter).toBe(categorieLengthBefore + 1); |
| 44 | + }); |
| 45 | + |
| 46 | + test('Adding a new Category should return the new Category', async () => { |
| 47 | + expect.assertions(3); |
| 48 | + const addedCategory = await addCategory(blog, newCategory); |
| 49 | + expect(typeof addedCategory.id).toBe('number'); |
| 50 | + expect(addedCategory.name).toBe(newCategory.name); |
| 51 | + expect(addedCategory.slug).toBe(newCategory.slug); |
| 52 | + }); |
| 53 | + |
| 54 | + test('Updating an Category should return the modified data', async () => { |
| 55 | + expect.assertions(7); |
| 56 | + const originalCategory = await getCategory(blog, 1); |
| 57 | + expect(originalCategory.id).toBe(1); |
| 58 | + expect(originalCategory.name).not.toBe(newCategory.name); |
| 59 | + expect(originalCategory.slug).not.toBe(newCategory.slug); |
| 60 | + const modifiedCategory = await modifyCategory(blog, 1, newCategory); |
| 61 | + expect(modifiedCategory.id).toBeDefined(); |
| 62 | + expect(typeof modifiedCategory.id).toBe('number'); |
| 63 | + expect(modifiedCategory.name).toBe(newCategory.name); |
| 64 | + expect(modifiedCategory.slug).toBe(newCategory.slug); |
| 65 | + }); |
| 66 | + |
| 67 | + test('Deleting an Category should return undefined', async () => { |
| 68 | + expect.assertions(2); |
| 69 | + return deleteCategory(blog, 2) |
| 70 | + .then(data => expect(data.id).toBe(2)) |
| 71 | + .then(async () => |
| 72 | + expect(await getCategory(blog, 2)).toBeUndefined()); |
| 73 | + }); |
| 74 | +}); |
0 commit comments