-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
145 lines (128 loc) · 3.89 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const expect = require('chai').expect,
sinon = require('sinon'),
stream = require('stream'),
fs = require('fs-extra'),
buf = s => Buffer.from(s, 'utf8'),
util = require('./lib/util'),
browserify = require('browserify');
/**
* Mocks a readable stream.
* @param {Array} chunks
* @param {boolean} objectMode
* @return {object} readable stream
*/
function mockStream(chunks, objectMode) {
const s = new stream.Readable({objectMode: objectMode});
chunks.forEach(chunk => s.push(chunk));
s.push(null);
return s;
};
/**
* Checks if each value in the stream matches the expected values.
* @param {object} stream
* @param {array} values
* @param {function} done
*/
function expectValues(stream, values, done) {
let i = 0;
stream
.on('data', (val) => {
const compare = values[i++];
if (typeof compare === 'object') {
expect(val).to.deep.equal(compare);
} else {
expect(val).to.equal(compare);
}
})
.on('end', () => {
if (i === values.length) {
done();
} else {
done(new Error(`Expected ${values.length} results but got ${i}`));
}
})
.on('error', done);
};
describe('index.js', function () {
const fn = require('./index');
it ('throws error if writeToDir is not set', function () {
expect(()=> fn(browserify(), {})).to.throw(Error);
});
it ('configures fileWriter correctly', function () {
const sandbox = sinon.sandbox.create();
sandbox.stub(util, 'fileWriter');
fn(browserify(), {writeToDir: 'someDir', verbose: true});
util.fileWriter.calledWith('someDir', true);
sandbox.restore();
});
});
describe('util.js', function () {
let sandbox;
describe('splitBundle', function () {
const fn = util[this.title];
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
it ('splits a pack stream as expected', function (done) {
const stream = mockStream([
buf('preludeA'),
buf('preludeB'),
buf('moduleA'),
buf(',moduleB'),
buf('postlude')
]),
modules = [{
id: 'moduleA',
file: './src/moduleA'
}, {
id: 'moduleB',
file: './src/moduleB'
}],
expectedValues = [
{path: 'moduleA.js', label: './src/moduleA', content: buf(',moduleA')},
{path: 'moduleB.js', label: './src/moduleB', content: buf(',moduleB')},
{path: 'prelude.js', label: '(prelude)', content: buf('preludeApreludeB0:[]')},
{path: 'postlude.js', label: '(postlude)', content: buf('postlude;')}
],
splitter = fn(modules);
expectValues(stream.pipe(splitter), expectedValues, done);
});
});
describe('fileWriter', function () {
const fn = util[this.title];
let sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
sandbox.stub(fs, 'outputFile');
fs.outputFile.callsFake((file, content, cb) => cb());
});
afterEach(function () {
sandbox.restore();
});
it ('writes out files as expected', function (done) {
mockStream([
{path: 'moduleA.js', label: './src/moduleA', content: buf('foo')},
{path: 'moduleB.js', label: './src/moduleB', content: buf('bar')}
], true)
.pipe(fn('someDir'))
.on('finish', () => {
expect(fs.outputFile.calledWith('someDir/moduleA.js', buf('foo'))).to.be.true;
expect(fs.outputFile.calledWith('someDir/moduleB.js', buf('bar'))).to.be.true;
done();
});
});
it ('logs file writes if log is set', function (done) {
mockStream([
{path: 'moduleA.js', label: './src/moduleA', content: buf('foo')},
], true).pipe(fn('someDir', true))
.on('finish', () => {
expect(console.log.called).to.be.true;
done();
});
sandbox.spy(console, 'log');
});
});
});