whatsthis
Version:
Discover embedable content from URL's
295 lines (228 loc) • 8.64 kB
JavaScript
describe('whatsthis', function () {
'use strict';
var assume = require('assume')
, What = require('./')
, what;
this.timeout(10000);
beforeEach(function () {
what = new What();
//
// The following plugins are disabled by default but we still want to test
// them.
//
what.use('twitch', require('./services/twitch'));
});
afterEach(function () {
what.destroy();
});
it('extracts links out of text', function (next) {
what.parse('this is a regular chat message with http://google.com google link', function (err, result) {
if (err) return next(err);
assume(result.match).to.equal('http://google.com');
next();
});
});
it('uses the last found url', function (next) {
what.parse('http://google.com http://youtube.com http://pewpew.com', function (err, result) {
if (err) return next(err);
assume(result.match).to.equal('http://pewpew.com');
next();
});
});
it('also works on no url', function (next) {
what.parse('all of text inbound', function (err, result) {
if (err) return next(err);
assume(result.match).is.undefined();
assume(result.type).equals('text');
assume(result.service).equals('unknown');
next();
});
});
describe('twitter', function () {
it('parses twitter.com as regular url', function (next) {
what.parse('http://twitter.com', function (err, result) {
if (err) return next();
assume(result.type).equals('link');
assume(result.html).equals('');
assume(result.service).equals('unknown');
next();
});
});
it('generates an embed code for tweets', function (next) {
what.parse('https://twitter.com/_mantis_/status/705816843563327488', function (err, result) {
if (err) return next(err);
assume(result.type).equals('social');
assume(result.service).equals('twitter');
assume(result.html).includes('blockquote');
assume(result.html).includes('THE BURNING SHRINE');
next();
});
});
it('embeds mobile urls', function (next) {
what.parse('https://mobile.twitter.com/Android/status/672464631835713536', function (err, result) {
if (err) return next();
assume(result.type).equals('social');
assume(result.service).equals('twitter');
assume(result.html).includes('blockquote');
next();
});
});
});
describe('instagram', function () {
it('parses instagram.com as regular url', function (next) {
what.parse('http://instagram.com', function (err, result) {
if (err) return next();
assume(result.type).equals('link');
assume(result.html).equals('');
assume(result.service).equals('unknown');
next();
});
});
it('generates an embed code for instagrams', function (next) {
what.parse('https://www.instagram.com/p/BJVX6jQBkx0/', function (err, result) {
if (err) return next(err);
assume(result.type).equals('social');
assume(result.service).equals('instagram');
assume(result.html).includes('Iron Banner');
next();
});
});
it('embeds shorturls urls', function (next) {
what.parse('http://instagr.am/p/fA9uwTtkSN/', function (err, result) {
if (err) return next();
assume(result.type).equals('social');
assume(result.service).equals('instagram');
assume(result.html).includes('blockquote');
next();
});
});
});
describe('twitch', function () {
it('parses twitch.tv as regular url', function (next) {
what.parse('http://twitch.tv', function (err, result) {
if (err) return next();
assume(result.type).equals('link');
assume(result.html).equals('');
assume(result.service).equals('unknown');
next();
});
});
it('generates an embed code for clips', function (next) {
what.parse('https://clips.twitch.tv/breezypeazy/LongRavenFrankerZ', function (err, result) {
if (err) return next(err);
assume(result.type).equals('video');
assume(result.service).equals('twitch');
assume(result.html).includes('clips.twitch.tv/embed');
assume(result.html).includes('iframe');
assume(result.html).includes('autoplay=false');
next();
});
});
});
describe('images', function () {
[
'https://i.imgur.com/aKaOqIh.gif',
'https://upload.wikimedia.org/wikipedia/en/a/aa/Rickroll%27D.jpg'
].forEach(function (url) {
it('understands: '+ url, function (next) {
what.parse(url, function (err, result) {
if (err) return next(err);
assume(result.service).equals('image');
assume(result.type).equals('image');
assume(result.html).includes('img src');
next();
});
});
});
it('transforms imgur gifv to gif', function (next) {
what.parse('https://i.imgur.com/aKaOqIh.gifv', function (err, result) {
if (err) return next(err);
assume(result.service).equals('image');
assume(result.type).equals('image');
assume(result.html).includes('img src="https://i.imgur.com/aKaOqIh.gif"');
next();
});
});
[
'https://i.imgur.com/aKaOqIh.gif lol',
'hello https://upload.wikimedia.org/wikipedia/en/a/aa/Rickroll%27D.jpg'
].forEach(function (url) {
it('understands: '+ url +' with text', function (next) {
what.parse(url, function (err, result) {
if (err) return next(err);
assume(result.service).equals('image');
assume(result.type).equals('image');
assume(result.html).includes('img src');
next();
});
});
});
});
describe('youtube', function () {
it('unshortens short urls', function (next) {
what.parse('http://youtu.be/dQw4w9WgXcQ', function (err, result) {
if (err) return next(err);
assume(result.url.href).equals('https://youtube.com/embed/dQw4w9WgXcQ');
assume(result.html).includes('iframe src');
assume(result.service).equals('youtube');
assume(result.type).equals('video');
next();
});
});
it('understands youtube watch urls', function (next) {
what.parse('https://www.youtube.com/watch?v=JxFcn0y2p4o', function (err, result) {
if (err) return next(err);
assume(result.html).includes('iframe src');
assume(result.service).equals('youtube');
assume(result.type).equals('video');
assume(result.html).includes('JxFcn0y2p4o');
next();
});
});
it('understands normal youtube urls', function (next) {
what.parse('http://youtube.com/dQw4w9WgXcQ', function (err, result) {
if (err) return next(err);
assume(result.url.href).equals('https://youtube.com/embed/dQw4w9WgXcQ');
assume(result.html).includes('iframe src');
assume(result.service).equals('youtube');
assume(result.type).equals('video');
next();
});
});
});
describe('xboxdvr', function () {
it('transforms to HTTPS', function (next) {
what.parse('http://xboxdvr.com/gamer/Fear%20Broner/screenshot/3988422', function (err, result) {
if (err) return next(err);
assume(result.url.protocol).includes('https');
next();
});
});
it('correctly embeds screenshots', function (next) {
what.parse('http://xboxdvr.com/gamer/Fear%20Broner/screenshot/3988422', function (err, result) {
if (err) return next(err);
assume(result.service).equals('xboxdvr');
assume(result.type).equals('image');
assume(result.url.href).includes('embed');
assume(result.url.href).includes('screenshot');
assume(result.html).includes('iframe src');
assume(result.html).includes('embed');
assume(result.html).includes('screenshot');
next();
});
});
it('correctly embed videos', function (next) {
what.parse('http://xboxdvr.com/gamer/Fear%20Broner/video/15816075', function (err, result) {
if (err) return next(err);
assume(result.service).equals('xboxdvr');
assume(result.type).equals('video');
assume(result.url.href).includes('embed');;
assume(result.url.href).includes('video');
assume(result.html).includes('iframe src');
assume(result.html).includes('embed');
assume(result.html).includes('video');
next();
});
});
});
});