|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var expect = require('expect'); |
| 4 | + |
| 5 | +var miss = require('mississippi'); |
| 6 | +var File = require('vinyl'); |
| 7 | +var normalize = require('normalize-path'); |
| 8 | + |
| 9 | +var mapFile = require('../'); |
| 10 | + |
| 11 | +var pipe = miss.pipe; |
| 12 | +var from = miss.from; |
| 13 | +var concat = miss.concat; |
| 14 | + |
| 15 | +function makeFile() { |
| 16 | + var file = new File({ |
| 17 | + cwd: __dirname, |
| 18 | + base: __dirname + '/assets', |
| 19 | + path: __dirname + '/assets/helloworld.js', |
| 20 | + contents: null, |
| 21 | + }); |
| 22 | + |
| 23 | + file.sourceMap = { |
| 24 | + version: 3, |
| 25 | + file: 'helloworld.js', |
| 26 | + names: [], |
| 27 | + mappings: '', |
| 28 | + sources: ['helloworld.js', 'helloworld2.js'], |
| 29 | + }; |
| 30 | + |
| 31 | + return file; |
| 32 | +} |
| 33 | + |
| 34 | +describe('mapFile', function() { |
| 35 | + |
| 36 | + it('ignores a file without sourceMap property', function(done) { |
| 37 | + var file = makeFile(); |
| 38 | + delete file.sourceMap; |
| 39 | + |
| 40 | + var spy = expect.createSpy(); |
| 41 | + |
| 42 | + function assert(files) { |
| 43 | + expect(files.length).toEqual(1); |
| 44 | + expect(spy).toNotHaveBeenCalled(); |
| 45 | + } |
| 46 | + |
| 47 | + pipe([ |
| 48 | + from.obj([file]), |
| 49 | + mapFile(spy), |
| 50 | + concat(assert), |
| 51 | + ], done); |
| 52 | + }); |
| 53 | + |
| 54 | + it('only ignores a file without sourceMap property', function(done) { |
| 55 | + var file = makeFile(); |
| 56 | + delete file.sourceMap; |
| 57 | + var file2 = makeFile(); |
| 58 | + |
| 59 | + function mapFn(filePath) { |
| 60 | + return filePath; |
| 61 | + } |
| 62 | + |
| 63 | + var spy = expect.createSpy().andCall(mapFn); |
| 64 | + |
| 65 | + function assert(files) { |
| 66 | + expect(files.length).toEqual(2); |
| 67 | + expect(spy.calls.length).toEqual(1); |
| 68 | + } |
| 69 | + |
| 70 | + pipe([ |
| 71 | + from.obj([file, file2]), |
| 72 | + mapFile(spy), |
| 73 | + concat(assert), |
| 74 | + ], done); |
| 75 | + }); |
| 76 | + |
| 77 | + it('calls map function per file', function(done) { |
| 78 | + var file = makeFile(); |
| 79 | + |
| 80 | + function mapFn(filePath) { |
| 81 | + return '/test/' + filePath; |
| 82 | + } |
| 83 | + |
| 84 | + function assert(files) { |
| 85 | + expect(files.length).toEqual(1); |
| 86 | + expect(files[0].sourceMap.file).toEqual('/test/helloworld.js'); |
| 87 | + } |
| 88 | + |
| 89 | + pipe([ |
| 90 | + from.obj([file]), |
| 91 | + mapFile(mapFn), |
| 92 | + concat(assert), |
| 93 | + ], done); |
| 94 | + }); |
| 95 | + |
| 96 | + it('normalizes Windows paths to unix paths', function(done) { |
| 97 | + var file = makeFile(); |
| 98 | + |
| 99 | + function mapFn(filePath) { |
| 100 | + return '\\test\\' + filePath; |
| 101 | + } |
| 102 | + |
| 103 | + function assert(files) { |
| 104 | + expect(files.length).toEqual(1); |
| 105 | + expect(files[0].sourceMap.file).toEqual('/test/helloworld.js'); |
| 106 | + } |
| 107 | + |
| 108 | + pipe([ |
| 109 | + from.obj([file]), |
| 110 | + mapFile(mapFn), |
| 111 | + concat(assert), |
| 112 | + ], done); |
| 113 | + }); |
| 114 | + |
| 115 | + it('does not need a map function', function(done) { |
| 116 | + var file = makeFile(); |
| 117 | + |
| 118 | + function assert(files) { |
| 119 | + expect(files.length).toEqual(1); |
| 120 | + expect(files[0].sourceMap.file).toEqual('helloworld.js'); |
| 121 | + } |
| 122 | + |
| 123 | + pipe([ |
| 124 | + from.obj([file]), |
| 125 | + mapFile(), |
| 126 | + concat(assert), |
| 127 | + ], done); |
| 128 | + }); |
| 129 | + |
| 130 | + it('ignores non-function argument', function(done) { |
| 131 | + var file = makeFile(); |
| 132 | + |
| 133 | + function assert(files) { |
| 134 | + expect(files.length).toEqual(1); |
| 135 | + expect(files[0].sourceMap.file).toEqual('helloworld.js'); |
| 136 | + } |
| 137 | + |
| 138 | + pipe([ |
| 139 | + from.obj([file]), |
| 140 | + mapFile('invalid argument'), |
| 141 | + concat(assert), |
| 142 | + ], done); |
| 143 | + }); |
| 144 | + |
| 145 | + it('still normalizes without a map function', function(done) { |
| 146 | + var file = makeFile(); |
| 147 | + file.sourceMap.file = '\\test\\' + file.sourceMap.file; |
| 148 | + |
| 149 | + function assert(files) { |
| 150 | + expect(files.length).toEqual(1); |
| 151 | + expect(files[0].sourceMap.file).toEqual('/test/helloworld.js'); |
| 152 | + } |
| 153 | + |
| 154 | + pipe([ |
| 155 | + from.obj([file]), |
| 156 | + mapFile(), |
| 157 | + concat(assert), |
| 158 | + ], done); |
| 159 | + }); |
| 160 | + |
| 161 | + it('calls map function with the filePath and the vinyl file', function(done) { |
| 162 | + var file = makeFile(); |
| 163 | + |
| 164 | + function mapFn(filePath, file) { |
| 165 | + expect(File.isVinyl(file)).toEqual(true); |
| 166 | + |
| 167 | + return file.base + '/' + filePath; |
| 168 | + } |
| 169 | + |
| 170 | + function assert(files) { |
| 171 | + expect(files.length).toEqual(1); |
| 172 | + |
| 173 | + var file = files[0]; |
| 174 | + var base = normalize(file.base); |
| 175 | + |
| 176 | + expect(file.sourceMap.file).toEqual(base + '/helloworld.js'); |
| 177 | + } |
| 178 | + |
| 179 | + pipe([ |
| 180 | + from.obj([file]), |
| 181 | + mapFile(mapFn), |
| 182 | + concat(assert), |
| 183 | + ], done); |
| 184 | + }); |
| 185 | + |
| 186 | + it('calls map function with undefined if sourceMap.file is undefined', function(done) { |
| 187 | + var file = makeFile(); |
| 188 | + delete file.sourceMap.file; |
| 189 | + |
| 190 | + function mapFn(filePath) { |
| 191 | + expect(filePath).toEqual(undefined); |
| 192 | + return 'test'; |
| 193 | + } |
| 194 | + |
| 195 | + function assert(files) { |
| 196 | + expect(files.length).toEqual(1); |
| 197 | + expect(files[0].sourceMap.file).toEqual('test'); |
| 198 | + } |
| 199 | + |
| 200 | + pipe([ |
| 201 | + from.obj([file]), |
| 202 | + mapFile(mapFn), |
| 203 | + concat(assert), |
| 204 | + ], done); |
| 205 | + }); |
| 206 | +}); |
0 commit comments