Repository navigation
Expand file tree
/
Copy pathjsfixtures.js
More file actions
155 lines (128 loc) · 3.89 KB
/
Copy pathjsfixtures.js
File metadata and controls
155 lines (128 loc) · 3.89 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var readFixtures = function() {
return jsFixtures.getFixtures().proxyCallTo_('read', arguments);
};
var preloadFixtures = function() {
jsFixtures.getFixtures().proxyCallTo_('preload', arguments);
};
var loadFixtures = function() {
jsFixtures.getFixtures().proxyCallTo_('load', arguments);
};
var setFixtures = function(html) {
jsFixtures.getFixtures().set(html);
};
var sandbox = function(attributes) {
return jsFixtures.getFixtures().sandbox(attributes);
};
jsFixtures = function() {
this.containerId = 'js-fixtures';
this.fixturesCache_ = {};
this.fixturesPath = 'spec/javascripts/fixtures';
};
jsFixtures.getFixtures = function() {
return jsFixtures.currentFixtures_ = jsFixtures.currentFixtures_ || new jsFixtures();
};
jsFixtures.prototype.set = function(html) {
this.cleanUp();
this.createContainer_(html);
};
jsFixtures.prototype.preload = function() {
this.read.apply(this, arguments);
};
jsFixtures.prototype.load = function() {
this.cleanUp();
var args, cb;
if (typeof(arguments[arguments.length - 1]) === 'function') {
cb = arguments[arguments.length - 1];
args = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
}
else {
args = Array.prototype.slice.call(arguments);
}
var This = this;
args.push(function (data) {
This.createContainer_(data);
if (cb) cb(data);
});
this.read.apply(this, args);
};
jsFixtures.prototype.read = function() {
var htmlChunks = [];
var asyncRead = false;
var fixtureUrls = arguments;
var cb;
var fetched = 0;
var urlCount;
var result;
if (typeof(arguments[arguments.length - 1]) === 'function') {
asyncRead = true;
cb = arguments[arguments.length - 1];
fixtureUrls = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
}
urlCount = fixtureUrls.length;
var fetchDone = function(data) {
htmlChunks.push(data);
if (++fetched == urlCount) {
result = htmlChunks.join('')
if (cb) return cb(result);
}
}
for(var urlIndex = 0; urlIndex < urlCount; urlIndex++) {
this.getFixtureHtml_(fixtureUrls[urlIndex], fetchDone, asyncRead);
}
return result;
};
jsFixtures.prototype.clearCache = function() {
this.fixturesCache_ = {};
};
jsFixtures.prototype.cleanUp = function() {
$('#' + this.containerId).remove();
};
jsFixtures.prototype.sandbox = function(attributes) {
var attributesToSet = attributes || {};
return $('<div id="sandbox" />').attr(attributesToSet);
};
jsFixtures.prototype.createContainer_ = function(html) {
var container;
if(html instanceof $.fn.constructor) {
container = $('<div id="' + this.containerId + '" />');
container.html(html);
} else {
container = '<div id="' + this.containerId + '">' + html + '</div>';
}
$('body').append(container);
};
jsFixtures.prototype.getFixtureHtml_ = function(url, cb, asyncRead) {
if (typeof this.fixturesCache_[url] == 'undefined') {
this.loadFixtureIntoCache_(url, cb, asyncRead);
} else {
cb(this.fixturesCache_[url]);
}
};
jsFixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl, cb, asyncRead) {
var self = this;
var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl;
$.ajax({
async: asyncRead,
cache: false,
dataType: 'html',
url: url,
success: function(data) {
self.fixturesCache_[relativeUrl] = data;
cb(data);
},
error: function(jqXHR, status, errorThrown) {
throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + (errorThrown ? errorThrown.message : "") + ')');
}
});
};
jsFixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
return this[methodName].apply(this, passedArguments);
};
if (typeof(afterEach) !== "undefined") {
afterEach(function() {
jsFixtures.getFixtures().cleanUp();
});
}
if (typeof exports !== "undefined") {
module.exports = jsFixtures;
}