Skip to content

Commit 2ffc338

Browse files
committed
Create a wrapper around createElement('img') so it can be tested
I have no idea how to get sinon to actually stub `document.createElement(‘img’)` and every attempt causes Phantom to stop responding. This was the easiest way out. I’m sorry. This is a test backfilled for #334
1 parent 5db3913 commit 2ffc338

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/raven.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ function send(data) {
717717

718718

719719
function makeRequest(data) {
720-
var img = document.createElement('img'),
720+
var img = newImage(),
721721
src = globalServer + authQueryString + '&sentry_data=' + encodeURIComponent(JSON.stringify(data));
722722

723723
img.crossOrigin = 'anonymous';
@@ -736,6 +736,13 @@ function makeRequest(data) {
736736
img.src = src;
737737
}
738738

739+
// Note: this is shitty, but I can't figure out how to get
740+
// sinon to stub document.createElement without breaking everything
741+
// so this wrapper is just so I can stub it for tests.
742+
function newImage() {
743+
return document.createElement('img');
744+
}
745+
739746
function isSetup() {
740747
if (!hasJSON) return false; // needs JSON support
741748
if (!globalServer) {

test/raven.test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ function flushRavenState() {
2424
Raven.uninstall();
2525
}
2626

27-
var imageCache = [];
28-
window.Image = function Image() {
29-
imageCache.push(this);
30-
};
31-
3227
// window.console must be stubbed in for browsers that don't have it
3328
if (typeof window.console === 'undefined') {
3429
console = {error: function(){}};
@@ -1045,9 +1040,10 @@ describe('globals', function() {
10451040

10461041
describe('makeRequest', function() {
10471042
it('should load an Image', function() {
1048-
imageCache = [];
10491043
authQueryString = '?lol';
10501044
globalServer = 'http://localhost/';
1045+
var imageCache = [];
1046+
this.sinon.stub(window, 'newImage', function(){ var img = {}; imageCache.push(img); return img; });
10511047

10521048
makeRequest({foo: 'bar'});
10531049
assert.equal(imageCache.length, 1);
@@ -1625,7 +1621,9 @@ describe('Raven (public API)', function() {
16251621
});
16261622

16271623
it('should work as advertised #integration', function() {
1628-
imageCache = [];
1624+
var imageCache = [];
1625+
this.sinon.stub(window, 'newImage', function(){ var img = {}; imageCache.push(img); return img; });
1626+
16291627
setupRaven();
16301628
Raven.captureMessage('lol', {foo: 'bar'});
16311629
assert.equal(imageCache.length, 1);

0 commit comments

Comments
 (0)