/
jasmine-fixture-loader.js
65 lines (54 loc) · 2.02 KB
/
jasmine-fixture-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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) + 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++;
};
// 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.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);
}
return spec.cachedFixtures[fixtureName];
};
spec.retrieveFixture = function(fixtureName) {
// construct a path to the fixture, including a cache-busting timestamp
var path = 'tmp/jasmine_fixtures/' + fixtureName + ".jasmine_fixture?" + 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);
}
if (xhr.status < 200 || xhr.status > 299) {
throw new Error("Couldn't load fixture with key: '" + fixtureName + "'. No such file: '" + path + "'.");
}
return xhr.responseText;
};
spec.loadFixtureCount = 0;
spec.cachedFixtures = {};