Skip to content

Commit

Permalink
Se asegura que AbstractController no se pueda inicializar directamente
Browse files Browse the repository at this point in the history
  • Loading branch information
fsodano committed Jul 26, 2020
1 parent 59ad528 commit 7c07678
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/module/__test__/abstractController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const AbstractController = require('../abstractController');
const AbstractControllerError = require('../error/abstractControllerError');

test('No se puede crear una nueva instancia de un AbstractController directamente', () => {
try {
// eslint-disable-next-line no-new
new AbstractController();
} catch (e) {
expect(e).toBeInstanceOf(AbstractControllerError);
}
});

test('Se puede crear una nueva instancia de una clase que hereda de AbstractController', () => {
const ConcreteController = class extends AbstractController {};
expect(new ConcreteController()).toBeInstanceOf(AbstractController);
});
8 changes: 6 additions & 2 deletions src/module/abstractController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const AbstractControllerError = require('./error/abstractControllerError');

module.exports = class AbstractController {
getSessionErrors(session) {
return Array.isArray(session.errors) ? session.errors : [];
constructor() {
if (new.target === AbstractController) {
throw new AbstractControllerError();
}
}
};
1 change: 1 addition & 0 deletions src/module/error/abstractControllerError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = class AbstractControllerError extends Error {};

0 comments on commit 7c07678

Please sign in to comment.