-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
52 lines (44 loc) · 1.51 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
import {exec} from 'child_process'
import path from 'path'
import test from 'ava'
import imageSize from 'image-size'
import del from 'del'
import mkdir from 'mkdir-promise'
test.before(async () => {
await mkdir('./images/out')
await execCli(140, 140)
await execCli(140, 140, '_test')
})
test.after.always(async () => await del('./images/out/*'))
test('resize jpg without prefix', async t => {
const jpgInfo = await sizeOf('./images/out/kitten.jpeg')
t.deepEqual(jpgInfo, {height: 88, width: 140, type: 'jpg'})
})
test('resize jpg with prefix', async t => {
const jpgInfo = await sizeOf('./images/out/kitten_test.jpeg')
t.deepEqual(jpgInfo, {height: 88, width: 140, type: 'jpg'})
})
test('resize png without prefix', async t => {
const pngInfo = await sizeOf('./images/out/kitten.png')
t.deepEqual(pngInfo, {height: 140, width: 121, type: 'png'})
})
test('resize png with prefix', async t => {
const pngInfo = await sizeOf('./images/out/kitten_test.png')
t.deepEqual(pngInfo, {height: 140, width: 121, type: 'png'})
})
function sizeOf(img) {
return new Promise((resolve, reject) => {
imageSize(
path.resolve(img),
(err, dimensions) => err ? reject(err) : resolve(dimensions))
})
}
function execCli(maxWidth, maxHeight, suffix) {
const suffixOpt = suffix ? `-s "${suffix}"` : ''
return new Promise((resolve, reject) => {
exec(
`node ./dist/index.js ./images/in/* -w ${maxWidth} -h ${maxHeight} ${suffixOpt} -o ./images/out`,
err => err ? reject(err) : resolve()
)
})
}