TAILIEUCHUNG - Test Driven JavaScript Development- P15

Test Driven JavaScript Development- P15:This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage, and gaining the ability to fearlessly refactor and organically evolve your code base. It is about writing modular and testable code. It is about writing JavaScript that works in a wide variety of environments and that doesn’t get in your user’s way. | Using the Ajax API 273 requestComplete transport options null . Adding these two lines makes the tests pass again. Re-running the massive request integration test in Internet Explorer confirms that the memory leak is now gone. Local Requests The last issue with the current implementation is that it is unable to make local requests. Doing so results in no errors yet nothing happens. The reason for this is that the local file system has no concept of HTTP status codes so the status code is 0 when readyState is 4. Currently our implementation only accepts status code 200 which is insufficient in any case. We will add support for local requests by checking if the script is running locally and that the status code is not set as the test in Listing shows. Listing Making sure the success handler is called for local requests test should call success handler for local requests function 4 0 var success stubFn stubFn true success success assert The test assumes a helper method to check if the script is running locally. Because we are stubbing it a reference to it is saved in the setup allowing it to be restored in tearDown as we did before. Please purchase PDF Split-Merge on to remove this watermark. From the Library of 274 Abstracting Browser Differences Ajax To pass the test we will call the success callback whenever the request is for a local file and the status code is not set. Listing shows the updated ready state change handler. Listing Allow local requests to succeed function requestComplete transport options var status if status 200 status if typeof function transport The implementation passes the test. In order to have this working in a browser as well we need to