Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha GRASSO committed Jan 3, 2018
1 parent 04d8d6d commit 43ee2c5
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/global-modules-indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let modulesIndex: object = {};

let startRecursiveCheck = (filePath: string): void => {
fs.readdir(filePath, (err, content) => {
if (err) console.error(err);
if (err) throw new Error(err.message);
else content.forEach(e => {
let name = e.replace('.js', '');
modulesIndex[name] = {};
Expand All @@ -19,7 +19,7 @@ let startRecursiveCheck = (filePath: string): void => {

let nextChecks = (name: string, filePath: string): void => {
fs.readdir(filePath, (err, content) => {
if (err) console.error(err);
if (err) throw new Error(err.message);
else content.forEach(e => {
fs.stat(filePath + '/' + e, (err, result) => {
result.isFile() ? modulesIndex[name] = require(filePath + '/' + e) : nextChecks(name, filePath + '/' + e);
Expand Down
169 changes: 163 additions & 6 deletions test/mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('RouteMapper', () => {
expect(typeof useCase).to.equal('function');
});

describe('get a collection', () => {
describe('handling errors during mapping', () => {
let request;

beforeEach(() => {
Expand All @@ -46,26 +46,183 @@ describe('RouteMapper', () => {
.expect(500, (err, res) => {
expect(res.status).to.deep.equal(500);
expect(res.body).to.deep.equal('Request not handled, it must be one of GET, POST, PUT, PATCH, or DELETE');
done();
if (err) done(err);
else done();
});
});

it('should return 404 if url is not matching anything', (done) => {
request
.get('/test/hello/world')
.expect('Content-type', "text/html; charset=utf-8")
.expect(404, () => {
done();
.expect(404, (err, res) => {
if (err) done(err);
else done();
});
});
});

describe('mapping to controller with methods correctly named', () => {
let request;

beforeEach(() => {
app.use('/', map);

request = supertest(app);
});

it('should return a collection', (done) => {
request
.get('/index')
.set('Content-type', 'application/json')
.expect(200, (err, res) => {
done();
if (err) done(err);
else done();
});
});

it('should return a single document', (done) => {
request
.get('/index/param1')
.set('Content-type', 'application/json')
.expect(200, (err, res) => {
if (err) done(err);
else done();
});
});

it('should post a single document', (done) => {
request
.post('/index')
.set('Content-type', 'application/json')
.expect(200, (err, res) => {
if (err) done(err);
else done();
});
});

it('should update a single document', (done) => {
request
.put('/index')
.set('Content-type', 'application/json')
.expect(200, (err, res) => {
if (err) done(err);
else done();
});
});

it('should update a single document', (done) => {
request
.patch('/index')
.set('Content-type', 'application/json')
.expect(200, (err, res) => {
if (err) done(err);
else done();
});
});

it('should delete a single document', (done) => {
request
.delete('/index')
.set('Content-type', 'application/json')
.expect(200, (err, res) => {
if (err) done(err);
else done();
});
});
});

describe('mapping to controller with methods incorrectly named', () => {
let request;

beforeEach(() => {
app.use('/', map);

request = supertest(app);
});

it('should return a collection', (done) => {
request
.get('/beta')
.set('Content-type', 'application/json')
.expect(500, (err, res) => {
expect(res.error.status).to.deep.equal(500);
expect(res.error.text).to.deep.equal(`Trying to use a method not implemented in the controller like advised in the docs of the router.
Go have a look at https://github.com/gaspaonrocks/universal-node-router/blob/master/readme.md for more info.
If it is still not working, post an issue.`);
if (err) done(err);
else done();
});
});
})

it('should return a single document', (done) => {
request
.get('/beta/param1')
.set('Content-type', 'application/json')
.expect(500, (err, res) => {
expect(res.error.status).to.deep.equal(500);
expect(res.error.text).to.deep.equal(`Trying to use a method not implemented in the controller like advised in the docs of the router.
Go have a look at https://github.com/gaspaonrocks/universal-node-router/blob/master/readme.md for more info.
If it is still not working, post an issue.`);
if (err) done(err);
else done();
});
});

it('should post a single document', (done) => {
request
.post('/beta')
.set('Content-type', 'application/json')
.expect(500, (err, res) => {
expect(res.error.status).to.deep.equal(500);
expect(res.error.text).to.deep.equal(`Trying to use a method not implemented in the controller like advised in the docs of the router.
Go have a look at https://github.com/gaspaonrocks/universal-node-router/blob/master/readme.md for more info.
If it is still not working, post an issue.`);
if (err) done(err);
else done();
});
});

it('should update a single document', (done) => {
request
.put('/beta')
.set('Content-type', 'application/json')
.expect(500, (err, res) => {
expect(res.error.status).to.deep.equal(500);
expect(res.error.text).to.deep.equal(`Trying to use a method not implemented in the controller like advised in the docs of the router.
Go have a look at https://github.com/gaspaonrocks/universal-node-router/blob/master/readme.md for more info.
If it is still not working, post an issue.`);
if (err) done(err);
else done();
});
});

it('should update a single document', (done) => {
request
.patch('/beta')
.set('Content-type', 'application/json')
.expect(500, (err, res) => {
expect(res.error.status).to.deep.equal(500);
expect(res.error.text).to.deep.equal(`Trying to use a method not implemented in the controller like advised in the docs of the router.
Go have a look at https://github.com/gaspaonrocks/universal-node-router/blob/master/readme.md for more info.
If it is still not working, post an issue.`);
if (err) done(err);
else done();
});
});

it('should delete a single document', (done) => {
request
.delete('/beta')
.set('Content-type', 'application/json')
.expect(500, (err, res) => {
expect(res.error.status).to.deep.equal(500);
expect(res.error.text).to.deep.equal(`Trying to use a method not implemented in the controller like advised in the docs of the router.
Go have a look at https://github.com/gaspaonrocks/universal-node-router/blob/master/readme.md for more info.
If it is still not working, post an issue.`);
if (err) done(err);
else done();
});
});
});
});
20 changes: 10 additions & 10 deletions test/mocks/beta/index2.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module.exports = {
list(req, res, next) {
res.json({ message: 'You did GET a collection from beta' });
listAll(req, res, next) {
res.status(200).json({ message: 'You did GET a collection from beta' });
},
find(req, res, next) {
res.json({ message: 'You did GET a single document from beta' });
findOne(req, res, next) {
res.status(200).json({ message: 'You did GET a single document from beta' });
},
create(req, res, next) {
res.json({ message: 'You did POST a single document from beta' });
createOne(req, res, next) {
res.status(200).json({ message: 'You did POST a single document from beta' });
},
update(req, res, next) {
res.json({ message: 'You did PUT/PATCH a single document from beta' });
updateOne(req, res, next) {
res.status(200).json({ message: 'You did PUT/PATCH a single document from beta' });
},
delete(req, res, next) {
res.json({ message: 'You did DELETE a single document from beta' });
deleteOne(req, res, next) {
res.status(200).json({ message: 'You did DELETE a single document from beta' });
}
}
10 changes: 5 additions & 5 deletions test/mocks/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module.exports = {
list(req, res, next) {
res.json({ message: 'You did GET a collection from index' });
res.status(200).json({ message: 'You did GET a collection from index' });
},
find(req, res, next) {
res.json({ message: 'You did GET a single document from index' });
res.status(200).json({ message: 'You did GET a single document from index' });
},
create(req, res, next) {
res.json({ message: 'You did POST a single document from index' });
res.status(200).json({ message: 'You did POST a single document from index' });
},
update(req, res, next) {
res.json({ message: 'You did PUT/PATCH a single document from index' });
res.status(200).json({ message: 'You did PUT/PATCH a single document from index' });
},
delete(req, res, next) {
res.json({ message: 'You did DELETE a single document from index' });
res.status(200).json({ message: 'You did DELETE a single document from index' });
}
}

0 comments on commit 43ee2c5

Please sign in to comment.