Skip to content

Commit 043ca37

Browse files
committed
Refactor, merge tests
Historically those tests where shared across two implementations of a plugin system
1 parent 6f5f828 commit 043ca37

File tree

5 files changed

+253
-264
lines changed

5 files changed

+253
-264
lines changed

test/.jshintrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"imagediff": true,
1717
"imagediffForJasmine2": true,
1818
"csscriticLib": true,
19-
"csscriticTestPath": true,
2019
"csscritic": true,
2120
"testHelper": true
2221
}

test/SpecRunner.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
<script src="../csscritic.js"></script>
2121

2222
<!-- include spec files here... -->
23-
<script src="specs/shared/storagePluginSpecs.js"></script>
24-
2523
<script src="specs/regressionSpec.js"></script>
2624
<script src="specs/reportingSpec.js"></script>
2725
<script src="specs/utilSpec.js"></script>

test/specs/.jshintrc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
"imagediff": true,
1818
"imagediffForJasmine2": true,
1919
"csscriticLib": true,
20-
"csscriticTestPath": true,
2120
"testHelper": true,
22-
"loadStoragePluginSpecs": true,
23-
"CanvasRenderingContext2D": true,
2421
"ayepromise": true,
2522
"Promise": true,
2623
"indexedDB": true
2724
},
28-
"exported": ["loadStoragePluginSpecs"]
25+
"exported": []
2926
}

test/specs/indexedDbStorageSpec.js

Lines changed: 252 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("IndexedDB storage", function () {
4545
});
4646
};
4747

48-
var storeReferenceImage = function (key, stringData) {
48+
var storeMockReferenceImage = function (key, stringData) {
4949
// TODO move away from JSON encoded test input, doesn't match internals of this module
5050
var data = JSON.parse(stringData),
5151
dataObj = {};
@@ -84,11 +84,257 @@ describe("IndexedDB storage", function () {
8484
};
8585
});
8686

87-
loadStoragePluginSpecs(
88-
constructStorage,
89-
readStoredReferenceImage,
90-
storeReferenceImage
91-
);
87+
var imgUri =
88+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAKUlEQVQ4jWNYt27df2Lwo0ePiMIMowaOGjgsDSRWIbEWjxo4auCwNBAAenk4PB4atggAAAAASUVORK5CYII=",
89+
img = null,
90+
storage;
91+
92+
var util = csscriticLib.util();
93+
94+
var setUpImageReturnedForUrl = function (image) {
95+
util.getImageForUrl.and.returnValue(Promise.resolve(image));
96+
};
97+
98+
var setUpImageForUrlToFail = function (e) {
99+
util.getImageForUrl.and.returnValue(testHelper.failedPromise(e));
100+
};
101+
102+
beforeEach(function (done) {
103+
spyOn(util, "getImageForUrl");
104+
105+
storage = constructStorage(util);
106+
107+
jasmine.addMatchers(imagediffForJasmine2);
108+
109+
testHelper.loadImageFromUrl(imgUri, function (image) {
110+
img = image;
111+
112+
done();
113+
});
114+
});
115+
116+
it("should store a the rendered page", function (done) {
117+
var value;
118+
119+
storage
120+
.storeReferenceImage({ url: "somePage.html" }, img, {
121+
width: 47,
122+
height: 11,
123+
})
124+
.then(function () {
125+
readStoredReferenceImage("somePage.html").then(function (
126+
stringValue
127+
) {
128+
expect(stringValue).not.toBeNull();
129+
130+
value = JSON.parse(stringValue);
131+
testHelper.loadImageFromUrl(
132+
value.referenceImageUri,
133+
function (image) {
134+
expect(image).toImageDiffEqual(img);
135+
136+
done();
137+
}
138+
);
139+
});
140+
});
141+
});
142+
143+
it("should store the viewport's size", function (done) {
144+
var image = "the image",
145+
storedValue;
146+
147+
spyOn(util, "getDataURIForImage");
148+
149+
storage
150+
.storeReferenceImage({ url: "somePage.html" }, image, {
151+
width: 47,
152+
height: 11,
153+
})
154+
.then(function () {
155+
readStoredReferenceImage("somePage.html").then(function (
156+
stringValue
157+
) {
158+
storedValue = JSON.parse(stringValue);
159+
160+
expect(storedValue.viewport.width).toEqual(47);
161+
expect(storedValue.viewport.height).toEqual(11);
162+
163+
done();
164+
});
165+
});
166+
});
167+
168+
it("should honour test case parameters when storing", function (done) {
169+
storage
170+
.storeReferenceImage(
171+
{
172+
url: "somePage.html",
173+
hover: "aValue",
174+
active: "anotherValue",
175+
},
176+
img,
177+
{}
178+
)
179+
.then(function () {
180+
readStoredReferenceImage(
181+
"somePage.html,active=anotherValue,hover=aValue"
182+
).then(function (stringValue) {
183+
expect(stringValue).not.toBeNull();
184+
185+
done();
186+
});
187+
});
188+
});
189+
190+
it("should read in a reference image", function (done) {
191+
setUpImageReturnedForUrl(img);
192+
193+
storeMockReferenceImage(
194+
"somePage.html",
195+
JSON.stringify({
196+
referenceImageUri: imgUri,
197+
})
198+
).then(function () {
199+
storage
200+
.readReferenceImage({ url: "somePage.html" })
201+
.then(function (result) {
202+
expect(util.getImageForUrl).toHaveBeenCalledWith(
203+
imgUri
204+
);
205+
expect(result.image).toBe(img);
206+
207+
done();
208+
});
209+
});
210+
});
211+
212+
it("should return the viewport's size with viewport", function (done) {
213+
setUpImageReturnedForUrl(img);
214+
215+
storeMockReferenceImage(
216+
"somePage.html",
217+
JSON.stringify({
218+
referenceImageUri: imgUri,
219+
viewport: {
220+
width: 19,
221+
height: 84,
222+
},
223+
})
224+
).then(function () {
225+
storage
226+
.readReferenceImage({ url: "somePage.html" })
227+
.then(function (result) {
228+
expect(result.viewport.width).toEqual(19);
229+
expect(result.viewport.height).toEqual(84);
230+
231+
done();
232+
});
233+
});
234+
});
235+
236+
it("should return the viewport's size and fallback to the image's size", function (done) {
237+
img.height = 34;
238+
img.width = 12;
239+
setUpImageReturnedForUrl(img);
240+
241+
storeMockReferenceImage(
242+
"somePage.html",
243+
JSON.stringify({
244+
referenceImageUri: imgUri,
245+
})
246+
);
247+
248+
storage
249+
.readReferenceImage({ url: "somePage.html" })
250+
.then(function (result) {
251+
expect(result.viewport.width).toEqual(12);
252+
expect(result.viewport.height).toEqual(34);
253+
254+
done();
255+
});
256+
});
257+
258+
it("should call error handler if no reference image has been stored", function (done) {
259+
storage
260+
.readReferenceImage({ url: "somePage.html" })
261+
.then(null, done);
262+
});
263+
264+
it("should call error handler if the image is missing", function (done) {
265+
storeMockReferenceImage("somePage.html", JSON.stringify({}));
266+
267+
storage
268+
.readReferenceImage({ url: "somePage.html" })
269+
.then(null, done);
270+
});
271+
272+
it("should call error handler if read reference image is invalid", function (done) {
273+
setUpImageForUrlToFail();
274+
275+
storeMockReferenceImage(
276+
"somePage.html",
277+
JSON.stringify({
278+
referenceImageUri: "broken uri",
279+
})
280+
);
281+
storage
282+
.readReferenceImage({ url: "somePage.html" })
283+
.then(null, done);
284+
});
285+
286+
it("should honour test case parameters when reading", function (done) {
287+
setUpImageReturnedForUrl(img);
288+
289+
storeMockReferenceImage(
290+
"somePage.html,active=anotherValue,hover=aValue",
291+
JSON.stringify({
292+
referenceImageUri: imgUri,
293+
})
294+
);
295+
296+
storage
297+
.readReferenceImage({
298+
url: "somePage.html",
299+
hover: "aValue",
300+
active: "anotherValue",
301+
})
302+
.then(function (img) {
303+
expect(img).not.toBeNull();
304+
305+
done();
306+
});
307+
});
308+
309+
it("should find the matching test case for multiple tests of the same url", function (done) {
310+
setUpImageReturnedForUrl(img);
311+
312+
storeMockReferenceImage(
313+
"somePage.html",
314+
JSON.stringify({
315+
referenceImageUri: "some image uri",
316+
})
317+
);
318+
storeMockReferenceImage(
319+
"somePage.html,width=42",
320+
JSON.stringify({
321+
referenceImageUri: "some image uri matching the width",
322+
})
323+
);
324+
325+
storage
326+
.readReferenceImage({
327+
url: "somePage.html",
328+
width: 42,
329+
})
330+
.then(function () {
331+
expect(util.getImageForUrl).toHaveBeenCalledWith(
332+
"some image uri matching the width"
333+
);
334+
335+
done();
336+
});
337+
});
92338
});
93339

94340
it("should initally create a database", function (done) {

0 commit comments

Comments
 (0)