How to spy on function from another module - javascript

I have two functions in one module:
export function foo() {
console.log('foo');
}
export function bar() {
foo()
}
Now I want to test bar(), set a spy on foo() and assert for it to be called. How can this be achieved?

EDIT/update: Sorry had bar and foo backwards.
The way the module is currently structured.
When the code is executed, the bar reference inside function bar is resolved against the local implementation. You can't modify that since it's outside of the module code. There's no access to the intervals.
Have you tried using code like this for the test file?
let obj = {};
obj.bar = function () {
this.foo();
}
obj.foo = function() {
...
}
export default obj;

Related

How to mock local function in Karma?

I have to test a js file that looks like the following:
const varA = document.A;
// some action with foo and bar
return varA;
function foo() {
}
function bar() {
}
I can mock document.A property this way:
sandbox.stub(document, 'A').value('test value');
But is there any way to mock local foo and bar functions?

Is there a way to have a constructor within an already existing constructor?

I have the following function that acts a base constructor:
function Foo() = {}
...
exports Foo
It's an empty constructor, and is used by other functions which I don't want to change. Also, Foo is only being exported from the file.
Now I need to create a different constructor perhaps within Foo.
The following is what I'd have with enum as a standalone constructor. But how do I make it a part of Foo?
function enum(data) {
this.data = data
}
enum.prototype.getVal() { return this.data; }
var obj = new enum(5);
obj.getVal();
Taking a completely wild guess from your commnets.
exporting from the file is of no real concern. If you're just using Foo as a namespace to "hang" functions off of you can do this:
Enum.js
// all constructors should be capitalized
function Enum() { }
Enum.prototype.whatever ...
exports Enum
Foo.js
const Enum = require('./Enum');
// it's unclear why `Foo` is even a function to be honest
function Foo() { }
Foo.Enum = Enum;
exports Foo
someOtherFile.js
const Foo = require('./Foo');
const myEnumInstance = new Foo.Enum();

how to override a javascript closure function call

I have the following scenario
var bar = (function () {
function foo() {
console.log("Hello World");
}
function bar() {
foo();
}
return bar;
} ());
// override foo before the call
bar();
Since foo is being called from the bar function which is returned from inside the a closure, it can be considered that foo is acting like a private property.
I want to know if it is possible to override the foo function before the bar is called? How many ways can it be achieved?
Here is something that is tried but I was unable to achieve the result.
var bar = (function () {
function foo() {
console.log("Hello World");
}
function bar() {
foo();
}
return bar;
} ());
var _bar = (function () {
function foo() {
console.log("Hey there");
}
return bar;
} ());
_bar();
I want to know If it is possible to override the foo function before the bar is called? How many ways can it be achived?
No, not and keep/reuse the current functionality of bar. You could completely replace bar, but that's not particularly useful.
foo is entirely private to the context in which it was created. bar has access to it, but nothing else does (well, okay, foo has access to itself). There's no way to reach into the execution context bar closes over and change its foo binding unless something explicitly enables that, which nothing in your example does.

member function isn't a function error

With this code:
function thing(){
function majig(){
alert("done");
}
}
var mything = new thing();
mything.majig();
I'm getting this error:
TypeError: mything.majig is not a function
I've done javascript for some time, and I've done functions as part of functions and called them before. I know it has to be something simple I'm missing or forgetting, but various websearches (and poking around here) are getting me deeper theory answers, or examples that seem to indicate that this should work.
I know TypeError: foo is not a function usually means a syntax error. I've looked up examples, and it looks like I have the syntax right (I've tried a few variations with no success).
It's got to be some dumb simple mistake, but I'm just not catching it right now. What do I do in my function to make the mything.majig(); run properly?
You have declared a function in thing, but it's not attached to this at all. Try this:
function thing(){
this.majig = function() {
alert("done");
}
}
var mything = new thing();
mything.majig();
Alternately:
function thing() {
return {
majig: function() {
alert("done");
}
};
}
Or, better yet:
function thing() { }
thing.prototype.majig = function () {
alert('done');
}
The syntax is not what you think it means. It's not a member declaration. It's an inner function. Inner functions work just like local variables - they're only accessible in the scope of the outer function:
function foo () {
function bar () {}
bar(); // accessible here
}
bar(); // undefined here
If your function is a constructor, then to add a member function to the object that it constructs you'd add it to the constructor's prototype:
function Foo () {}
Foo.prototype.bar = function () {}; // bar is a member of object Foo
var f = new Foo();
f.bar(); // call member function
Objects in javascript are dynamic. They behave more like maps/hashes do in other languages. This means you can add a member function directly to an object instead of a constructor's prototype:
var f = {};
f.bar = function () {};
f.bar(); // call member function
Following the logic above, since this in a constructor refers to the object being constructed, you can also dynamically add a function to this. This is typically called "decoration" since it is effectively an ad-hoc version of the decorator design pattern:
function Foo () {
this.bar = function () {}
}
var f = new Foo();
f.bar();

JavaScript design patterns: Injecting a dependency that is not yet created

I have a CommonJS module:
// main-module
module.exports = function () {
var foo,
someModule = require('other-module')(foo);
// A value is given to foo after other-module has been initialised
foo = "bar";
}
As you can see, this requires other-module:
// other-module.js
module.exports = function (foo) {
function example() {
console.log(foo);
// > "bar"
}
}
I would like the example function inside of other-module to be aware of the foo variable inside of main-module, even though is it established after the module is required.
When other-module runs, foo will not be undefined. However, the point is that by time my example function runs, foo will have been given a value of bar.
The pattern above obviously does not work. What design pattern do I need to implement?
I'm not super-familiar with CommonJS, so this might not be the idiomatic way to do it, but using a function instead of a variable should work:
// main-module
module.exports = function () {
var foo,
someModule = require('other-module')(function() { return foo; });
foo = "bar";
}
// other-module.js
module.exports = function (fooFn) {
function example() {
console.log(fooFn());
}
}
The foo value (a string) will be passed by value, so it's undefined inside other-module. You could use an options object that is passed by reference:
var options = {},
someModule = require('other-module')(options);
options.foo = "bar";

Categories