Skip to content

Commit

Permalink
feature-[168359407]: get all categories
Browse files Browse the repository at this point in the history
- add functionality to get all categories
[Delivers #168359407]
  • Loading branch information
henryade committed Sep 7, 2019
1 parent 00ddd4c commit 0b97bbe
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions server/controllers/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ class Categories {
serverError(res);
}
}

/**
* @name getAll
* @async
* @static
* @memberof Categories
* @param {Object} req express request object
* @param {Object} res express response object
* @returns {JSON} server response
*/
static async getAll(req, res) {
try {
const allCategories = await Category.findAll();
return serverResponse(res, 200, { categories: allCategories });
} catch (error) {
serverError(res);
}
}
}

export default Categories;
2 changes: 2 additions & 0 deletions server/routes/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const {
getSessionFromToken
} = middlewares;

route.get('/', Categories.getAll);

route.post(
'/create',
verifyToken,
Expand Down
33 changes: 33 additions & 0 deletions test/categories/getCategories.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import app from '../../server';
import models from '../../server/database/models';

const { BASE_URL } = process.env;
const { Category } = models;

chai.use(chaiHttp);

describe('GET Categories Test', () => {
it('gets all categories', async () => {
const response = await chai.request(app).get(`${BASE_URL}/categories`);
expect(response).to.have.status(200);
expect(response.body.categories).to.be.an('array');
});

context('when there is an internal error', () => {
it('returns a 500 server error', async () => {
const stub = sinon.stub(Category, 'findAll');
const error = new Error('server error, this will be resolved shortly');
stub.yields(error);
const res = await chai.request(app).get(`${BASE_URL}/categories`);
expect(res).to.have.status(500);
expect(res.body).to.include.key('error');
expect(res.body.error).to.equal(
'server error, this will be resolved shortly'
);
Category.findAll.restore();
});
});
});
3 changes: 2 additions & 1 deletion test/categories/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as createCategory from './createCategory.test';
import * as getCategories from './getCategories.test';

export default createCategory;
export { createCategory, getCategories };

0 comments on commit 0b97bbe

Please sign in to comment.