Webcomponent Tester - How to assert a function not to be called? - polymer

Using Polymer 1.* and webcomponent tester...
I have spy(alert, 'open') and expect(alert.open).to.have.not.been.called;. How can I assert a function is not called? Right now this. If I inverse it with expect(alert.open).to.have.been.called;, it also fails.
I tried .calledCount(0) and it defaults to has not been called which fails.
The spy is good, it's just complaining ether way that it is not asserted and test fails.

You should expect on the spy itself
const myElement = fixture('my-element');
const openSpy = sinon.spy(myElement, 'open');
//myElement.doSomethingThatShouldNotTriggerOpen();
openSpy.should.have.callCount(0);

Related

An attempt at understanding what ECMAScript-6 Function.prototype.bind() actually does

This question is a follow-up to this one. For some reason I'm coming back to JS after 7 years and boy, I can hardly recognize the dear old beast.
Purely for educational purpose, I decided to rewrite naive implementations of the various things Function.prototype.bind() allows to do. It's just an exercise to try to understand what's going on and spark a few questions.
I would be happy to stand corrected for all the mistakes and misunderstandings in my examples and comments.
Also, this code is not mine. I got the idea from a blog and only slightly tweaked it, but unfortunately I lost track of the source. If anyone recognizes the original, I'll be happy to give due credit. Meanwhile, I apologize for the blunder.
Naive binding
The initial idea is simply to do what lambda calculus savvies apparently call a "partial application", i.e. fixing the value of the first parameters of a function, that also accepts an implicit "this" first parameter, like so:
Function.prototype.naive_bind = function (fixed_this, ...fixed_args) {
const fun = this; // close on the fixed args and the bound function
return function(...free_args) { // leave the rest of the parameters free
return fun.call(fixed_this, ...fixed_args, ...free_args);
}
}
Binding constructors (1st round)
class class1 {
constructor (p1, p2) {
this.p1 = p1;
this.p2 = p2;
}
}
var innocent_bystander = { "huh?":"what?" }
var class2 = class1.naive_bind(innocent_bystander);
class2 (1,2) // exception: class constructor must be invoked with new (as expected)
console.log(new class2(1,2)) // exception: class constructor must be invoked with new (Rats!)
function pseudoclass1 (p1, p2) {
this.p1 = p1;
this.p2 = p2;
}
var pseudoclass2 = pseudoclass1.naive_bind(innocent_bystander);
pseudoclass2 (1,2) // same exception (as expected)
console.log(new pseudoclass2(1,2)) // same again (at least it's consistent...)
Tonnerre de Brest ! Apparently the runtime is not happy with a mere wrapper based on Function.prototype.call().
It seems the real bind() is adding the dollop of secret sauce needed to give the generated function the appropriate "constructor" flavour (Apparently by "constructor" the ECMA 262 specification does not mean a class constructor, but any function that can be invoked with "new" and use "this" to populate the properties and methods of a freshly created object)
Binding other functions
var purefunction1 = console.log
var purefunction2 = purefunction1.naive_bind (null, "Sure,", "but")
purefunction2 ("you can still", "bind pure", "functions")
// sure, but you can still bind pure functions
// ...and make animals talk (by binding simple methods)
var cat = { speech: "meow" }
var dog = { speech: "woof" }
var fish= { speech: "-" }
var talk = function(count) { console.log ((this.speech+", ").repeat(count-1) + this.speech + "!") }
talk.naive_bind (cat,1)(); // meow!
talk.naive_bind (dog,1)(); // woof!
talk.naive_bind (cat)(3) // meow, meow, meow!
talk.naive_bind (fish)(10) // -, -, -, -, -, -, -, -, -, -!
// and bind arrow functions
// ("this" is wasted on them, but their parameters can still be bound)
var surprise = (a,b,c) => console.log (this.surprise, a,b,c)
var puzzlement = surprise.naive_bind(innocent_bystander, "don't", "worry");
// "this" refers to window, so we get our own function as first output.
surprise ("where", "am", "I?") // function surprise(a, b, c) where am I?
// the bound value of this is ignored, but the other arguments are working fine
puzzlement("dude") // function surprise(a, b, c) don't worry dude
Apparently, everything works as expected. Or did I miss something?
Binding constructors (2nd round)
We apparently can't get away with passing a wrapper to new, but we can try invoking new directly.
Since the value of this is provided by new, the construction-specialized wrapper has only to worry about real constructor parameters.
Function.prototype.constructor_bind = function (...fixed_args) {
const fun = this;
return function(...free_args) {
return new fun (...fixed_args, ...free_args);
}
}
var class1_ctor = class1.constructor_bind(1);
console.log (class1_ctor(2)) // class1 { p1:1, p2:2 }
var monster = (a,b) => console.log ("boooh", a, b)
var abomination = monster.constructor_bind(1);
console.log (abomination(2)) // exception: fun is not a constructor (as expected)
Well, that seems to cut it. I imagine the real bind() is far safer and faster, but at least we can reproduce the basic functionalities, namely:
providing a fixed value of this to methods
doing partial applications on any legit function, though constructors require a specific variant
edit: questions removed to comply with SO policy.
Replaced by just one, the one that matches the title of this post, and that I tried to explore by providing a naive and possibly erroneous equivalent of what I thought the real function did:
Please help me understand how the native Function.prototype.bind() method works, according to the version 6.0 of the ECMAScript specification.
The only bit you have been missing is the introduction of new.target in ES6, which a) makes it possible to distinguish between [[call]] and [[construct]] in a function and b) needs to be forwarded in the new call.
So a more complete polyfill might look like this:
Function.prototype.bind = function (fixed_this, ...fixed_args) {
const fun = this;
return function(...free_args) {
return new.target != null
? Reflect.construct(fun, [...fixed_args, ...free_args], new.target)
: fun.call(fixed_this, ...fixed_args, ...free_args);
}
}
Some other details would involve that fun is asserted to be a function object, and that the returned bound function has a special .name, an accurate .length, and no .prototype. You can find these things in the spec, which apparently you've been reading already.

Is 'chaining' functions available in GAS?

Is it possible to 'chain' functions? Say, I have a serverclickhandler and then would like to invoke another function after it right away. How do I go about it?
Thanks.
To have a second handler executed after the first one you just need to call it directly from your first handler, e.g.
var app = null;
function firstHandler(e) {
if( app == null )
app = UiApp.getActiveApplication();
//do your thing
//now, instead "return app;" you return the second handler
return secondHandler(e);
}
function secondHandler(e) {
if( app == null )
app = UiApp.getActiveApplication();
//do your job
return app;
}
I placed the app var on a global scope so you can get it only when necessary (sharing it between the functions without explicit passing it), saving a possibly costly and unknown behavior (to me at least) of a second getActiveApplication() call.
Not sure I understood exactly your question but serverhandlers to which you refer are precisely made for that... (see doc)
Note that you can assign multiple handlers to UI elements to trigger different functions and eventually chain them.

MovieClip extension

I have been trying to develop a CustomButton class that extends MovieClip, however I am having problems. I have got this error:
ArgumentError: Error #1063: Argument count mismatch on
mkh.custombutton::CustomButton(). Expected 2, got 0. at
flash.display::Sprite/constructChildren() at flash.display::Sprite()
at flash.display::MovieClip()
I've tried to debug my code, but it says
"Cannot display source code at this location."
I am not sure where is the problem, but I suppose it's in the constructor:
public function CustomButton( buttonlabel:String, animationAR:Array, active:Boolean=true, animated:Boolean = false, type:String = "free", group:int = 0 )
I would be very grateful if anyone helped me. Thank you.
EDIT2: I think I know why it's not appearing, so nevermind.
Seems like you must be instantiating CustomButton without passing it any arguments.
Like so:
var cBtn = new CustomButton();
However, you constructor has 2 arguments that must be passed - buttonLabel and animationAR (the rest are OK because they are assigned a default value).
So you should be doing something like this:
var cBtn = new CustomButton('Test', someArray);
I think I know what the problem is, now I hope I can explain to you clearly enough (English is not my first language). Did you by any chance make a graphic MovieClip in the Flash program and linked it to your CustomButton class? If so, be careful with the instances you might have on the stage, because when Flash creates the Sprites/Movieclips objects that are on the stage it calls their constructor without any parameters.
To avoid this, either:
Set default values for all parameters in your CustomButtonClass (EDIT: which would solve your problem, but is not very good practice)
Use addChild to put instances of your Button onto the stage (I recommend this one)
Hope this helps!
public function CustomButton(
buttonlabel:String,
animationAR:Array,
active:Boolean=true,
animated:Boolean = false,
type:String = "free",
group:int = 0
);
That's how you defined your constructor. This means that the first 2 arguments (buttonlabel abd animationAR) are required arguments. The rest are optional.
Now if you try to instantiate this like
var cb:CustomButton=new CustomButton();
You are not passing any arguments to the constructot, which will throw that error.
Note that this is what happens when you create the object directly in the UI.
A way to fix this would be to redefine the constructor as:
public function CustomButton(
buttonlabel:String="CustomButton",
animationAR:Array=[],
active:Boolean=true,
animated:Boolean = false,
type:String = "free",
group:int = 0
);
This makes all arguments optional and should work. Of course, you'll be best off putting the default value of the arguments as something you know will work. For example, in my example, the empty array default for animationAR could break your code, in which case you need to add this to the constructor body:
if(animationAR.length==0) {
animationAR.push(new Animation());
//YOU WILL HAVE TO CHANGE THIS LINE TO CORRESPOND TO YOUR CODE
}
OR ELSE, you could instantiate the object as
var cb:CustomButton=new CustomButton("My Crazy-ass CustomButton", animArray);

HTTPService/ResultEvent with Flex 3.2 versus Flex >= 3.5

through a design decission or what-so-ever Adobe changed the content of the ResultEvent fired by a HTTPService Object.
Take a look at following example:
var httpService:HTTPService = myHTTPServices.getResults();
httpService.addEventListener(ResultEvent.RESULT,resultHandler);
httpService.send();
/**
* Handels the login process
*/
function resultHandler(event:ResultEvent):void
{
// get http service
var httpService = (event.target as HTTPService);
// do something
}
It works like a charm with Flex 3.2. But when I try to compile it with Flex 3.5 or Flex 4.0 event.target as HTTPService is null.
I figured out that event.target is now an instance of HTTPOperation. That is interesting because I can't find HTTPOperation in the langref. However, I think what Flash Builder's debugger means is mx.rpc.http.Operation.
The debugger also shows that event.target has a private attribute httpService which is the instance I expected to get with event.target. But it's private, so event.target.httpService doesn't work.
If I only want to remove the EventListener I can cast event.target as EventDispatcher. But I need to use methods from HTTPService.
So: How can I get the HTTPService instance from the ResultEvent?
Any help would be appreciated. Thanks!
J.
It is useful to go through the source if you get into this. On OS X the rpc classes are here: /Applications/Adobe Flash Builder Beta 2/sdks/3.4.1/frameworks/projects/rpc/src
Inside mx.rpc.http.HTTPService there is indeed an inner-class named HTTPOperation.
It extends mx.rpc.http.AbstractOperation which in turn extends mx.rpc.AbstractOperation. Inside AbstractOperation is a getter method get service which looks to return what you need.
Since HTTPService is an inner-class it is effectively private so you'll need to cast to an AbstractOperation (either mx.rpc.http.AbstractOperation or mx.rpc.AbstractOperation).
So something like:
function resultHandler(event:ResultEvent):void
{
// get the operation
var operation:AbstractOperation = AbstractOperation(event.target);
// get http service
var httpService:HTTPService = HTTPService(operation.service);
}
edit: I take it back! Looks like Adobe is sending null for the service when it calls the super when constructing the HTTPOperation. The HTTPService is therefore only cached in the private variable httpService. I have no idea why they hide it from you but it looks like you'll have to keep your own reference around.
I solved this problem for myself.
There are some properties in HTTPService that are available from AbstractOperation. For example, I use property request which is an Object:
myService.request["service"] = myService;
And later, when I get Event which has HTTPOperation in event.currentTarget, I get my HTTPService in such way:
var eventService : HTTPService = HTTPService( AbstractOperation( event.currentTarget ).request["service"] );

How do I reference an object, add a tween to it and put this in an Eventlistener in AS3?

I'm having quite some trouble to try and get an app I wrote in AS2 to AS3. The reason I need to go to AS3 is something icky, so I won't go into detail about it.
I've got 90% of the application running with the new code.
Now I've come to the point where I have to convert this code from AS2,
function setAnimation(theObject,id)
{
theObject.vensterid=id;
theObject.onEnterFrame = function()
{
var myHoriTween:Tween = new Tween (this,"_x",Strong.easeOut,this._x,(130+((theObject.vensterid-frameno)*260)),1,true);
}
}
setAnimation(venster0,0);
, to AS3. My attempt of doing this ended up like
function setAnimation(anObject,id) {
var theObject = this[anObject];
theObject.vensterid=id;
function slideHorizontal(event:Event)
{
var myTween:Tween = new Tween (theObject,"x",Strong.easeOut,this.x,(130+((theObject.vensterid-frameno)*260)),1,true);
}
theObject.addEventListener(Event.ENTER_FRAME,slideHorizontal);
}
setAnimation(venster0,0);
and gives me the following non-error (it doesn't show as a compiler error, but as output):
TypeError: Error #1010: A term is undefined and has no properties.
at sliding_windows_as3_fla::SlideMenu_1/setAnimation()
at sliding_windows_as3_fla::SlideMenu_1/frame1()
I think this is very strange since it doesn't say anything about which term (and there are quite a lot) and googling didn't find me an explanation either.
I didn't get the chance to test your code, because it's difficult to set up a context for it, but my thoughts would be:
You should declare the parameter types: function setAnimation(anObject:Object,id:uint):void. It's at least good practice.
var theObject = this[anObject]; is completely unnecessary if your variable anObject is an object. I think var theObject = this[anObject]; doesn't work, theObject ends up being null and that's why you get your error. If you have declared a variable called venster0, that is the instance of a class that extends Object, then you can pass the reference to it without any other trouble.
Depending on the object you work with, theObject.vensterid=id; might not work. The class that theObject instances must have the 'vensterid' property, or you will get `1119: Access of possibly undefined property vensterid through a reference with static type ...
I think your problem here is following string:
var theObject = this[anObject];
Just replace it with
var theObject = anObject;
I hope that's what you need.
Alternatively instead of
setAnimation(venster0,0);
you could pass an instance name (i.e. String):
setAnimation("venster0",0);
That will work because by this['propertyname'] you are actually accessing Object's property by name.
Just going to throw out that using the built-in Tween classes in Flash/Flex is a pain. Look into using Tweening libraries instead: Tweener, TweenLite, etc. They are much easier to work with, and you don't have to worry about maintaining references until the Tween completes.