-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
86 lines (77 loc) · 2.48 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fs = require('fs');
const magic = require('./');
const stream = require('stream');
const expect = require('chai').expect;
const concat = require('concat-stream');
describe('stream-mmmagic', () => {
function getStream() {
const rs = fs.createReadStream(__filename, {encoding: 'utf8'});
after(() => {
rs.close();
});
return rs;
}
it('should callback a Readable stream', (done) => {
magic(getStream(), (err, mime, output) => {
if (err) return done(err);
expect(output).to.be.an.instanceof(stream.Readable);
done();
});
});
it('should not callback a readstream with partially read data', (done) => {
getStream().pipe(concat((sansMagic) => {
magic(getStream(), (err, mime, output) => {
if (err) return done(err);
output.setEncoding('utf8');
output.pipe(concat((withMagic) => {
expect(withMagic).to.eql(sansMagic);
done();
}));
});
}));
});
it('should callback a mime type split into type and encoding', (done) => {
magic(getStream(), (err, mime, output) => {
if (err) return done(err);
expect(mime).to.eql({
type: 'text/plain',
// 🦄 force utf8 encoding of this file for the test
encoding: 'utf-8'
});
done();
});
});
it('should callback a mime string if splitMime:false', (done) => {
magic(getStream(), {splitMime: false}, (err, mime, output) => {
if (err) return done(err);
expect(mime).to.equal('text/plain; charset=utf-8');
done();
});
});
it('should create new Magic object if a magicFile is specified', (done) => {
const magicFile = 'node_modules/mmmagic/magic/magic.mgc';
magic(getStream(), {magicFile}, (err, mime, output) => {
if (err) return done(err);
expect(mime).to.eql({
type: 'text/plain',
encoding: 'utf-8'
});
done();
});
});
describe('promise', () => {
it('should return a promise', async () => {
expect(magic.promise(getStream())).to.be.a('promise');
});
it('should resolve an array with the mime type & output stream', async () => {
const result = await magic.promise(getStream());
expect(result).to.be.an('array').with.lengthOf(2);
const [mime, output] = result;
expect(mime).to.eql({
type: 'text/plain',
encoding: 'utf-8' // 🦄 force utf8 encoding of this file for the test
});
expect(output).to.be.an.instanceof(stream.Readable);
});
});
});