Design Pattern Singleton implemented in JavaScript Code error - javascript

I have the CalculationDesignSingleton.js file, and the CalculationDesignSingleton.test.js which is using Jest to test the file. When I run the test file, I received an error said calculation.getInstance is not a function. Can someone help me? Thank you
CalculationDesignSingleton.js
let calculation = (function () {
let instance;
function createInstance() {
let calculator = new Calculator("I am the instance");
return calculator;
}
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
CalculationDesignSingleton.test.js
const calculation = require('../src/Operations/CalculationDesignSingleton');
test('Test Design Pattern Singleton of Calculation', () => {
//I need to test the get results function
const singletonA = calculation.getInstance();
const singletonB = calculation.getInstance();
const isEqual = singletonA === singletonB;
expect(isEqual).toBe(true);
});

Related

Javascript arrow notation functions undefined syntax error

I am new to javascript, i trying to learn to js, here i am getting syntax error while console the output
const screemwarriors = () => {
var warriors = "Ninja";
shootwarriors = function(){
console.log(warriors);
}
}
const output = screemwarriors();
console.log(output.shootwarriors());
Here i am getting syntax error
Looks like your trying to access the result as an object. You must return the function in an object if you intend to do it in this way.
Update: cleaned up code
const screemwarriors = () => ({
shootwarriors: () => {
console.log("Ninja");
}
});
const output = screemwarriors();
output.shootwarriors();
Cheers,
Your calling code will work if you modify your function like this:
const screemwarriors = function() {
var warriors = 'Ninja';
return {
shootwarriors: function() {
return warriors;
}
}
}
const output = screemwarriors();
console.log(output.shootwarriors());

How spy whether a function has been used or not with Jest unit testing for javascript?

When I try to set a spy on a imported function I get the following error msg TypeError: Cannot read property '_isMockFunction' of undefined
I don't understand what it's wrong with this code
Imported function is like below here
export
export
function myFn(){
let htmlEl = document.querySelector('html');
let el = document.querySelector('.target-el');
if(el){
el.addEventListener('click', myInternalFn, false);
}
function myInternalFn () {
isUserLoggedIn((isIn) => {
let logoutClassName = 'el--logout';
if (isIn) {
el.classList.remove(logoutClassName);
return;
}
el.classList.add(logoutClassName);
});
}
function isUserLoggedIn (fn) {
return fn(localStorage.getItem('userLoggedIn') === 'true');
}
}
document.addEventListener('DOMContentLoaded', () => {
myFn();
});
TDD:
import { betSlip } from "../src/main/javascript/custom/betslip-dialog";
describe('Testing bet slip button (only on mobile)', function () {
let htmlEl;
let el;
beforeEach(() => {
document.body.innerHTML =
`
<html>
<div class="target-el"></div>
</html>
`;
myFn();
htmlEl = document.querySelector('html');
});
it('When el button has been clicked for the first time', done => {
jest.spyOn(myFn, 'myInternalFn');
myInternalFn.click();
expect(true).toBe(true);
done();
});
});
According to Jest docs https://facebook.github.io/jest/docs/en/jest-object.html#jestspyonobject-methodname in your code
jest.spyOn(myFn, 'myInternalFn');
myFn needs to be an object and myInternalFn needs to be a property of this object.
In current realization myInternalFn is hidden in myFnscope, and isn't exposed to outside.
I suggest you to rewrite code (if it's possible) to use either prototype:
myFn.prototype.myInternalFn = function myInternalFn () { ... }
//and in tests
jest.spyOn(myFn.prototype, 'myInternalFn');
or direct assign to function object(not the best way as for me)
myFn.myInternalFn = function myInternalFn () { ... }
// and in tests
jest.spyOn(myFn, 'myInternalFn');
A main idea is - without public exposing myInternalFn you can't hang spy on it.

How to implement the Module design pattern in JavaScript

I am reviewing some design patterns in JavaScript. My first design pattern is the module pattern. After researching a bit I built the following example:
var CarCostEstimation = (function() {
//Let's imagine script is getting best price from different api's
function getBestMotorCost() {
return 3000;
}
function getBestChasisCost() {
return 1000;
}
function getBestWheelsCost() {
return 400;
}
return {
getEstimatedCost: function() {
var chasisCost = getBestChasisCost();
var motorCost = getBestMotorCost();
var wheelsCost = getBestWheelsCost();
return chasisCost + motorCost + wheelsCost;
}
}
})();
var totalCost = CarCostEstimation.getEstimatedCost();
console.log(totalCost);
I have two doubts:
If the way I am calling the "private" methods, in my public method is the correct one.
If I want to add this CarCostEstimation module to another Estimation module, how would it do it?
For point 2:
var CostEstimation = (function() {
var estimationObject;
function sumDifferentEstimations() {
estimationObject.getEstimatedCost();
}
return {
addEstimationObjetc: function(object) {
estimationObject = object
},
getTotalCost: function() {
return sumDifferentEstimations();
}
}
})();
Another way to define your functions. (I'm also using some ES6 syntax)
const CarCostEstimation = function() {
return {
getBestMotorCost: () => 3000,
getBestChasisCost: () => 1000,
getBestWheelsCost: () => 400,
getEstimatedCost: function() {
const chasisCost = this.getBestChasisCost();
const motorCost = this.getBestMotorCost();
const wheelsCost = this.getBestWheelsCost();
return chasisCost + motorCost + wheelsCost;
}
}
}();
const totalCost = CarCostEstimation.getEstimatedCost();
console.log(totalCost);
To export your function as a module, you could use the export statement from ES6 and import your module using the import statement
https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export

javascript const in javascript library

I am trying to write a small javascript library as shown below. What I really want is when I call
console.log(tnd().pv);
it should output same number and not generate new number everytime. I know the issue is it calls Math.random everytime I console log. But how can I do so that it outputs same number?
(function () {
var tnd = function() {
return new tnlib();
};
var tnlib = function() {
this.version = function(){
console.log('1.0');
};
this.pv = Math.random()*10000000000000000;
};
if(!window.tnd) {
window.tnd = tnd;
}
})();
Don't execute Math.random() on each invocation of tnlib, but as a static variable:
(function () {
function tnd() {
return new tnlib();
}
function tnlib() {
}
tnlib.prototype.version = function(){
console.log('1.0');
};
tnlib.prototype.pv = Math.random()*10000000000000000;
if (!window.tnd) {
window.tnd = tnd;
}
}());
(or, if you really need to make pv an instance property):
var staticPv = Math.random()*10000000000000000;
function tnlib() {
this.pv = staticPv;
…
}

jsMockito: How to run callback sended to mock?

I am learning how to use jsMockito to write perfect code. So, could you give me any idea on how to run callback which is provided to service?
Here is my class:
function MyClass(service) {
this.service = service;
}
MyClass.prototype.doSomething = function() {
this.service.doIt(function() {
console.log("How to run this function while running tests?");
})
}
And here is my test:
var MyClassTest = TestCase("MyClassTest");
MyClassTest.prototype.testMyClass = function() {
this.service = mock(Service);
this.myClass = new MyClass(this.service);
this.myClass.doSomething();
}
So, I need to see the log message:
"How to run this function while running tests?"
Any ideas are welcome.
Finally I have found the solution.
We need to create doItCallback:
function MyClass(service) {
this.service = service;
}
MyClass.prototype.doItCallback = function() {
console.log("How to run this function while running tests?");
}
MyClass.prototype.doSomething = function() {
this.service.doIt(this.doItCallback);
}
Also we need to update the mock:
var MyClassTest = TestCase("MyClassTest");
MyClassTest.prototype.testMyClass = function() {
this.service = mock(Service);
this.myClass = new MyClass(this.service);
var myClass = this.myClass;
when(this.service).doIt().then(function() {
myClass.doItCallback();
});
this.myClass.doSomething();
}

Categories