| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RawModule = require("../lib/RawModule"); | 
					
						
							|  |  |  | const OriginalSource = require("webpack-sources").OriginalSource; | 
					
						
							|  |  |  | const RawSource = require("webpack-sources").RawSource; | 
					
						
							|  |  |  | const RequestShortener = require("../lib/RequestShortener"); | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2017-06-19 01:25:03 +08:00
										 |  |  | const crypto = require("crypto"); | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe("RawModule", () => { | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 	const source = "sourceStr attribute"; | 
					
						
							|  |  |  | 	const identifier = "identifierStr attribute"; | 
					
						
							|  |  |  | 	const readableIdentifier = "readableIdentifierStr attribute"; | 
					
						
							|  |  |  | 	const myRawModule = new RawModule(source, identifier, readableIdentifier); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 	describe("identifier", () => { | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 		it("returns value for identifierStr attribute", () => { | 
					
						
							|  |  |  | 			expect(myRawModule.identifier()).toBe("identifierStr attribute"); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 	describe("size", () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 		it('returns value for sourceStr attribute"s length property', () => { | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 			const sourceStrLength = myRawModule.sourceStr.length; | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 			expect(myRawModule.size()).toBe(sourceStrLength); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 	describe("readableIdentifier", () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 		it( | 
					
						
							|  |  |  | 			'returns result of calling provided requestShortener"s shorten method ' + | 
					
						
							|  |  |  | 				"on readableIdentifierStr attribute", | 
					
						
							| 
									
										
										
										
											2017-02-10 06:38:39 +08:00
										 |  |  | 			() => { | 
					
						
							|  |  |  | 				const requestShortener = new RequestShortener(path.resolve()); | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 				expect(myRawModule.readableIdentifier(requestShortener)).toBeDefined(); | 
					
						
							| 
									
										
										
										
											2017-02-10 06:38:39 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 	describe("needRebuild", () => { | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 		it("returns false", () => { | 
					
						
							|  |  |  | 			expect(myRawModule.needRebuild()).toBe(false); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 	describe("source", () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 		it( | 
					
						
							|  |  |  | 			"returns a new OriginalSource instance with sourceStr attribute and " + | 
					
						
							|  |  |  | 				"return value of identifier() function provided as constructor arguments", | 
					
						
							| 
									
										
										
										
											2017-01-18 22:27:19 +08:00
										 |  |  | 			() => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 				const originalSource = new OriginalSource( | 
					
						
							|  |  |  | 					myRawModule.sourceStr, | 
					
						
							|  |  |  | 					myRawModule.identifier() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 				myRawModule.useSourceMap = true; | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 				expect(myRawModule.source()).toEqual(originalSource); | 
					
						
							| 
									
										
										
										
											2017-02-10 06:38:39 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 		it( | 
					
						
							|  |  |  | 			"returns a new RawSource instance with sourceStr attribute provided " + | 
					
						
							| 
									
										
										
										
											2018-02-26 10:28:40 +08:00
										 |  |  | 				"as constructor argument if useSourceMap is falsy", | 
					
						
							| 
									
										
										
										
											2017-02-10 06:38:39 +08:00
										 |  |  | 			() => { | 
					
						
							|  |  |  | 				const rawSource = new RawSource(myRawModule.sourceStr); | 
					
						
							|  |  |  | 				myRawModule.useSourceMap = false; | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 				expect(myRawModule.source()).toEqual(rawSource); | 
					
						
							| 
									
										
										
										
											2017-02-10 06:38:39 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-06-19 01:25:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe("updateHash", () => { | 
					
						
							|  |  |  | 		it("should include sourceStr in its hash", () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			const hashModule = module => { | 
					
						
							| 
									
										
										
										
											2017-06-19 01:25:03 +08:00
										 |  |  | 				const hash = crypto.createHash("sha256"); | 
					
						
							|  |  |  | 				module.updateHash(hash); | 
					
						
							|  |  |  | 				return hash.digest("hex"); | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			const hashFoo = hashModule(new RawModule('"foo"')); | 
					
						
							|  |  |  | 			const hashBar = hashModule(new RawModule('"bar"')); | 
					
						
							| 
									
										
										
										
											2018-01-24 20:17:21 +08:00
										 |  |  | 			expect(hashFoo).not.toBe(hashBar); | 
					
						
							| 
									
										
										
										
											2017-06-19 01:25:03 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-01-06 22:23:29 +08:00
										 |  |  | }); |