|
| 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