Created
April 22, 2020 20:02
-
-
Save talitaoliveira/419f2871ce51a2f0d8999af9a1cd296c to your computer and use it in GitHub Desktop.
Testing Chain of Responsibility Design pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProcessValidations { | |
/** | |
* Create concrete element | |
* @param {Array} validators | |
*/ | |
constructor(transaction) { | |
this.validators = [] | |
this.transaction = transaction | |
} | |
setValidators(...validators) { | |
this.validators = validators | |
return this; | |
} | |
process() { | |
let handlers = [...this.validators] | |
const code = 'code' | |
handlers = handlers.map((handler, index) => () => { | |
handler(this.transaction, code, handlers[index + 1]) | |
}) | |
handlers[0]() | |
} | |
} | |
const validation1 = (p1, p2, next) => { | |
console.log('validation1') | |
console.log(p1) | |
console.log(p2) | |
next(p1, p2) | |
} | |
const validation2 = (p1, p2, next) => { | |
console.log('validation2') | |
console.log(p1) | |
console.log(p2) | |
next() | |
} | |
const validation3 = (p1, p2, next) => { | |
console.log('validation3') | |
} | |
const ProcessValidations = | |
new ProcessValidations('transactionValue') | |
.setValidators(validation1, validation2, validation3) | |
.process() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment