Skip to content

Commit ca44579

Browse files
committed
test: ensure to handle invalid input
closes Kikobeats#13
1 parent cd08aa8 commit ca44579

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
const { gcd, highestFirst, formatAspectRatio } = require('./util')
44

55
module.exports = (height, width, seperator = ':') => {
6-
if (typeof height !== 'number') throw new Error(`Invalid height: ${height}`)
7-
if (typeof width !== 'number') throw new Error(`Invalid width: ${width}`)
6+
if (typeof height !== 'number') {
7+
throw new Error(
8+
`Invalid height: expected a \`number\`, received \`${height}\``
9+
)
10+
}
11+
12+
if (typeof width !== 'number') {
13+
throw new Error(
14+
`Invalid width: expected a \`number\`, received \`${width}\``
15+
)
16+
}
17+
818
const [h, w] = highestFirst(height, width)
919
const divisor = gcd(h, w)
1020
return formatAspectRatio(h, w, divisor, seperator)

test/index.js

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,27 @@ const aspectRatio = require('..')
44
const should = require('should')
55

66
describe('aspect ratio', () => {
7-
it('1024x768', () => {
8-
should(aspectRatio(1024, 768)).equal('4:3')
9-
})
10-
11-
it('1920x1080', () => {
12-
should(aspectRatio(1920, 1080)).equal('16:9')
13-
})
14-
15-
it('1280x1024', () => {
16-
should(aspectRatio(1280, 1024)).equal('5:4')
17-
})
18-
19-
it('using other separator', () => {
20-
should(aspectRatio(1920, 1080, '/')).equal('16/9')
21-
})
22-
23-
it('invalid width', () => {
24-
let error = ''
25-
try {
26-
should(aspectRatio(1920, null, '/')).equal('16/9')
27-
} catch (e) {
28-
error = e.message
29-
}
30-
should(error).equal('Invalid width: null')
31-
})
32-
33-
it('invalid height', () => {
34-
let error = ''
35-
try {
36-
should(aspectRatio(null, 1080, '/')).equal('16/9')
37-
} catch (e) {
38-
error = e.message
39-
}
40-
should(error).equal('Invalid height: null')
41-
})
42-
43-
it('invalid width & height', () => {
44-
let error = ''
45-
try {
46-
should(aspectRatio(null, null)).equal('16/9')
47-
} catch (e) {
48-
error = e.message
49-
}
50-
should(error).equal('Invalid height: null')
7+
it('1024x768', () => should(aspectRatio(1024, 768)).equal('4:3'))
8+
it('1920x1080', () => should(aspectRatio(1920, 1080)).equal('16:9'))
9+
it('1280x1024', () => should(aspectRatio(1280, 1024)).equal('5:4'))
10+
it('specifying output separator', () =>
11+
should(aspectRatio(1920, 1080, '/')).equal('16/9'))
12+
13+
describe('invalid input', () => {
14+
;[
15+
[1920, null, '/'],
16+
[null, 1080, '/'],
17+
[null, null],
18+
[undefined, undefined],
19+
['1920', '1080']
20+
].forEach(input => {
21+
it(JSON.stringify(input), () => {
22+
try {
23+
aspectRatio(...input)
24+
} catch ({ message }) {
25+
should(message.includes('Invalid')).be.true()
26+
}
27+
})
28+
})
5129
})
5230
})

0 commit comments

Comments
 (0)