describe("Svg to Image", function () {
"use strict";
var createElementFrom = function (htmlString) {
var div = document.createElement("div");
div.innerHTML = htmlString;
return div.childNodes[0];
};
describe("on SVG rendering", function () {
beforeEach(function () {
jasmine.addMatchers(imagediff.jasmine);
});
it("should render the SVG", function (done) {
var referenceImg = createElementFrom(
'',
),
twoColorSvg =
'";
referenceImg.onload = function () {
svg2image
.renderSvg(twoColorSvg)
.then(function (image) {
expect(image).toImageDiffEqual(referenceImg);
})
.catch(function (err) {
expect(err).toBe(null);
fail();
})
.finally(done);
};
});
it("should render an SVG with inline image", function (done) {
var referenceImg = createElementFrom(
'',
),
twoColorSvg =
'";
referenceImg.onload = function () {
svg2image
.renderSvg(twoColorSvg)
.then(function (image) {
// This fails in Safari, possibly due to a bug with same origin policy stuff
try {
expect(image).toImageDiffEqual(referenceImg);
} catch (err) {
expect(err).toBeNull();
}
})
.catch(function (err) {
expect(err).toBe(null);
fail();
})
.finally(done);
};
});
it("should return an error when the SVG cannot be rendered", function (done) {
var OldImage = window.Image,
imageSpy;
// We need to mock, as only Chrome & Safari seem to throw errors on a faulty SVG
spyOn(window, "Image").and.callFake(function () {
// HACK only spy on the first call
if (!imageSpy) {
imageSpy = {};
return imageSpy;
}
return new OldImage();
});
svg2image.renderSvg("svg").then(null, done);
imageSpy.onerror();
expect(true).toBe(true); // work around warning from jasmine that no expectation is given
});
it("should return an image without event listeners attached", function (done) {
var anSvg =
'';
svg2image
.renderSvg(anSvg)
.then(function (image) {
expect(image.onerror).toBeNull();
expect(image.onload).toBeNull();
})
.catch(function (err) {
expect(err).toBe(null);
fail();
})
.finally(done);
});
});
});