2021-02-28

Problem applying Decorator Design Pattern to JavaScript code

I have a class named Calculation which I added two functions are addOperationToName() and sayOperation() as examples of the Decorator design pattern. However, when I test it using Jest, I got an error that 'fn is not a function'. Can someone help me?

Calculation.js

class Calculation {
constructor(a, b, op) {
    this.a = a;
    this.b = b;
    this.op = op;
}

static Create(a, b, op){
    return new Calculation(a, b, op);
}

 GetResults() {
    return this.op(this.a,this.b)
}

addOperationToName(fn){
    return function(name){
        const operation = name + ' is a operation';
        fn(operation);
    }
}

sayOperation(name){
    console.log(name);
}
}
module.exports = Calculation;

Calculation.test.js

const Calculation = require('../src/models/Calculation');

test('Test say name for Product operation', () => {
let op = Product;
let calculation = new Calculation(1,2,op);
let sayTest = calculation.addOperationToName(calculation.sayOperation());
expect(sayTest('Multiply')).toBe('Multiply is an operation');
});


from Recent Questions - Stack Overflow https://ift.tt/2O8DBDi
https://ift.tt/eA8V8J

No comments:

Post a Comment