forked from anacronw/js-fixtures
/
fixtures-spec.js
287 lines (265 loc) · 11.7 KB
/
fixtures-spec.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
describe("fixtures.Fixtures", function(){
var ajaxData = "some ajax data";
var fixtureUrl = "some_url";
var anotherFixtureUrl = "another_url";
var server = sinon.fakeServer.create();
var xhr = sinon.useFakeXMLHttpRequest();
var fixturesBody = function(){
return $('#' + fixtures.containerId).contents().find('body');
};
var appendFixturesContainerToDom = function(){
fixtures.set('old content');
};
var text = 'some text';
var html = '<div>' + text + '</div>';
beforeEach(function(done){
server = sinon.fakeServer.create();
server.respondWith(ajaxData);
server.autoRespondAfter = 1;
setTimeout(done, 0); //Workaround for Mocha bug in IE https://github.com/visionmedia/mocha/issues/502
});
afterEach(function(){
server.restore();
fixtures.cleanUp();
fixtures.clearCache();
});
describe("default initial config values", function(){
it("should set 'js-fixtures' as the default container id", function(){
expect(fixtures.containerId).to.equal('js-fixtures');
});
it("should set 'spec/javascripts/fixtures/' as the default fixtures path", function(){
expect(fixtures.path).to.equal('spec/javascripts/fixtures');
});
it("should set body to null", function(){
expect(fixtures.body()).to.be(null);
});
it("should set window to null", function(){
expect(fixtures.window()).to.be(null);
});
});
describe("body", function(){
it("should not be null when initialized properly", function(){
fixtures.set('test');
expect(fixtures.body()).to.not.be(null);
});
it("should return the body contents of the iframe", function(){
var sillyString = 'some silly string';
fixtures.set(sillyString);
expect(fixtures.body()).to.equal(sillyString);
});
});
describe("window", function(){
it("should return the window object of the iframe", function(){
fixtures.set('test');
expect(fixtures.window()).to.not.equal(window);
});
it("should contain global vars injected into frame", function(){
fixtures.set('<scr' + 'ipt>' + 'var test = "globalVar"' + '</scr' + 'ipt>');
expect(fixtures.window().test).to.equal('globalVar');
});
});
describe("read", function(){
it("should return fixture HTML", function(){
var html = fixtures.read(fixtureUrl);
expect(html).to.equal(ajaxData);
});
it("should return duplicated HTML of a fixture when its url is provided twice in a single call", function(){
var html = fixtures.read(fixtureUrl, fixtureUrl);
expect(html).to.equal(ajaxData + ajaxData);
});
it("should return merged HTML of two fixtures when two different urls are provided in a single call", function(){
var html = fixtures.read(fixtureUrl, anotherFixtureUrl);
expect(html).to.equal(ajaxData + ajaxData);
});
it("should use the configured fixtures path concatenating it to the requested url (without concatenating a slash if it already has an ending one)", function(){
fixtures.path = 'a path ending with slash/';
fixtures.read(fixtureUrl);
expect(server.requests[0].url).to.have.string('a path ending with slash/' + fixtureUrl);
});
it("should use the configured fixtures path concatening it to the requested url (concatenating a slash if it doesn't have an ending one)", function(){
fixtures.path = "a path without an ending slash";
fixtures.read(fixtureUrl);
expect(server.requests[0].url).to.have.string("a path without an ending slash/" + fixtureUrl);
});
});
describe("load", function(){
it("should insert fixture HTML into container", function(){
fixtures.load(fixtureUrl);
expect(fixturesBody().html()).to.equal(ajaxData);
});
it("should insert duplicated fixture HTML into container when the same url is provided twice in a single call", function(){
fixtures.load(fixtureUrl, fixtureUrl);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
it("should insert merged HTML of two fixtures into container when two different urls are provided in a single call", function(){
fixtures.load(fixtureUrl, anotherFixtureUrl);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
describe("when fixture container does not exist", function(){
it("should automatically create fixtures container and append it to the DOM", function(){
fixtures.load(fixtureUrl);
expect(fixturesBody().size()).to.equal(1);
});
});
describe("when fixture container exists", function(){
beforeEach(function(){
appendFixturesContainerToDom();
});
it("should replace it with new content", function(){
fixtures.load(fixtureUrl);
expect(fixtures.body()).to.equal(ajaxData);
});
});
xdescribe("when fixture contains an inline script tag", function(){
beforeEach(function(){
ajaxData = "<script>document.writeln('test')</script>";
server.respondWith(ajaxData);
});
it("should execute the inline javascript after the fixture has been inserted into the body", function(){
fixtures.load(fixtureUrl);
expect(fixturesBody().html()).to.equal('test');
});
});
});
describe("appendLoad", function(){
beforeEach(function(){
ajaxData = 'some ajax data';
server.respondWith(ajaxData);
});
it("should insert fixture HTML into container", function(){
fixtures.appendLoad(fixtureUrl);
expect(fixturesBody().html()).to.equal(ajaxData);
});
it("should insert duplicated fixture html into container when the same url is provided twice in a single call", function(){
fixtures.appendLoad(fixtureUrl, anotherFixtureUrl);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
it("should insert merged HTML of two fixtures into container when two different urls are provided in a single call", function(){
fixtures.appendLoad(fixtureUrl, anotherFixtureUrl);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
it("should automatically create fixtures container and append it to the DOM", function(){
fixtures.appendLoad(fixtureUrl);
expect(fixturesBody().size()).to.equal(1);
});
describe("with a prexisting fixture",function(){
beforeEach(function() {
fixtures.appendLoad(fixtureUrl);
});
it("should add new content", function() {
fixtures.appendLoad(fixtureUrl);
expect(fixturesBody().html()).to.equal(ajaxData + ajaxData);
});
it("should not add a new fixture container", function(){
fixtures.appendLoad(fixtureUrl);
expect(fixturesBody().size()).to.equal(1);
});
});
xdescribe("when fixture contains an inline script tag", function(){
beforeEach(function(){
ajaxData = '<scr' + 'ipt>document.writeln("test");</scr' + 'ipt>';
server.respondWith(ajaxData);
});
it("should execute the inline javascript after the fixture has been inserted into the body", function(){
fixtures.appendLoad(fixtureUrl);
expect($.trim(fixturesBody().html())).to.equal('test');
});
});
});
describe("preload", function() {
describe("read after preload", function() {
it("should go from cache", function() {
fixtures.preload(fixtureUrl, anotherFixtureUrl);
fixtures.read(fixtureUrl, anotherFixtureUrl);
expect(server.requests.length).to.equal(2);
});
it("should return correct HTMLs", function() {
fixtures.preload(fixtureUrl, anotherFixtureUrl);
var html = fixtures.read(fixtureUrl, anotherFixtureUrl);
expect(html).to.equal(ajaxData + ajaxData);
});
});
it("should not preload the same fixture twice", function() {
fixtures.preload(fixtureUrl, fixtureUrl);
expect(server.requests.length).to.equal(1);
});
});
describe("set", function() {
it("should insert HTML into container", function() {
fixtures.set(html);
expect(fixturesBody().html().toLowerCase()).to.equal(html);
});
describe("when fixture container does not exist", function() {
it("should automatically create fixtures container and append it to DOM", function() {
fixtures.set(html);
expect(fixturesBody().size()).to.equal(1);
});
});
describe("when fixture container exists", function() {
beforeEach(function() {
appendFixturesContainerToDom();
});
it("should replace it with new content", function() {
fixtures.set(html);
expect(fixturesBody().html().toLowerCase()).to.equal(html);
});
});
});
describe("appendSet",function(){
it("should insert HTML into container", function() {
fixtures.appendSet(html);
expect(fixturesBody().html().toLowerCase()).to.equal(html);
});
describe("when fixture container does not exist", function() {
it("should automatically create fixtures container and append it to DOM", function() {
fixtures.appendSet(html);
expect(fixturesBody().size()).to.equal(1);
});
});
describe("when fixture container exists", function() {
beforeEach(function() {
fixtures.appendSet(html);
});
it("should add new content", function() {
fixtures.appendSet(html);
expect(fixturesBody().text()).to.equal(text+text);
});
});
});
describe("cleanUp", function() {
it("should remove fixtures container from DOM", function() {
appendFixturesContainerToDom();
fixtures.cleanUp();
expect(fixturesBody().size()).to.equal(0);
});
});
});
describe("fixtures.Fixtures using mock AJAX call", function() {
var defaultFixturesPath;
beforeEach(function() {
defaultFixturesPath = fixtures.path;
fixtures.path = 'spec/fixtures';
});
afterEach(function() {
fixtures.path = defaultFixturesPath;
});
describe("when fixture file exists", function() {
var stub;
beforeEach(function(){
var xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function(xhr){
stub = sinon.stub(xhr, "send", function(something){
xhr.responseText = '<div id="real_non_mocked_fixture"></div>';
});
};
});
afterEach(function(){
stub.restore();
});
it("should load content of fixture file", function() {
var fixtureUrl = "real_non_mocked_fixture.html";
var fixtureContent = fixtures.read(fixtureUrl);
expect(fixtureContent).to.equal('<div id="real_non_mocked_fixture"></div>');
});
});
});