| 
									
										
										
										
											2017-02-20 17:28:14 +08:00
										 |  |  | /* global describe, beforeEach, it */ | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | require("should"); | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | const sinon = require("sinon"); | 
					
						
							|  |  |  | const TemplatePluginEnvironment = require("./helpers/TemplatePluginEnvironment"); | 
					
						
							|  |  |  | const ConcatSource = require("webpack-sources").ConcatSource; | 
					
						
							|  |  |  | const AmdMainTemplatePlugin = require("../lib/AmdMainTemplatePlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("AmdMainTemplatePlugin", () => { | 
					
						
							|  |  |  | 	let env; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const applyTemplatePluginWithOptions = function(Plugin, name) { | 
					
						
							|  |  |  | 		const plugin = new Plugin(name); | 
					
						
							|  |  |  | 		const templatePluginEnvironment = new TemplatePluginEnvironment(); | 
					
						
							|  |  |  | 		const environment = templatePluginEnvironment.getEnvironmentStub(); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 		environment.mainTemplate.applyPluginsWaterfall = () => "templateName"; | 
					
						
							|  |  |  | 		plugin.apply(environment); | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 		return templatePluginEnvironment; | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 	const setupPluginAndGetEventBinding = function(name) { | 
					
						
							|  |  |  | 		const templatePlugin = applyTemplatePluginWithOptions(AmdMainTemplatePlugin, name); | 
					
						
							|  |  |  | 		const eventBindings = templatePlugin.getEventBindings(); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 		return eventBindings[0]; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 	beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 		env = { | 
					
						
							|  |  |  | 			modulesListWithExternals: [{ | 
					
						
							| 
									
										
										
										
											2017-02-20 17:26:18 +08:00
										 |  |  | 				id: "module-1", | 
					
						
							|  |  |  | 				external: true, | 
					
						
							|  |  |  | 				request: { | 
					
						
							|  |  |  | 					amd: "external-amd-module" | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-20 17:26:18 +08:00
										 |  |  | 			}, { | 
					
						
							|  |  |  | 				id: "module-2", | 
					
						
							|  |  |  | 				external: true, | 
					
						
							|  |  |  | 				request: "external-non-amd-module" | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				id: "module-3", | 
					
						
							|  |  |  | 				external: true | 
					
						
							|  |  |  | 			}, { | 
					
						
							|  |  |  | 				id: "module-4", | 
					
						
							|  |  |  | 				external: false | 
					
						
							|  |  |  | 			}] | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 	it("has apply function", () => new AmdMainTemplatePlugin().apply.should.be.a.Function()); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 	describe("when applied", () => { | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 		beforeEach(() => | 
					
						
							|  |  |  | 			env.templatePlugin = applyTemplatePluginWithOptions(AmdMainTemplatePlugin, "foo")); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 		describe("event handlers", () => { | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 			beforeEach(() => env.eventBindings = env.templatePlugin.getEventBindings()); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 			it("binds one handlers", () => env.eventBindings.length.should.be.exactly(1)); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 			describe("render-with-entry handler", () => { | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 				beforeEach(() => env.eventBinding = env.eventBindings[0]); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 				it("binds to render-with-entry event", () => | 
					
						
							|  |  |  | 					env.eventBinding.name.should.be.exactly("render-with-entry")); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 				describe("with name", () => { | 
					
						
							|  |  |  | 					beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						env.chunk = { | 
					
						
							| 
									
										
										
										
											2017-06-25 22:40:35 +08:00
										 |  |  | 							getModules: () => env.modulesListWithExternals | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						}; | 
					
						
							|  |  |  | 						env.eventBinding = setupPluginAndGetEventBinding("foo"); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 					it("creates source wrapper with module name and external dependencies", () => { | 
					
						
							|  |  |  | 						const source = env.eventBinding.handler("moduleSource()", env.chunk, "bar"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						source.should.be.instanceof(ConcatSource); | 
					
						
							| 
									
										
										
										
											2017-02-20 17:28:14 +08:00
										 |  |  | 						source.source().should.be.exactly("define(\"templateName\", [\"external-amd-module\",\"external-non-amd-module\",null], function(__WEBPACK_EXTERNAL_MODULE_module_1__, __WEBPACK_EXTERNAL_MODULE_module_2__, __WEBPACK_EXTERNAL_MODULE_module_3__) { return moduleSource()});"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 				describe("with external dependencies", () => { | 
					
						
							|  |  |  | 					beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						env.chunk = { | 
					
						
							| 
									
										
										
										
											2017-06-25 22:40:35 +08:00
										 |  |  | 							getModules: () => env.modulesListWithExternals | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						}; | 
					
						
							|  |  |  | 						env.eventBinding = setupPluginAndGetEventBinding(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 					it("creates source wrapper with external dependencies", () => { | 
					
						
							|  |  |  | 						const source = env.eventBinding.handler("moduleSource()", env.chunk, "bar"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						source.should.be.instanceof(ConcatSource); | 
					
						
							| 
									
										
										
										
											2017-02-20 17:28:14 +08:00
										 |  |  | 						source.source().should.be.exactly("define([\"external-amd-module\",\"external-non-amd-module\",null], function(__WEBPACK_EXTERNAL_MODULE_module_1__, __WEBPACK_EXTERNAL_MODULE_module_2__, __WEBPACK_EXTERNAL_MODULE_module_3__) { return moduleSource()});"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 				describe("with only local dependencies", () => { | 
					
						
							|  |  |  | 					beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2017-11-16 01:43:19 +08:00
										 |  |  | 						const externalFlag = { | 
					
						
							|  |  |  | 							external: false | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 						const noExternals = env.modulesListWithExternals.map((module) => Object.assign({}, module, externalFlag)); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						env.chunk = { | 
					
						
							| 
									
										
										
										
											2017-11-16 01:43:19 +08:00
										 |  |  | 							getModules: () => noExternals | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						}; | 
					
						
							|  |  |  | 						env.eventBinding = setupPluginAndGetEventBinding(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 					it("creates source wrapper with callback only", () => { | 
					
						
							|  |  |  | 						const source = env.eventBinding.handler("moduleSource()", env.chunk, "bar"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						source.should.be.instanceof(ConcatSource); | 
					
						
							| 
									
										
										
										
											2017-02-20 17:26:18 +08:00
										 |  |  | 						source.source().should.be.exactly("define(function() { return moduleSource()});"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 		describe("main template event handlers", () => { | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 			beforeEach(() => | 
					
						
							|  |  |  | 				env.mainTemplateBindings = env.templatePlugin.getMainTemplateBindings()); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 			it("binds two handlers", () => env.mainTemplateBindings.length.should.be.exactly(2)); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 			describe("global-hash-paths handler", () => { | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 				beforeEach(() => env.mainTemplateBinding = env.mainTemplateBindings[0]); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 				it("binds to global-hash-paths event", () => | 
					
						
							|  |  |  | 					env.mainTemplateBinding.name.should.be.exactly("global-hash-paths")); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 				it("adds name to path array", () => | 
					
						
							|  |  |  | 					env.mainTemplateBinding.handler([]).should.deepEqual(["foo"])); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 			describe("hash handler", () => { | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 				beforeEach(() => env.mainTemplateBinding = env.mainTemplateBindings[1]); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 22:37:26 +08:00
										 |  |  | 				it("binds to hash event", () => env.mainTemplateBinding.name.should.be.exactly("hash")); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 21:53:42 +08:00
										 |  |  | 				it("updates hash", () => { | 
					
						
							|  |  |  | 					const hash = { | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 						update: sinon.spy() | 
					
						
							|  |  |  | 					}; | 
					
						
							|  |  |  | 					env.mainTemplateBinding.handler(hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					hash.update.callCount.should.be.exactly(2); | 
					
						
							|  |  |  | 					hash.update.firstCall.args[0].should.be.exactly("exports amd"); | 
					
						
							|  |  |  | 					hash.update.secondCall.args[0].should.be.exactly("foo"); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-20 17:26:18 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |