From 7c076786021f3adbeff6465ac9533de6d88334eb Mon Sep 17 00:00:00 2001 From: Fabricio Leonardo Sodano Pascazi Date: Sun, 26 Jul 2020 19:24:27 +1000 Subject: [PATCH] Se asegura que AbstractController no se pueda inicializar directamente --- src/module/__test__/abstractController.test.js | 16 ++++++++++++++++ src/module/abstractController.js | 8 ++++++-- src/module/error/abstractControllerError.js | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/module/__test__/abstractController.test.js create mode 100644 src/module/error/abstractControllerError.js diff --git a/src/module/__test__/abstractController.test.js b/src/module/__test__/abstractController.test.js new file mode 100644 index 0000000..6104b2c --- /dev/null +++ b/src/module/__test__/abstractController.test.js @@ -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); +}); diff --git a/src/module/abstractController.js b/src/module/abstractController.js index 8877350..7cb8418 100644 --- a/src/module/abstractController.js +++ b/src/module/abstractController.js @@ -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(); + } } }; diff --git a/src/module/error/abstractControllerError.js b/src/module/error/abstractControllerError.js new file mode 100644 index 0000000..d27e069 --- /dev/null +++ b/src/module/error/abstractControllerError.js @@ -0,0 +1 @@ +module.exports = class AbstractControllerError extends Error {};