Skip to content

Instantly share code, notes, and snippets.

@ravasthi
Forked from mavenlink/loadFixture.js
Last active January 3, 2016 14:49
Show Gist options
  • Select an option

  • Save ravasthi/8478820 to your computer and use it in GitHub Desktop.

Select an option

Save ravasthi/8478820 to your computer and use it in GitHub Desktop.

Revisions

  1. ravasthi revised this gist Jan 17, 2014. 1 changed file with 33 additions and 14 deletions.
    47 changes: 33 additions & 14 deletions loadFixture.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,21 @@
    var uniqueLoadIndicator = null;
    // Loads fixure markup into the DOM as a child of the jasmine_content div
    spec.loadFixture = function(fixtureName) {
    var $destination = $('#jasmine_content');


    uniqueLoadIndicator = null;
    var indicatorScript = "<script>uniqueLoadIndicator = 'loaded';</s" + "cript>";

    // get the markup, inject it into the dom
    $destination.html(spec.fixtureHtml(fixtureName));

    // keep track of fixture count to fail specs that
    $destination.html(spec.fixtureHtml(fixtureName) + indicatorScript);
    while (uniqueLoadIndicator != "loaded") {
    //
    if (console) console.log("Browser wasn't ready... sleeping.");
    spec.retrieveFixture(fixtureName);
    }
    uniqueLoadIndicator = null;

    // keep track of fixture count to fail specs that
    // call loadFixture() more than once
    spec.loadFixtureCount++;
    };
    @@ -16,6 +26,13 @@ spec.readFixture = function(fixtureName) {
    return spec.fixtureHtml(fixtureName);
    };

    spec.readJSON = function(fixtureName) {
    var data = spec.fixtureHtml(fixtureName);
    return window.JSON && window.JSON.parse ?
    window.JSON.parse( data ) :
    (new Function("return " + data))();
    };

    spec.fixtureHtml = function(fixtureName) {
    if (!spec.cachedFixtures[fixtureName]) {
    spec.cachedFixtures[fixtureName] = spec.retrieveFixture(fixtureName);
    @@ -26,23 +43,25 @@ spec.fixtureHtml = function(fixtureName) {
    spec.retrieveFixture = function(fixtureName) {

    // construct a path to the fixture, including a cache-busting timestamp
    var path = '/tmp/js_dom_fixtures/' + fixtureName + ".fixture.html.erb?" + new Date().getTime();
    var xhr;
    var path = '/spec/javascripts/fixtures/' + fixtureName + "?" + new Date().getTime();
    var responseText;

    // retrieve the fixture markup via xhr request to jasmine server
    try {
    xhr = new jasmine.XmlHttpRequest();
    xhr.open("GET", path, false);
    xhr.send(null);
    $.ajax(path, {
    dataType: "html",
    complete: function(xhr, status) {
    responseText = xhr.responseText;
    },
    error: function(xhr, status, error) {
    throw new Error("Couldn't load fixture with key: '" + fixtureName + "'. No such file: '" + path + "'.");
    }
    });
    } catch(e) {
    throw new Error("couldn't fetch " + path + ": " + e);
    }
    var regExp = new RegExp(/Couldn\\\'t load \/fixture/);
    if (regExp.test(xhr.responseText)) {
    throw new Error("Couldn't load fixture with key: '" + fixtureName + "'. No such file: '" + path + "'.");
    }

    return xhr.responseText;
    return responseText;
    };

    spec.loadFixtureCount = 0;
  2. mavenlink created this gist Feb 5, 2010.
    49 changes: 49 additions & 0 deletions loadFixture.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    // Loads fixure markup into the DOM as a child of the jasmine_content div
    spec.loadFixture = function(fixtureName) {
    var $destination = $('#jasmine_content');

    // get the markup, inject it into the dom
    $destination.html(spec.fixtureHtml(fixtureName));

    // keep track of fixture count to fail specs that
    // call loadFixture() more than once
    spec.loadFixtureCount++;
    };

    // Returns fixture markup as a string. Useful for fixtures that
    // represent the response text of ajax requests.
    spec.readFixture = function(fixtureName) {
    return spec.fixtureHtml(fixtureName);
    };

    spec.fixtureHtml = function(fixtureName) {
    if (!spec.cachedFixtures[fixtureName]) {
    spec.cachedFixtures[fixtureName] = spec.retrieveFixture(fixtureName);
    }
    return spec.cachedFixtures[fixtureName];
    };

    spec.retrieveFixture = function(fixtureName) {

    // construct a path to the fixture, including a cache-busting timestamp
    var path = '/tmp/js_dom_fixtures/' + fixtureName + ".fixture.html.erb?" + new Date().getTime();
    var xhr;

    // retrieve the fixture markup via xhr request to jasmine server
    try {
    xhr = new jasmine.XmlHttpRequest();
    xhr.open("GET", path, false);
    xhr.send(null);
    } catch(e) {
    throw new Error("couldn't fetch " + path + ": " + e);
    }
    var regExp = new RegExp(/Couldn\\\'t load \/fixture/);
    if (regExp.test(xhr.responseText)) {
    throw new Error("Couldn't load fixture with key: '" + fixtureName + "'. No such file: '" + path + "'.");
    }

    return xhr.responseText;
    };

    spec.loadFixtureCount = 0;
    spec.cachedFixtures = {};