Jasmine .andCallFake not triggering for function call with spineJs - testing

I am using jasmine to test my front end, and have a spy set up to watch for the edit function to be called within a controller. The callback takes a message and either brings up the edit view or throws an error.
spyOn(edit, "edit").andCallFake (callback) ->
console.log(callback)
callback()
I also have a spy setup to watch for a function in the model that fetches an updated version of the item within the edit controller.
spyOn(ag, "fetchLatestVersion").andCallFake (callback) ->
console.log(callback)
callback()
This function returns a message that gets sent to the edit callback and then displays the view or throws an error.
My edit function is running correctly until it gets to the fetchLatestVersion() function and then it doesn't seem to want to run the callback and doesn't even seem to output what the callback is. Any help with jasmine's .andCallFake() would be much appreciated.
Thanks in advance!
EDIT:
I just removed the edit spy (ended up being unnecessary) and my error has since changed. I am receiving the correct callback function from .fetchLatestVersion(), but I end up getting an error saying:
Error: Expected a spy, but got Function.
Let me know if you need more information. Thanks again!

This turned out to be an issue with Spine (the frontend framework) and how it finds objects. It makes a clone rather than returning the actual object. By changing the records to irecords I was able to get the test to pass correctly!

Related

Vue: Showing that the function does not exist when I have defined it the relevant store

I am currently working on a simple app to store workout routines in Nuxt 3 and Appwrite. The link to the source code is here.
After logging in and adding in some workouts in the app's UI, whenever I try to call the deleteWorkout function, I get an error in the console saying that the function is not defined, whereas I have clearly defined in the workoutStore. I can't seem to figure out the reason for the same.
The same can be seen in the given screenshot.
Console on clicking the delete button
PS:
Most probably the error should be originating from either /pages/workouts.vue, /components/WorkoutDetails.vue or /stores/workout.js.
I am using Appwrite to manage the back-end of the web app, and the instructions to setup the same can be found in the README.md. (Though I don't think the error I am facing is related to the same.)
In your code the problem is, you declear your deleteWorkout() function outside of the actions block in workout.js file.
Make sure all your functions in the workout store are inside the actions block. Then it will be accessable from the vue component

Selenium - watch for an error condition AND run 'happy path' test code

My app displays an error dialog whenever a JavaScript error occurs. This is always a bad sign, so I want to set up my tests so that, if the error dialog appears, it causes the test to fail there and then.
So I'd like to do something like (very much pseudocode!);
// start a new 'guard' thread;
start {
found = this.driver.wait(untilVisible(By.css('.myErrorDialog')), VERY_LONG_TIMEOUT);
if (found) {
// the error dialog appeared! That's bad!
throw();
}
}
// now run the test
login();
clickButton();
testBannerContains();
But I'm having trouble and I think it has to do with the way Selenium schedules actions.
What I've found is that for a single driver, I can only schedule one thing at a time, so the guard I set up early in the test blocks the body of the test from starting.
Is there a better way to handle conditions like 'this should never happen', or a way to create two independent threads in the same test?
So the problem with the code you have is that it immediately runs it and waits for a VERY_LONG_TIMEOUT amount of time for that error dialog to appear. Since it never does, it continues to wait. You have already discovered that is not what you want... ;)
I haven't done anything like this but I think you want a JS event handler that watches for the event that is triggered when the error dialog appears. See the link below for some guidance there.
Can my WebDriver script catch a event from the webpage?
One option would be to watch for that event to fire and then store true (or whatever) in some JS variable. Before leaving a page, check to see if the variable is set to true and if so, fail the test. You can set and get JS variables using JavascriptExecutor. Some google searches should get you all you need to use it.

Confusing Swift type annotations for React Native Promises

I'm playing around with React Native and attempting to write some native code that communicates over bluetooth. I'm confused by the type annotations that I need to use in order for it to work. Could someone please explain why I have to have the "resolver" and "rejecter" bits in the following two code snippets? Is there a way to write this without those unused parts?
My implementation, MyAsyncModule.swift:
#objc(MyAsyncModule)
class MyAsyncModule: NSObject {
#objc func echoAsync(
input: NSNumber,
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock
) -> Void {
resolve(input)
}
}
From my bridge file, MyAsyncModuleBridge.m
RCT_EXTERN_METHOD(echoAsync:
(nonnull NSNumber *)input
resolver:(RCTPromiseResolveBlock *)resolve
rejecter:(RCTPromiseRejectBlock *)reject
)
I am coming from scripting land so types are foreign to me, but it seems too weird that React Native refuses to identify my the echoAsync method unless both the implementation and the bridge include the resolver and rejecter bits...
The resolver and reject calls are needed to have the framework generate a "promise". A promise can be thought of as a placeholder for a value that will be made available in the future. The resolver is called when the native code is done doing its work and is ready to pass the results back to the JavaScript land. reject is used when the native side detects an error and is used to report that error from native to JavaScript land.
To get a bit deeper, when you're JavaScript calls a native function, it doesn't pause and wait for native to finish up like a normal function call. It instead just goes on executing the next line of code (notice how React-Native prevents you from setting a return value for your exported functions meaning they are explicitly making sure you don't try and wait for a return value).
So then how does native code ever report the results back to JavaScript? There are two options
callbacks (in native these have the type RCTResponseSenderBlock) when called, cause a JavaScript function to run with the passed arguments
promises (with the types RCTPromiseResolveBlock and RCTPromiseRejectBlock) which causes you success handler to run with the passed arguments when resolver is called or causes your error handler to run when reject is called.
As for async function you MUST use promises.
For more info on JavaScript promises checkout:
http://www.html5rocks.com/en/tutorials/es6/promises/
https://facebook.github.io/react-native/docs/native-modules-ios.html#promises

CoffeeScript function can not be found

I have a Rails 3.2.8 application that I have recently upgraded from 3.1, and I have converted all of the original application.js code to CoffeeScript. Most of it is working fine. However, I have a breadcrumb function that I call in several views that is not being found. For right now, I'm just throwing up an alert to see if it is working:
product_breadcrumb = (attr) ->
alert attr
That's in a file called product_search.js.coffee. It is being successfully compiled, and ends up looking like this:
(function() {
var product_breadcrumb;
product_breadcrumb = function(attr) {
return alert(attr);
};
}).call(this);
I guess that's right, I don't know. Anyway, in Firebug I'm getting:
ReferenceError: product_breadcrumb is not defined
Please note that this is after an Ajax call. I don't know why the function wouldn't be available though. It's just a function definition after all. Shouldn't it still be available to the rendered HTML from the Ajax call? I can't understand why the function can't be found.
This needs to be on the global scope, and then you need to call it that way.
You should write:
root = exports ? this
and name your function
root.product_breadcrumb
then you can call it elsewhere as expected.
See this answer for a much lengthier explanation.

Safari Extension - How to respond to Settings changes?

I'm currently working on an Extension for Safari 5 and I want to run a listener function whenever Settings changes are made. Apple provides an example for that, but it doesn't work for me. I currently have this listener function in my global html file:
function numberChanged()
{
if(event.key == "number")
alert("Number has changed!");
}
safari.self.addEventListener("change", numberChanged, false);
I hope somebody can help me. Does somebody know what I'm doing wrong?
I believe that you need to include ‘event’ as a parameter in your function so it looks like this:
function numberChanged(event)
{
if(event.key == "number")
alert("Number has changed!");
}
however, that said, it’s not working properly for me either (with or without the param), so I might be wrong. Interestingly, every time I change a field or click a button on this stackoverflow form, my alert (similar to yours) IS firing, even though I did not change my setting. totally weird.
update: I got it working, finally. The example that apple provides is just wrong. So there are two parts to the answer. I gave the first part above — you do need to add ‘event’ as a parameter to your function. the second part is that the addeventlistener has to be done on the settings object and not, as apple shows you, using ‘self’ from the global.html page. so the working call would look like this for you:
safari.extension.settings.addEventListener("change",numberChanged,false);