-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
69 lines (58 loc) · 2.07 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
64
65
66
67
68
69
import test from 'ava';
import 'babel-core/register';
import setupHtmlToAmp from './lib';
import http from 'http';
import Promise from 'bluebird';
const htmlToAmp = setupHtmlToAmp();
test('simple input', t => {
const html = 'foo bar';
return htmlToAmp(html)
.then(amp => { t.is(amp, '<article><p>foo bar</p></article>'); });
});
test('handle image with size', t => {
const html = '<img width="100" height="200" src="http://image.com" />';
const expected = '<article><figure><amp-img width="100" height="200" layout="responsive" src="http://image.com"></amp-img></figure></article>';
return htmlToAmp(html).then(amp => { t.is(amp, expected); });
});
test('handle image without size', t => {
return new Promise(resolve => {
const server = http.createServer((req, res) => {
res.end(new Buffer('R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==', 'base64'));
}).listen(0, () => {
resolve(server.address().port);
});
}).then(port => {
const expected = `<article><figure><amp-img width="1" height="1" layout="responsive" src="http://localhost:${port}/"></amp-img></figure></article>`;
const html = `<img src="http://localhost:${port}/">`;
return htmlToAmp(html).then(actual => { t.is(actual, expected); });
});
});
test('handle image without src & without width/height', t => {
const html = '<img />';
const expected = '<article><figure></figure></article>';
return htmlToAmp(html).then(amp => { t.is(amp, expected); });
});
test.cb('callback interface', t => {
const html = '<img />';
const expected = '<article><figure></figure></article>';
return htmlToAmp(html, (err, amp) => {
t.ifError(err);
t.is(amp, expected);
t.end();
});
});
test.cb('callback interface with an error', t => {
new Promise(resolve => {
const server = http.createServer((req, res) => {
res.end('beep boop');
}).listen(0, () => {
resolve(server.address().port);
});
}).then(port => {
const html = `<img src="http://localhost:${port}/">`;
htmlToAmp(html, err => {
t.ok(err);
t.end();
});
});
});