|
| 1 | +var should = require("should"); |
| 2 | + |
| 3 | +var LoadersList = require("../lib/LoadersList"); |
| 4 | + |
| 5 | +describe("LoaderList", function() { |
| 6 | + it('should create LoadersList without any input', function() { |
| 7 | + var loader = new LoadersList(); |
| 8 | + (loader.list).should.eql([]); |
| 9 | + }); |
| 10 | + it('should create LoadersList with a blank array', function() { |
| 11 | + var loader = new LoadersList([]); |
| 12 | + (loader.list).should.eql([]); |
| 13 | + }); |
| 14 | + it('should throw when create LoadersList with an invalid array', function() { |
| 15 | + should.throws(function() { |
| 16 | + var loader = new LoadersList(['dummyString']); |
| 17 | + }, /must be an object or array/); |
| 18 | + }); |
| 19 | + it('should create LoadersList with an invalid array', function() { |
| 20 | + should.throws(function() { |
| 21 | + var loader = new LoadersList(['dummyString']); |
| 22 | + }, /must be an object or array/); |
| 23 | + }); |
| 24 | + it('should create LoadersList with an valid array', function() { |
| 25 | + var loaders = [{ |
| 26 | + test: /\.css$/, |
| 27 | + loader: 'css' |
| 28 | + }]; |
| 29 | + var loader = new LoadersList(loaders); |
| 30 | + (loader.list).should.eql(loaders); |
| 31 | + }); |
| 32 | + it('should create LoadersList and match with empty array', function() { |
| 33 | + var loader = new LoadersList([]); |
| 34 | + (loader.match('something')).should.eql([]); |
| 35 | + }); |
| 36 | + it('should not match with loaders array', function() { |
| 37 | + var loader = new LoadersList([{ |
| 38 | + test: /\.css$/, |
| 39 | + loader: 'css' |
| 40 | + }]); |
| 41 | + (loader.match('something')).should.eql([]); |
| 42 | + }); |
| 43 | + it('should match with regex', function() { |
| 44 | + var loader = new LoadersList([{ |
| 45 | + test: /\.css$/, |
| 46 | + loader: 'css' |
| 47 | + }]); |
| 48 | + (loader.match('style.css')).should.eql(['css']); |
| 49 | + }); |
| 50 | + it('should match with string', function() { |
| 51 | + var loader = new LoadersList([{ |
| 52 | + test: 'style.css', |
| 53 | + loader: 'css' |
| 54 | + }]); |
| 55 | + (loader.match('style.css')).should.eql(['css']); |
| 56 | + }); |
| 57 | + it('should match with function', function() { |
| 58 | + var loader = new LoadersList([{ |
| 59 | + test: function(str) { |
| 60 | + return str === 'style.css'; |
| 61 | + }, |
| 62 | + loader: 'css' |
| 63 | + }]); |
| 64 | + (loader.match('style.css')).should.eql(['css']); |
| 65 | + }); |
| 66 | + it('should throw if invalid test', function() { |
| 67 | + var loader = new LoadersList([{ |
| 68 | + test: { |
| 69 | + invalid: 'test' |
| 70 | + }, |
| 71 | + loader: 'css' |
| 72 | + }]); |
| 73 | + should.throws(function() { |
| 74 | + (loader.match('style.css')).should.eql(['css']); |
| 75 | + }, /not a valid test/); |
| 76 | + }); |
| 77 | + it('should accept multiple test array that all match', function() { |
| 78 | + var loader = new LoadersList([{ |
| 79 | + test: [ |
| 80 | + /style.css/, |
| 81 | + /yle.css/ |
| 82 | + ], |
| 83 | + loader: 'css' |
| 84 | + }]); |
| 85 | + (loader.match('style.css')).should.eql(['css']); |
| 86 | + }); |
| 87 | + it('should accept multiple test array that not all match', function() { |
| 88 | + var loader = new LoadersList([{ |
| 89 | + test: [ |
| 90 | + /style.css/, |
| 91 | + /something.css/ |
| 92 | + ], |
| 93 | + loader: 'css' |
| 94 | + }]); |
| 95 | + (loader.match('style.css')).should.eql([]); |
| 96 | + }); |
| 97 | + it('should not match if include does not match', function() { |
| 98 | + var loader = new LoadersList([{ |
| 99 | + test: /\.css$/, |
| 100 | + include: /output.css/, |
| 101 | + loader: 'css' |
| 102 | + }]); |
| 103 | + (loader.match('style.css')).should.eql([]); |
| 104 | + }); |
| 105 | + it('should match if include matches', function() { |
| 106 | + var loader = new LoadersList([{ |
| 107 | + test: /\.css$/, |
| 108 | + include: /style.css/, |
| 109 | + loader: 'css' |
| 110 | + }]); |
| 111 | + (loader.match('style.css')).should.eql(['css']); |
| 112 | + }); |
| 113 | + it('should not match if exclude matches', function() { |
| 114 | + var loader = new LoadersList([{ |
| 115 | + test: /\.css$/, |
| 116 | + exclude: /style.css/, |
| 117 | + loader: 'css' |
| 118 | + }]); |
| 119 | + (loader.match('style.css')).should.eql([]); |
| 120 | + }); |
| 121 | + it('should match if exclude does not match', function() { |
| 122 | + var loader = new LoadersList([{ |
| 123 | + test: /\.css$/, |
| 124 | + exclude: /output.css/, |
| 125 | + loader: 'css' |
| 126 | + }]); |
| 127 | + (loader.match('style.css')).should.eql(['css']); |
| 128 | + }); |
| 129 | + it('should work if a loader is applied to all files', function() { |
| 130 | + var loader = new LoadersList([{ |
| 131 | + loader: 'css' |
| 132 | + }]); |
| 133 | + (loader.match('style.css')).should.eql(['css']); |
| 134 | + (loader.match('scripts.js')).should.eql(['css']); |
| 135 | + }); |
| 136 | + it('should work with using loader as string', function() { |
| 137 | + var loader = new LoadersList([{ |
| 138 | + test: /\.css$/, |
| 139 | + loader: 'css' |
| 140 | + }]); |
| 141 | + (loader.match('style.css')).should.eql(['css']); |
| 142 | + }); |
| 143 | + it('should work with using loader as array', function() { |
| 144 | + var loader = new LoadersList([{ |
| 145 | + test: /\.css$/, |
| 146 | + loader: ['css'] |
| 147 | + }]); |
| 148 | + (loader.match('style.css')).should.eql(['css']); |
| 149 | + }); |
| 150 | + it('should work with using loaders as string', function() { |
| 151 | + var loader = new LoadersList([{ |
| 152 | + test: /\.css$/, |
| 153 | + loaders: 'css' |
| 154 | + }]); |
| 155 | + (loader.match('style.css')).should.eql(['css']); |
| 156 | + }); |
| 157 | + it('should work with using loaders as array', function() { |
| 158 | + var loader = new LoadersList([{ |
| 159 | + test: /\.css$/, |
| 160 | + loaders: ['css'] |
| 161 | + }]); |
| 162 | + (loader.match('style.css')).should.eql(['css']); |
| 163 | + }); |
| 164 | + it('should throw if using loaders with non-string or array', function() { |
| 165 | + var loader = new LoadersList([{ |
| 166 | + test: /\.css$/, |
| 167 | + loaders: { |
| 168 | + someObj: true |
| 169 | + } |
| 170 | + }]); |
| 171 | + should.throws(function() { |
| 172 | + (loader.match('style.css')).should.eql(['css']); |
| 173 | + }, /should have one of the fields/) |
| 174 | + }); |
| 175 | + it('should work with using loader with inline query', function() { |
| 176 | + var loader = new LoadersList([{ |
| 177 | + test: /\.css$/, |
| 178 | + loader: 'css?modules=1' |
| 179 | + }]); |
| 180 | + (loader.match('style.css')).should.eql(['css?modules=1']); |
| 181 | + }); |
| 182 | + it('should work with using loader with string query', function() { |
| 183 | + var loader = new LoadersList([{ |
| 184 | + test: /\.css$/, |
| 185 | + loader: 'css', |
| 186 | + query: 'modules=1' |
| 187 | + }]); |
| 188 | + (loader.match('style.css')).should.eql(['css?modules=1']); |
| 189 | + }); |
| 190 | + it('should work with using loader with object query', function() { |
| 191 | + var loader = new LoadersList([{ |
| 192 | + test: /\.css$/, |
| 193 | + loader: 'css', |
| 194 | + query: { |
| 195 | + modules: 1 |
| 196 | + } |
| 197 | + }]); |
| 198 | + (loader.match('style.css')).should.eql(['css?{"modules":1}']); |
| 199 | + }); |
| 200 | + it('should work with using array loaders with basic object notation', function() { |
| 201 | + var loader = new LoadersList([{ |
| 202 | + test: /\.css$/, |
| 203 | + loaders: [{ |
| 204 | + loader: 'css' |
| 205 | + }] |
| 206 | + }]); |
| 207 | + (loader.match('style.css')).should.eql(['css']); |
| 208 | + }); |
| 209 | + it('should throw if using array loaders with object notation without specifying a loader', function() { |
| 210 | + var loader = new LoadersList([{ |
| 211 | + test: /\.css$/, |
| 212 | + loaders: [{ |
| 213 | + stuff: 1 |
| 214 | + }] |
| 215 | + }]); |
| 216 | + should.throws(function() { |
| 217 | + (loader.match('style.css')).should.eql(['css']); |
| 218 | + }, /should have a 'loader' specified/) |
| 219 | + }); |
| 220 | + it('should work with using array loaders with object notation', function() { |
| 221 | + var loader = new LoadersList([{ |
| 222 | + test: /\.css$/, |
| 223 | + loaders: [{ |
| 224 | + loader: 'css', |
| 225 | + query: 'modules=1' |
| 226 | + }] |
| 227 | + }]); |
| 228 | + (loader.match('style.css')).should.eql(['css?modules=1']); |
| 229 | + }); |
| 230 | + it('should work with using multiple array loaders with object notation', function() { |
| 231 | + var loader = new LoadersList([{ |
| 232 | + test: /\.css$/, |
| 233 | + loaders: [{ |
| 234 | + loader: 'style', |
| 235 | + query: 'filesize=1000' |
| 236 | + }, { |
| 237 | + loader: 'css', |
| 238 | + query: 'modules=1' |
| 239 | + }] |
| 240 | + }]); |
| 241 | + (loader.match('style.css')).should.eql(['style?filesize=1000', 'css?modules=1']); |
| 242 | + }); |
| 243 | + it('should work with using string multiple loaders', function() { |
| 244 | + var loader = new LoadersList([{ |
| 245 | + test: /\.css$/, |
| 246 | + loaders: 'style?filesize=1000!css?modules=1' |
| 247 | + }]); |
| 248 | + (loader.match('style.css')).should.eql(['style?filesize=1000', 'css?modules=1']); |
| 249 | + }); |
| 250 | + it('should throw if using array loaders with a single legacy', function() { |
| 251 | + var loader = new LoadersList([{ |
| 252 | + test: /\.css$/, |
| 253 | + loaders: ['style-loader', 'css-loader'], |
| 254 | + query: 'modules=1' |
| 255 | + }]); |
| 256 | + should.throws(function() { |
| 257 | + (loader.match('style.css')).should.eql(['css']); |
| 258 | + }, /Cannot define 'query' and multiple loaders in loaders list/) |
| 259 | + }); |
| 260 | + it('should throw if using array loaders with invalid type', function() { |
| 261 | + var loader = new LoadersList([{ |
| 262 | + test: /\.css$/, |
| 263 | + loaders: ['style-loader', 'css-loader', 5], |
| 264 | + }]); |
| 265 | + should.throws(function() { |
| 266 | + (loader.match('style.css')).should.eql(['css']); |
| 267 | + }, /Element from loaders list should be a string or an object/) |
| 268 | + }); |
| 269 | +}); |
0 commit comments