2016-04-27 20:06:24 +01:00
|
|
|
import test from 'ava';
|
2016-04-29 03:23:21 +01:00
|
|
|
import {Observable} from 'rx-lite';
|
2016-04-27 20:06:24 +01:00
|
|
|
import randomPuppy from './';
|
|
|
|
|
2016-04-29 03:23:21 +01:00
|
|
|
const imgurRegEx = /^https?:\/\/(\w+\.)?imgur.com\/[a-zA-Z0-9]+(\.[a-zA-Z]{3})?(#[a-zA-Z]*)?$/;
|
2016-04-27 20:06:24 +01:00
|
|
|
|
|
|
|
test('get random', async t => {
|
|
|
|
const result = await randomPuppy();
|
|
|
|
t.regex(result, imgurRegEx);
|
|
|
|
});
|
|
|
|
|
2016-04-28 16:31:41 +01:00
|
|
|
test.cb('use callback', t => {
|
|
|
|
t.plan(2);
|
|
|
|
randomPuppy((err, result) => {
|
|
|
|
t.falsy(err);
|
|
|
|
t.regex(result, imgurRegEx);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test.cb('use callback and different subreddit', t => {
|
|
|
|
t.plan(2);
|
|
|
|
randomPuppy('aww', (err, result) => {
|
|
|
|
t.falsy(err);
|
|
|
|
t.regex(result, imgurRegEx);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-27 20:06:24 +01:00
|
|
|
test('get more random', async t => {
|
|
|
|
const result1 = await randomPuppy();
|
|
|
|
t.regex(result1, imgurRegEx);
|
|
|
|
const result2 = await randomPuppy();
|
|
|
|
t.regex(result2, imgurRegEx);
|
|
|
|
const result3 = await randomPuppy();
|
|
|
|
t.regex(result3, imgurRegEx);
|
|
|
|
const result4 = await randomPuppy();
|
|
|
|
t.regex(result4, imgurRegEx);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('different subreddit', async t => {
|
|
|
|
const result1 = await randomPuppy('aww');
|
|
|
|
t.regex(result1, imgurRegEx);
|
|
|
|
const result2 = await randomPuppy('aww');
|
|
|
|
t.regex(result2, imgurRegEx);
|
|
|
|
const result3 = await randomPuppy('aww');
|
|
|
|
t.regex(result3, imgurRegEx);
|
|
|
|
const result4 = await randomPuppy('aww');
|
|
|
|
t.regex(result4, imgurRegEx);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('invalid subreddit', async t => {
|
|
|
|
const result1 = await randomPuppy('23rkljr2klj3');
|
|
|
|
t.falsy(result1);
|
|
|
|
const result2 = await randomPuppy('');
|
|
|
|
t.regex(result2, imgurRegEx);
|
|
|
|
const result3 = await randomPuppy({});
|
|
|
|
t.regex(result3, imgurRegEx);
|
|
|
|
const result4 = await randomPuppy(false);
|
|
|
|
t.regex(result4, imgurRegEx);
|
|
|
|
});
|
2016-04-29 03:23:21 +01:00
|
|
|
|
|
|
|
test('all', t => {
|
|
|
|
t.plan(10);
|
|
|
|
const puppyEmitter = randomPuppy.all('puppies');
|
|
|
|
const robotEmitter = randomPuppy.all('robots');
|
|
|
|
|
|
|
|
const puppySource = Observable.fromEvent(puppyEmitter, 'data');
|
|
|
|
const robotSource = Observable.fromEvent(robotEmitter, 'data');
|
|
|
|
|
|
|
|
const sharedSource = Observable
|
|
|
|
.merge(puppySource, robotSource)
|
|
|
|
.take(10);
|
|
|
|
|
|
|
|
sharedSource.subscribe(data => {
|
|
|
|
t.regex(data, imgurRegEx);
|
|
|
|
// console.log(data);
|
|
|
|
});
|
|
|
|
return sharedSource.toPromise();
|
|
|
|
});
|