Skip to content

Commit

Permalink
finish sns tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelvilche committed Jun 18, 2020
1 parent c45ce42 commit 63c029b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 49 deletions.
36 changes: 9 additions & 27 deletions lib/sns/serverless/dispatcher.js
Expand Up @@ -23,10 +23,16 @@ class SNSServerlessDispatcher {

const listener = new Listener(this._event);

await this.process(listener);
if(typeof listener.process !== 'function')
throw new SNSServerlessHandlerError('Process method is required and must be a function', SNSServerlessHandlerError.codes.PROCESS_NOT_FOUND);

if(this.error && !listener.notThrow)
throw this.error;
try {

await listener.process();

} catch(err) {
throw new SNSServerlessHandlerError(err, SNSServerlessHandlerError.codes.INTERNAL_ERROR);
}
}

/**
Expand All @@ -50,30 +56,6 @@ class SNSServerlessDispatcher {
throw new SNSServerlessHandlerError('Invalid message cannot parse the body from Records', SNSServerlessHandlerError.codes.INVALID_MESSAGE);
}
}

/**
* Process the event and send to listener
*
* @static
* @param {ObjectConstructor} Listener
* @memberof SNSServerlessDispatcher
*/
static async process(listener) {

try {
if(typeof listener.process !== 'function')
throw new SNSServerlessHandlerError('Process method is required and must be a function', SNSServerlessHandlerError.codes.PROCESS_NOT_FOUND);

if(listener.struct)
listener.validate();

return listener.process();

} catch(err) {
this.error = new SNSServerlessHandlerError(err, SNSServerlessHandlerError.codes.INTERNAL_ERROR);
}
}

}

module.exports = SNSServerlessDispatcher;
43 changes: 21 additions & 22 deletions tests/sns/serverless/handler.js
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');

const sandbox = require('sinon').createSandbox();

const { SNSServerlessHandler, SNSListener } = require('../../../lib');
Expand Down Expand Up @@ -111,33 +110,33 @@ describe('Serverless Handler Test', () => {
});
});

// it('Should throw an error when process is not found', () => {
it('Should throw an error when process is not found', () => {

// const ListernerTestWithoutProcess = function() {};
const ListernerTestWithoutProcess = function() {};

// assert.rejects(SNSServerlessHandler.handle(ListernerTestWithoutProcess, event), {
// name: 'SNSServerlessHandlerError',
// code: 4,
// message: 'Process method is required and must be a function'
// });
// });
assert.rejects(SNSServerlessHandler.handle(ListernerTestWithoutProcess, event), {
name: 'SNSServerlessHandlerError',
code: 4,
message: 'Process method is required and must be a function'
});
});

// it('Should throw an error when process throws an error', () => {
it('Should throw an error when process throws an error', async () => {

// const error = new Error('This is an error originated on listener process method');
const error = new Error('This is an error originated on listener process method');

// const ListernerTestProcessError = function() {};
// ListernerTestProcessError.prototype.process = async function() {
// throw error;
// };
const ListernerTestProcessError = function() {};
ListernerTestProcessError.prototype.process = async function() {
throw error;
};

// assert.rejects(SNSServerlessHandler.handle(ListernerTestProcessError, event), {
// name: 'SNSServerlessHandlerError',
// code: 5,
// message: 'This is an error originated on listener process method',
// previousError: error
// });
// });
await assert.rejects(SNSServerlessHandler.handle(ListernerTestProcessError, event), {
name: 'SNSServerlessHandlerError',
code: 5,
message: 'This is an error originated on listener process method',
previousError: error
});
});

it('Should process the event and set the properties to listener', () => {

Expand Down

0 comments on commit 63c029b

Please sign in to comment.