Using Jasmine to `spyOn` a function in a (different) closure - jasmine

We're using require and Browserify, so single-function modules are imported like this:
var loadJson = require('../loadJson');
and used like this:
x = loadJson(url);
I'd like to spyOn that loadJson function, but it doesn't seem to be possible.
It's not a global function, so this doesn't work:
spyOn(window, 'loadJson')
It's not a local function, so this doesn't work:
loadJson = createSpy('loadJsonSpy', loadJson).and.callThrough();
When I require the module into my Jasmine spec, the function is visible inside that closure, but that's not the same closure as the other module which are actually using loadJson for real.
So in short, I think it's not possibly to use spyOn in this case - is that correct? Any creative workarounds?

If loadJson is singleton, then you can do this.
var functionsToSpyOn = {loadJson: loadJson}
spyOn(functionsToSpyOn, 'loadJson')
This is the workaround I used when I had the same problem.

Related

Jasmine-marbles - Is there a expect(...).ToHaveBeenCalledWithObservable(...) function?

Is there a way to test observable in a function arguments?
Is there any thing like expect(someObj.foo).ToHaveBeenCalledWithObservable(cold('a|', {a: 1}))?
I don't think there is something that like that but you can maybe take advantage of callFake and toBeObservable.
We callFake and associate a local variable to the argument that was used.
Then we assert the localVariable toBeObservable of your expecation.
let argumentForFoo: Observable<any>;
spyOn(someObj.foo).and.callFake(argument => argumentForFoo = argument);
// make sure someObj.foo gets called somewhere here so the callFake can run.
expect(argumentForFoo).toBeObservable(/*...*/);

p5.js - When to declare variables using var vs this.varName

I'm still figuring out how to use p5.js. In regular java you have to declare each variable using its data type, ex. int foo = 0.
In p5, I know you can just use var foo but you can also declare variables using this.foo. If someone could clarify when is the proper time to use var and when i can use this, that would be very helpful.
For example, if I want to declare a variable inside a method, should i use var foo = thing or could I declare it using this.foo = thing? What should I use when declaring global variables or when referring to objects passed into methods?
Thanks!
First of all, p5 is not a language, it is a Javascript library, you are coding in Javascript, not p5.
Coming to your question, if you want to use some function as a data type, similar to a class in java, and want all the "instances" of that to have their own different variables, you use this. If they are just variables you use in someway but don't need to be specific for each instance, or if the function is not a constructor function and is not to be used as a data type, you will just use var then.
Again, there is no class stuff in javascript, you will have to write what is called a constructor function in order to "simulate" a java class, but be aware that a constructor function should not return anything. Here is an example of car class in java:
class car {
int speed = ___;
String model = ___;
static int numOfWheels = ___;
}
This is what it will look like in javascript (a constructor function):
function car() {
this.speed = ____;
this.model = ____;
var numOfWheels = ___;
}
If you declare a variable without this, it can be roughly compared to a static variable in a java class in the sense that it will be constant among all the instances.
So basically, at least in most cases, you will use this.varName usually inside constructor functions, i.e., functions that you will use to construct objects.
What should I use when declaring global variables or when referring to objects passed into methods?
Global variables will almost always be var something = something. When referring to objects passed into functions, just use the dot notation to refer to its properties like passedObject.someProperty
I would recommend you to learn Javascript before jumping into p5 directly, here are some resources that I found useful when I started learning Javascript-
w3 School
JavaScript Info Website
TheNewBoston

Access SASS variable in function

If I have the following in my SASS:
$my-var: 1;
my_function();
And this as a function defined:
module Sass::Script::Functions
def my_function
end
end
Is it possible to access the SASS variable $my-var within that function?
Edit:
My use case is that this function will be used in multiple stylesheets which all define their own value of $my-var as appropriate (for an id image URL path). This function will then be called in quite a few places within those stylesheets so I was hoping to avoid always passing in the variable to the function.
Assuming that you need to drop down to the Ruby level for your function, you could use a Sass function as a wrapper around it like this:
$default-var: 1;
#function sass-function($var: $default-var) {
#return ruby-function($var);
}

make document.getElementById into a var

Is there any way you can make "document.getElementById" into a variable?
I want to be able to write
myVariable("id").innerHTML = (blabla);
instead of
document.getElementById("id").innerHTML = (blabla);
Pardon me if this has been answered. I've sought and found nil! newbie
You can wrap the output into another function
var shortID = function(id) {
return document.getElementById(id);
}
shortID('myID').innerHTML = "...";
To provide a simpler way to call common code, you can just define your own function like migvill suggests.
To answer the question directly though, you can point to the document.getElementById function (or any other function) using a variable. This is as obvious as you could imagine:
var myVariable = document.getElementById;
The problem with this is that the function itself (the object that myVariable now points to) is not intrinsically linked to the document object that it is designed to work with. When you write document.getElementById("id"), the document is automatically given to the function, but with myVariable you would need to specify it. This can be done using the call function:
myVariable.call(document, "id").innerHTML = "blabla";
And finally, the bind function can be used to create a new function that automatically links the given object (this has essentially the same effect as defining your own wrapper function):
var newFunc = myVariable.bind(document);
newFunc("id").innerHTML = "blabla";

Defining Lua methods as initialization

In the Lua language, I am able to define functions in a table with something such as
table = { myfunction = function(x) return x end }
I wondered if I can created methods this way, instead of having to do it like
function table:mymethod() ... end
I am fairly sure it is possible to add methods this way, but I am unsure of the proper name of this technique, and I cannot find it looking for "lua" and "methods" or such.
My intention is to pass a table to a function such as myfunction({data= stuff, name = returnedName, ?method?init() = stuff}).
Unfortunately I have tried several combinations with the colon method declaration but none of them is valid syntax.
So...anyone here happens to know?
Sure: table:method() is just syntactic sugar for table.method(self), but you have to take care of the self argument. If you do
tab={f=function(x)return x end }
then tab:f(x) won't work, as this actually is tab.f(tab,x) and thus will return tab instead of x.
You might take a look on the lua users wiki on object orientation or PiL chapter 16.

Resources