Ajax & Jasmine Flashcards
What is the process of an Ajax Call?
Split transaction. You send something to server. Server responds. Triggers Event. Then you can run code based on that event.
What are events?
Typical user events like moving the mouse, etc. Then you can bind your javascript objects using JQuery to a certain event.
How do you ensure that javascript will work on a browser?
You put your javascript functions in a setup code so that it can only run once javascript is setup and will not run if they do not have javascript.
Steps to Manipulate the DOM using events.
Identify elements you wins to work on.
Identify elements that interactions will trigger stuff to happen
Create failing tests for handler with TDD
Write Handler functions that cause desired act
In a setup function, bind the handlers.
Make sure you call $(variable.setup)
What happens to links or buttons that are clickable without javascript?
- handler runs first
- if handler returns false or no action taken
- otherwise other actions follow handler
What is Ajax?
JSAPI call XmlHttpRequest contacts server asynchronously (in background) and without redrawing the page. We can use the information from the server anyway we want.
A controller action receives request via route what should it render if the request is an Ajax call?
You can render a lot of different things. render :layout => false render :partial => 'movies/show' render :json => @movies render :xml => @movies render :text => @movie.title render :nothing => true
Keys to Ajax Calls: Réponse Content Type? Controller Actions? Server rely on? Will it Freeze the UI?
Response Content type can be anything, not just HTML
Ajax can be handled with its own controller actions
Server must rely on an explicit hint
It won’t freeze the UI if server fails to respond?
What are True of Jasmine’s it() method?
It can take a named function as its second argument.
It can take an anonymous function as its 2nd argument
it executes asynchronously.
It can take a names function as its 2nd argument
It can take an anonymous function as its 2 argument.
It does not execute asynchronously.
What are stubs in Jasmine?
They are called spies. They shadow the method and they basically replaces the method. Stub out the place you want to call and then give it the values it should return.
What about Fixtures in Jasmine?
Just enough html for your function to be able to run in its environment. You call load fixtures(‘name.html’) and then it will load it into div#jasmine-fixtures
What about Stubbing Ajax?
Remember that Ajax then calls another function. The success function gets called in Ajax if the call goes through so to spy on Ajax you have to use
spyOn($, ‘ajax’).andCallFake(function(ajaxArgs) {
ajaxArgs.success(htmlResponse, ‘200’);
});
Does Ajax Return Server reply content?
No it does not. So in stubbing, andReturn won’t work.
My Ajax calls are slow, can I assume that it is the server?
No it could be slow interpreter, could be slow database, could be network delays because too much data to browser.
Why would you want to minify javascript?
Fast page load times