Skip to content

Commit e1acd76

Browse files
committed
add tests
1 parent 22764e3 commit e1acd76

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const pluginName = 'gulp-typed-css-modules';
88
module.exports = options=>{
99
options = options || {};
1010

11-
const DtsCreator = options.ctm || require('typed-css-modules');
11+
const DtsCreator = options.tcm || require('typed-css-modules');
1212

1313
const creator = new DtsCreator(options);
1414

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Gulp plugin for typed-css-modules",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "mocha"
88
},
99
"keywords": [
1010
"gulp",
@@ -17,5 +17,10 @@
1717
"gulp-util": "^3.0.7",
1818
"through2": "^2.0.3",
1919
"typed-css-modules": "^0.1.13"
20+
},
21+
"devDependencies": {
22+
"event-stream": "^3.3.4",
23+
"mocha": "^3.2.0",
24+
"vinyl": "^2.0.1"
2025
}
2126
}

test/test.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
const assert = require('assert');
2+
const path = require('path');
3+
const PassThrough = require('stream').PassThrough;
4+
const es = require('event-stream');
5+
const Vinyl = require('vinyl');
6+
7+
const gulptcm = require('../');
8+
9+
// mock for DtsCreator
10+
class FakeTCMFactory{
11+
constructor(){
12+
this.buf = null;
13+
}
14+
make(resultString){
15+
const factory = this;
16+
return class FakeTCM {
17+
create(filepath, content){
18+
factory.buf = content;
19+
20+
return Promise.resolve({
21+
tokens: [],
22+
contents: [],
23+
formatted: resultString,
24+
messageList: [],
25+
outputFilePath: filepath,
26+
});
27+
}
28+
};
29+
}
30+
}
31+
32+
describe('gulp-typed-css-modules', ()=>{
33+
describe('buffer mode', ()=>{
34+
it('handle file', done=>{
35+
36+
const file = new Vinyl({
37+
path: path.join(__dirname, 'fake.css'),
38+
contents: new Buffer('.foo { display: none; }'),
39+
});
40+
41+
const factory = new FakeTCMFactory();
42+
43+
const stream = gulptcm({
44+
tcm: factory.make('export const myClass: string;\n'),
45+
});
46+
47+
stream.write(file);
48+
49+
stream.once('data', file=>{
50+
// file is returned as Buffer file
51+
assert(file.isBuffer());
52+
53+
// file extension is changed
54+
assert.equal(file.path, path.join(__dirname, 'fake.d.ts'));
55+
56+
// file content is properly passed
57+
assert.equal(factory.buf, '.foo { display: none; }');
58+
59+
// the content of buffer is correct
60+
assert.equal(file.contents.toString('utf8'), 'export const myClass: string;\n');
61+
62+
done();
63+
});
64+
});
65+
});
66+
describe('stream mode', ()=>{
67+
it('handle file', done=>{
68+
69+
const inputStream = new PassThrough();
70+
const file = new Vinyl({
71+
path: path.join(__dirname, 'fake.css'),
72+
// contents: es.readArray(['.bar ', '{ displa', 'y: none;', ' }\n']),
73+
contents: inputStream,
74+
});
75+
76+
inputStream.write(new Buffer('.bar '));
77+
inputStream.write(new Buffer('{ displa'));
78+
inputStream.write(new Buffer('y: none;'));
79+
inputStream.write(new Buffer(' }\n'));
80+
inputStream.end();
81+
82+
const factory = new FakeTCMFactory();
83+
84+
const stream = gulptcm({
85+
tcm: factory.make('export const bar: string;\n'),
86+
});
87+
88+
stream.write(file);
89+
90+
stream.once('data', file=>{
91+
// file is returned as Stream file
92+
assert(file.isStream());
93+
94+
// file extension is changed
95+
assert.equal(file.path, path.join(__dirname, 'fake.d.ts'));
96+
97+
// the content of buffer is correct
98+
file.contents.pipe(es.wait((err, data)=>{
99+
// file content is properly passed
100+
assert.equal(factory.buf, '.bar { display: none; }\n');
101+
102+
// result of tcm is received
103+
assert.equal(data, 'export const bar: string;\n');
104+
done();
105+
}));
106+
});
107+
});
108+
});
109+
describe('null file', ()=>{
110+
it ('ignore null files', done=>{
111+
const file = new Vinyl({
112+
path: path.join(__dirname, 'fake.css'),
113+
contents: null,
114+
});
115+
116+
const factory = new FakeTCMFactory();
117+
factory.buf = '吉野家';
118+
119+
const stream = gulptcm({
120+
tcm: factory.make('This is not returned!'),
121+
});
122+
123+
stream.write(file);
124+
stream.end();
125+
126+
stream.once('data', file=>{
127+
done(new Error('file comes!'));
128+
});
129+
stream.once('end', ()=>{
130+
// no tcm call
131+
assert.equal(factory.buf, '吉野家');
132+
133+
done();
134+
});
135+
});
136+
});
137+
});

0 commit comments

Comments
 (0)