forked from sindresorhus/capture-website-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
72 lines (63 loc) · 1.77 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
70
71
72
import test from 'ava';
import {execa} from 'execa';
import createTestServer from 'create-test-server';
import {fileTypeFromBuffer} from 'file-type';
test('main', async t => {
const server = await createTestServer();
server.get('/', (request, response) => {
response.end('<body>Unicorn</body>');
});
const {stdout} = await execa('./cli.js', [server.url], {encoding: 'buffer'});
const {mime} = await fileTypeFromBuffer(stdout);
t.is(mime, 'image/png');
await server.close();
});
test('support HTML input', async t => {
const {stdout} = await execa('./cli.js', [], {
input: '<h1>Unicorn</h1>',
encoding: 'buffer',
});
const {mime} = await fileTypeFromBuffer(stdout);
t.is(mime, 'image/png');
});
test('check flags', async t => {
// Copied from `cli.js`
let flags = `
--output=screenshot.png
--width=1000
--height=600
--type=jpeg.
--quality=0.5
--scale-factor=3
--emulate-device="iPhone X"
--timeout=80
--delay=10
--wait-for-element="#header"
--element=".main-content"
--hide-elements=".sidebar"
--remove-elements="img.ad"
--click-element="button"
--scroll-to-element="#map"
--disable-animations
--no-javascript
--module=https://sindresorhus.com/remote-file.js
--module=local-file.js
--module="document.body.style.backgroundColor = 'red'"
--header="x-powered-by: capture-website-cli"
--user-agent="I love unicorns"
--cookie="id=unicorn; Expires=Wed, 21 Oct 2018 07:28:00 GMT;"
--authentication="username:password"
--launch-options="{\\"headless\\": false}"
--dark-mode
--inset=10,-15,-15,25
--clip=10,30,300,1024
--no-block-ads
`;
flags = flags.trim()
.replace(/(?<==)"|(?<!\\)"$/gm, '')
.replace(/\\"/g, '"')
.split('\n');
const {stdout} = await execa('./cli.js', ['noop-file', '--internal-print-flags', ...flags]);
const json = JSON.parse(stdout);
t.snapshot(json);
});