/
index.test.js
151 lines (119 loc) · 4.13 KB
/
index.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
146
147
148
149
150
151
'use strict';
var expect = require('chai').use(require('sinon-chai')).expect;
var sinon = require('sinon');
var es = require('event-stream');
var gutil = require('gulp-util');
var fixt = require('./fixtures/index.fixtures');
describe('plugin:gulpTransform()', function() {
var transform;
before(function() {
transform = require('../index');
});
it('is the main module', function() {
expect(transform).to.equal(require('..'));
});
it('exports gulpTransform()', function() {
expect(transform).to.be.instanceof(Function);
expect(transform).to.have.property('name', 'gulpTransform');
});
describe('param:transformFn', function() {
var fn; var file;
beforeEach(function() {
file = fixt.file.buffered();
fn = fixt.fn.transform();
transform(fn).write(file);
});
it('throws PluginError if undefined', function() {
expect(transform).to.throw(gutil.PluginError);
});
it('throws PluginError if not a Function', function() {
expect(transform.bind(null, 'paralysis')).to.throw(gutil.PluginError);
});
it('throws PluginError if return value is not a ctring/Buffer', function() {
expect(function() {
transform(fixt.fn.bufferReturn()).write(fixt.file.buffered());
}).not.to.throw(gutil.PluginError);
expect(function() {
transform(fixt.fn.noReturn()).write(fixt.file.buffered());
}).to.throw(gutil.PluginError);
});
it('is called once per file', function() {
expect(fn).to.have.been.calledOnce;
});
it('is called with contents as first argument', function() {
expect(fn).to.have.been.calledWith(fixt.data.buffer);
});
it('is called with file as second argument', function() {
expect(fn).to.have.been.calledWith(sinon.match.any, file);
});
});
describe('param:options', function() {
var fn; var file;
beforeEach(function() {
file = fixt.file.buffered();
fn = fixt.fn.transform();
});
it('throws a PluginError if defined but not an Object', function() {
expect(transform.bind(null, fn, 42)).to.throw(gutil.PluginError);
});
describe('option:encoding', function() {
it('determines encoding of contents passed to transformFn', function() {
transform(fn, {encoding: 'utf8'}).write(file);
expect(fn).to.have.been.calledWith(fixt.data.string);
});
it('contents passed as a Buffer if undefined', function() {
transform(fn).write(file);
expect(fn).to.have.been.calledWith(fixt.data.buffer);
});
});
describe('option:thisArg', function() {
it('determines value of this in transformFn', function() {
transform(fn, {thisArg: 42}).write(file);
expect(fn).to.have.been.calledOn(42);
});
it('this is undefined if thisArg is undefined', function() {
transform(fn).write(file);
expect(fn).to.have.been.calledOn(undefined);
});
});
});
describe('mode:buffer', function() {
var file;
beforeEach(function(done) {
transform(fixt.fn.transform()).once('data', function(_file) {
file = _file;
done();
}).write(fixt.file.buffered());
});
it('returns a stream of vinyl files', function() {
expect(gutil.File.isVinyl(file)).to.be.true;
});
it('files are in buffer mode', function() {
expect(file.isBuffer()).to.be.true;
});
it('transforms file contents', function() {
expect(file).property('contents').to.deep.equal(fixt.expected.buffer);
});
});
describe('mode:streaming', function() {
var file;
beforeEach(function(done) {
transform(fixt.fn.transform()).once('data', function(_file) {
file = _file;
done();
}).write(fixt.file.streaming());
});
it('returns a stream of vinyl files', function() {
expect(gutil.File.isVinyl(file)).to.be.true;
});
it('files are in streaming mode', function() {
expect(file.isStream()).to.be.true;
});
it('transforms file contents', function(done) {
file.pipe(es.wait(function(err, data) {
expect(data).to.deep.equal(fixt.expected.buffer);
done();
}));
});
});
});