-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
63 lines (49 loc) · 1.36 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import test from 'ava';
import getTitleAtUrl from './index.js';
global.Promise = Promise;
const validUrl = 'http://www.google.com';
const difficultUrl = 'https://www.yahoo.com';
const fourOhFourUrl = 'http://www.google.com/aa';
test('support help shortcut', async t => {
const resultCallback = (title, error, response, body) => {
if (error) {
t.log(error);
t.fail(`error: ${error}, response: ${JSON.stringify(response)}`);
return;
}
console.error('first test result');
if (!title) {
t.fail(`Failed to retrieve a title for Google, body: ${body}, response: ${response}`);
}
t.is(title, 'Google');
};
const {title, error, body, response} = await getTitleAtUrl(validUrl);
resultCallback(title, error, response, body);
});
test('Won\'t work with an invalid url', async t => {
try {
const {error} = await getTitleAtUrl('afewaefaefwf', () => {});
if (error) {
t.pass();
} else {
t.fail();
}
} catch {
t.pass();
}
});
test('Shouldn\'t work with a 404', async t => {
const callback = (title, error) => {
if (error) {
t.pass();
} else {
t.fail();
}
};
const {title, error} = await getTitleAtUrl(fourOhFourUrl);
callback(title, error);
});
test('can handle Yahoo', async t => {
const {title} = await getTitleAtUrl(difficultUrl);
t.is(title, 'Yahoo');
});