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