Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ describe("Asynchronous tests:", function() {
expect(playButton).toBeDefined();
}
done();
}).catch(function(err) {
done(err);
});
});

Expand Down Expand Up @@ -159,11 +161,13 @@ describe("Asynchronous tests:", function() {

obj.initPromise.then(function() {
gfyDataTest(gfyRootElement, initData);
newGfyObject.init(newData).then(function() {
gfyRootElement = newGfyObject.getRootElement();
gfyDataTest(gfyRootElement, newData);
done();
});
return newGfyObject.init(newData);
}).then(function() {
gfyRootElement = newGfyObject.getRootElement();
gfyDataTest(gfyRootElement, newData);
done();
}).catch(function(err) {
done(err);
});
}

Expand Down Expand Up @@ -235,6 +239,8 @@ describe("Asynchronous tests:", function() {
var videoElem = gfyRootElement.getElementsByTagName('video')[0];
if (videoElem) expect(videoElem.autoplay).toBeFalsy();
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -254,6 +260,8 @@ describe("Asynchronous tests:", function() {
expect(gfyRootElement.getElementsByClassName('gfyCtrlFaster')[0]).toBeDefined();
expect(gfyRootElement.getElementsByClassName('play-button')[0]).toBeUndefined();
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -274,6 +282,8 @@ describe("Asynchronous tests:", function() {
}
expect(gfyRootElement.getElementsByClassName('overlay')[0]).toBeDefined();
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -288,6 +298,8 @@ describe("Asynchronous tests:", function() {
obj.initPromise.then(function() {
expect(gfyRootElement.getElementsByClassName('title')[0]).toBeUndefined();
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -306,6 +318,8 @@ describe("Asynchronous tests:", function() {
expect(gfyRootElement.getElementsByClassName('controls')[0]).toBeUndefined();
expect(gfyRootElement.getElementsByClassName('play-button')[0]).toBeUndefined();
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -331,6 +345,8 @@ describe("Asynchronous tests:", function() {
expect(videoSrc.src.indexOf('mobile')).toBeGreaterThan(-1);
done();
}
}).catch(function(err) {
done(err);
});
});

Expand All @@ -357,6 +373,8 @@ describe("Asynchronous tests:", function() {
expect(videoSrc.src.indexOf('mobile')).toBeGreaterThan(-1);
done();
}
}).catch(function(err) {
done(err);
});
});

Expand All @@ -372,6 +390,8 @@ describe("Asynchronous tests:", function() {
var videoElem = gfyRootElement.getElementsByTagName('video')[0];
expect(videoElem.playbackRate).toEqual(3);
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -387,6 +407,8 @@ describe("Asynchronous tests:", function() {
var videoElem = gfyRootElement.getElementsByTagName('video')[0];
expect(videoElem.playbackRate).toEqual(8);
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -402,6 +424,8 @@ describe("Asynchronous tests:", function() {
var videoElem = gfyRootElement.getElementsByTagName('video')[0];
expect(videoElem.playbackRate).toEqual(0.125);
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -423,6 +447,8 @@ describe("Asynchronous tests:", function() {
expect(gfyCtrlSlower.style.backgroundPosition).toEqual("0px 0px");
expect(gfyCtrlFaster.style.backgroundPosition).toEqual("-192px 0px");
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -446,6 +472,8 @@ describe("Asynchronous tests:", function() {
expect(gfyCtrlFaster.style.backgroundPosition).toEqual("-20px 0px");
done();
}, 100);
}).catch(function(err) {
done(err);
});
});

Expand All @@ -456,6 +484,8 @@ describe("Asynchronous tests:", function() {
obj.initPromise.then(function() {
expect(gfyRootElement.className).toEqual("testclass1");
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -466,6 +496,8 @@ describe("Asynchronous tests:", function() {
obj.initPromise.then(function() {
expect(gfyRootElement.className).toEqual("testclass1 testclass2");
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -476,6 +508,8 @@ describe("Asynchronous tests:", function() {
obj.initPromise.then(function() {
expect(gfyRootElement.className).toEqual("testclass1");
done();
}).catch(function(err) {
done(err);
});
});

Expand Down Expand Up @@ -541,6 +575,8 @@ describe("Asynchronous tests:", function() {
obj.initPromise.then(function() {
expect(gfyRootElement.style.maxWidth).not.toEqual('');
done();
}).catch(function(err) {
done(err);
});
});

Expand All @@ -556,6 +592,8 @@ describe("Asynchronous tests:", function() {
obj.initPromise.then(function() {
expect(gfyRootElement.style.maxWidth).toEqual('');
done();
}).catch(function(err) {
done(err);
});
});
});
Expand All @@ -581,5 +619,7 @@ function responsiveTest(data, done) {
expect(sizer).toBeDefined();
}
done();
}).catch(function(err) {
done(err);
});
}
57 changes: 29 additions & 28 deletions web/js/gfyObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,13 @@ var gfyObject = function (gfyElem, classname) {
sendAnalytics(gfyId);

// call gfycat API to get info for this gfycat
loadJSONP("https://gfycat.com/cajax/get/" + gfyId, function (data) {
if (data) {
fetchData("https://api.gfycat.com/v1/gfycats/" + gfyId, function (data) {
if (data && data.gfyItem) {
gfyItem = data.gfyItem;
createGfyContent();
resolve();
} else {
reject();
reject(data && data.message || data);
}
});
} else {
Expand Down Expand Up @@ -526,33 +526,34 @@ var gfyObject = function (gfyElem, classname) {
opt[optName] = bool[nonDefaultValue];
}
}
/**
* used to load ajax info for each gfycat on the page
* callback functions must be setup and uniquely named for each
*
* @type {(<T = typeof window>(url: string, callback: (this: T, data: any) => void, context?: T) => void)}
*
* @template T
* @param {string} url
* @param {(this: T, data: any) => void} callback
* @param {T} [context]
*/
function fetchData(url, callback, context) {
var request = new XMLHttpRequest();

// INIT
request.open('GET', url);

// Setup handler
request.onreadystatechange = function (event) {
if (this.readyState !== XMLHttpRequest.DONE) {
return;
}

// used to load ajax info for each gfycat on the page
// callback functions must be setup and uniquely named for each
function loadJSONP(url, callback, context) {
var unique = Math.floor((Math.random()*10000000) + 1);
// INIT
var name = "_" + gfyId + "_" + unique++;
if (url.match(/\?/)) url += "&callback=" + name;
else url += "?callback=" + name;

// Create script
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;

// Setup handler
window[name] = function (data) {
callback.call((context || window), data);
document.getElementsByTagName('head')[0].removeChild(script);
script = null;
try {
delete window[name];
} catch (e) {}
};
callback.call((context || window), this.response);
};

// Load JSON
document.getElementsByTagName('head')[0].appendChild(script);
// Load JSON
request.send();
}

function checkScrollVideo() {
Expand Down