| 
									
										
										
										
											2017-02-10 06:39:59 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | const should = require("should"); | 
					
						
							|  |  |  | const sinon = require("sinon"); | 
					
						
							|  |  |  | const TemplatePluginEnvironment = require("./helpers/TemplatePluginEnvironment"); | 
					
						
							|  |  |  | const ConcatSource = require("webpack-sources").ConcatSource; | 
					
						
							|  |  |  | const JsonpExportMainTemplatePlugin = require("../lib/JsonpExportMainTemplatePlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("JsonpExportMainTemplatePlugin", () => { | 
					
						
							|  |  |  | 	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-18 18:59:31 +08:00
										 |  |  | 		return templatePluginEnvironment; | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 	beforeEach(() => env = {}); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 	it("has apply function", () => (new JsonpExportMainTemplatePlugin()).apply.should.be.a.Function()); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 	describe("when applied", () => { | 
					
						
							|  |  |  | 		beforeEach(() => | 
					
						
							|  |  |  | 			env.templatePlugin = applyTemplatePluginWithOptions(JsonpExportMainTemplatePlugin, "foo")); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 		describe("event handlers", () => { | 
					
						
							|  |  |  | 			beforeEach(() => env.eventBindings = env.templatePlugin.getEventBindings()); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 			it("binds one handlers", () => env.eventBindings.length.should.be.exactly(1)); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 			describe("render-with-entry handler", () => { | 
					
						
							|  |  |  | 				beforeEach(() => env.eventBinding = env.eventBindings[0]); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +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-18 18:59:31 +08:00
										 |  |  | 				it("creates source wrapper calling JSONP global callback", () => { | 
					
						
							|  |  |  | 					const source = env.eventBinding.handler("moduleSource()", env.chunk, "bar"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 					source.should.be.instanceof(ConcatSource); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 					source.source().should.be.exactly("templateName(moduleSource());"); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 		describe("main template event handlers", () => { | 
					
						
							|  |  |  | 			beforeEach(() => env.mainTemplateBindings = env.templatePlugin.getMainTemplateBindings()); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 			it("binds two handlers", () => env.mainTemplateBindings.length.should.be.exactly(2)); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 			describe("global-hash-paths handler", () => { | 
					
						
							|  |  |  | 				beforeEach(() => env.mainTemplateBinding = env.mainTemplateBindings[0]); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +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-18 18:59:31 +08:00
										 |  |  | 				it("adds name to path array", () => env.mainTemplateBinding.handler([]).should.deepEqual(["foo"])); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 			describe("hash handler", () => { | 
					
						
							|  |  |  | 				beforeEach(() => env.mainTemplateBinding = env.mainTemplateBindings[1]); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 				it("binds to hash event", () => env.mainTemplateBinding.name.should.be.exactly("hash")); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +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("jsonp export"); | 
					
						
							|  |  |  | 					hash.update.secondCall.args[0].should.be.exactly("foo"); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 18:59:31 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-08 08:05:19 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |