Skip to content
This repository has been archived by the owner on May 27, 2018. It is now read-only.

loadFixtures does not wait for async call #3

Closed
levous opened this issue Dec 5, 2011 · 5 comments
Closed

loadFixtures does not wait for async call #3

levous opened this issue Dec 5, 2011 · 5 comments

Comments

@levous
Copy link

levous commented Dec 5, 2011

I've tried Safari and Firefox

I'm using loadFixtures to load html for a jasmine spec.

Here is my spec

describe("example", function(){
    it("has text when set using text method", function(){

        loadFixtures("spec/fixtures/example.html");

        var node = document.getElementById('validationDiv');
        expect(node).toExist();
        expect(document.getElementById("validationDiv")).toBeDefined();
        expect(document.getElementById("validationDiv")).toExist();

        document.getElementById("validationDiv").innerHTML = "hi there";
        expect(document.getElementById("validationDiv").innerHTML).toEqual("hi there");
    });
}); 

Here is my fixture

<div id="my-example-fixture">
    <input type="text" id="text1"></input>
    <input type="text" id="text2"></input>
    <div id="validationDiv"></div>
</div>

In debugging the code, the loadFixtures method is returning to the test caller prior to the callback being handled in

 _loadFixtureIntoCache: function(url)
  {
    var self= this;
    var xhr= new jasmine.Fixtures.XHR();
    xhr.open('GET', url, true);

    xhr.onreadystatechange= function()
    {
      if (4!==xhr.readyState)
        return;
      var status= xhr.status;  // <<<<  This line is hit after the test has already failed
      var succeeded= 0===status || (status>=200 && status<300) || 304==status;

      if (!succeeded)
        throw new Error('Failed to load resource: status=' + status + ' url=' + url);

      self._fixturesCache[url]= xhr.responseText;   // <<<< The expected html is returned as responseText 
      xhr.onreadystatechange= null;
      xhr= null;
    }
    xhr.send(null);
  },

@jeffwatkins
Copy link
Owner

Are you using a file URL? Or are your tests running off a Web server?

@levous
Copy link
Author

levous commented Dec 5, 2011

I'm using the jasmine gem in a non rails, static file environment
When I use the jasmine-jquery loadFixtures, it loads prior to the tests being executed

(but I'd rather use yours as I don't want to introduce query into my tests)
:)

@jeffwatkins
Copy link
Owner

The code actually uses the following:

_loadFixtureIntoCache: function(url)
  {
    var self= this;
    var xhr= new jasmine.Fixtures.XHR();
    xhr.open('GET', url, false);
    xhr.send(null);
    var status= xhr.status;
    var succeeded= 0===status || (status>=200 && status<300) || 304==status;

    if (!succeeded)
      throw new Error('Failed to load resource: status=' + status + ' url=' + url);
    this._fixturesCache[url]= xhr.responseText;
  },

You've changed the async parameter from false to true. There is no way for the method to block until the XHR request is complete in async mode.

@levous
Copy link
Author

levous commented Feb 6, 2012

You wrote:

You've changed the async parameter from false to true. There is no way for the method to block until the XHR request is complete in async mode.

But I did not change the code at all. I simply wrote the spec and found the code that was running first. I am not sure why I had this version of code. I'll try the latest and see if that works correctly. Thanks very much for the response.

@jeffwatkins
Copy link
Owner

Actually, it turns out I'm a complete idiot: in an early version I had the flag backwards (making the call async instead of sync). You probably just need to pull to get the latest version.

Sorry about that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants