-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
45 lines (39 loc) · 1.35 KB
/
index.spec.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
'use strict'
const { createAvatar } = require('.')
const { join } = require('path')
const { mkdtempSync, createWriteStream, readFileSync } = require('fs')
const { tmpdir } = require('os')
const crypto = require('crypto')
const { expect } = require('@hapi/code')
describe('#createAvatar()', () => {
function createOutput () {
const dir = mkdtempSync(join(tmpdir(), 'byia-'))
const outputFile = join(dir, 'image.jpg')
return {
outputFile,
outputStream: createWriteStream(outputFile)
}
}
it('writes correct image', async () => {
const { outputFile, outputStream } = createOutput()
await createAvatar({ firstName: 'Antony', lastName: 'MacKenzie-Jones' }, outputStream, { background: '#ff0c7e' })
const data = readFileSync(outputFile, { encoding: 'base64' })
const hash = crypto
.createHash('md5')
.update(data)
.digest('hex')
expect(hash).to.equal('dcd5bed4310b6e099700122aa9cb5112')
})
it('with missing name', async () => {
const { outputStream } = createOutput()
await expect(
createAvatar({}, outputStream, { background: '#ff0c7e' })
).not.to.reject()
})
it('with empty name', async () => {
const { outputStream } = createOutput()
await expect(
createAvatar({ firstName: '', lastName: '' }, outputStream, { background: '#ff0c7e' })
).not.to.reject()
})
})