| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const should = require("should"); | 
					
						
							|  |  |  | const sinon = require("sinon"); | 
					
						
							|  |  |  | const ContextReplacementPlugin = require("../lib/ContextReplacementPlugin"); | 
					
						
							|  |  |  | const applyPluginWithOptions = require("./helpers/applyPluginWithOptions"); | 
					
						
							|  |  |  | const PluginEnvironment = require("./helpers/PluginEnvironment"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("ContextReplacementPlugin", () => { | 
					
						
							|  |  |  | 	it("has apply function", () => (new ContextReplacementPlugin()).apply.should.be.a.Function()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 	it("should consume resourceRegExp as regular expression", () => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 		let instance = new ContextReplacementPlugin(/selector/, "mock", "mock", "mock"); | 
					
						
							|  |  |  | 		should(instance.resourceRegExp instanceof RegExp).be.exactly(true); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 	it("should consume newContentResource as function", () => { | 
					
						
							|  |  |  | 		let instance = new ContextReplacementPlugin(/selector/, () => {}, "mock", "mock"); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 		should(instance.newContentCallback).be.a.Function(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 	it("should consume newContentResource as not an string or function", () => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 		let instance = new ContextReplacementPlugin(/selector/, 42, "newContentRecursive", "newContentRegExp"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		should(instance.resourceRegExp instanceof RegExp).be.exactly(true); | 
					
						
							|  |  |  | 		should(instance.newContentResource).be.exactly(undefined); | 
					
						
							|  |  |  | 		should(instance.newContentRecursive).be.exactly(undefined); | 
					
						
							|  |  |  | 		should(instance.newContentRegExp).be.exactly(42); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 	it("should consume newContentResource as an object", () => { | 
					
						
							|  |  |  | 		let instance = new ContextReplacementPlugin(/selector/, "newResource", { | 
					
						
							|  |  |  | 			test: "obj" | 
					
						
							|  |  |  | 		}, /selector/); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 		should(instance.resourceRegExp instanceof RegExp).be.exactly(true); | 
					
						
							|  |  |  | 		should(instance.newContentResource).be.exactly("newResource"); | 
					
						
							|  |  |  | 		should(instance.newContentRecursive).be.exactly(undefined); | 
					
						
							|  |  |  | 		should(instance.newContentRegExp).be.exactly(undefined); | 
					
						
							|  |  |  | 		should(instance.newContentCreateContextMap).be.a.Function(); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		let x = (nothing, obj) => { | 
					
						
							|  |  |  | 			should(obj.test).be.exactly("obj") | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		instance.newContentCreateContextMap(undefined, spy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 	it("should consume newContentResource as an object", () => { | 
					
						
							|  |  |  | 		let instance = new ContextReplacementPlugin(/selector/, "newResource", () => {}, /selector/); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		should(instance.resourceRegExp instanceof RegExp).be.exactly(true); | 
					
						
							|  |  |  | 		should(instance.newContentResource).be.exactly("newResource"); | 
					
						
							|  |  |  | 		should(instance.newContentRecursive).be.exactly(undefined); | 
					
						
							|  |  |  | 		should(instance.newContentRegExp).be.exactly(undefined); | 
					
						
							|  |  |  | 		should(instance.newContentCreateContextMap).be.a.Function(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 	describe("when applied", () => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 		describe("when before resolve is called", () => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 			it("default call", () => { | 
					
						
							|  |  |  | 				let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				let x = (nothing, result) => { | 
					
						
							|  |  |  | 					should(result.request).be.exactly('./folder') | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					should(result.dependencies[0].critical).be.exactly(false) | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 					should(result.recursive).be.exactly(true) | 
					
						
							|  |  |  | 					should(result.regExp instanceof RegExp).be.exactly(true) | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				obj.beforeResolve.handler({ | 
					
						
							|  |  |  | 					request: "selector", | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					dependencies: [{ | 
					
						
							|  |  |  | 						critical: true | 
					
						
							|  |  |  | 					}] | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 				}, spy) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it("default call with newContentCallback as a function", () => { | 
					
						
							|  |  |  | 				let obj = buildPluginWithParams(/selector/, (result) => { | 
					
						
							|  |  |  | 					should(result.request).be.exactly('selector') | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					should(result.dependencies[0].critical).be.exactly(false) | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 					should(result.recursive).be.exactly(undefined) | 
					
						
							|  |  |  | 					should(result.regExp).be.exactly(undefined) | 
					
						
							|  |  |  | 				}, true, /filter/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				let x = (nothing, result) => { | 
					
						
							|  |  |  | 					should(result.request).be.exactly('selector') | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					should(result.dependencies[0].critical).be.exactly(false) | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 					should(result.recursive).be.exactly(undefined) | 
					
						
							|  |  |  | 					should(result.regExp).be.exactly(undefined) | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				obj.beforeResolve.handler({ | 
					
						
							|  |  |  | 					request: "selector", | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					dependencies: [{ | 
					
						
							|  |  |  | 						critical: false | 
					
						
							|  |  |  | 					}] | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 				}, spy) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it("call when result is false", () => { | 
					
						
							|  |  |  | 				let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				let x = (nothing, result) => { | 
					
						
							|  |  |  | 					should(result).be.Undefined(); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				obj.beforeResolve.handler(false, spy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 		describe("when after resolve is called", () => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 			it("default call where regex is correct", () => { | 
					
						
							|  |  |  | 				let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				let x = (nothing, result) => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:42:30 +08:00
										 |  |  | 					result.resource.should.containEql('selector') | 
					
						
							|  |  |  | 					result.resource.should.containEql('folder') | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				obj.afterResolve.handler({ | 
					
						
							|  |  |  | 					resource: "selector", | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					dependencies: [{ | 
					
						
							|  |  |  | 						critical: true | 
					
						
							|  |  |  | 					}] | 
					
						
							|  |  |  | 				}, spy); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it("default call where regex is incorrect", () => { | 
					
						
							|  |  |  | 				let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				let x = (nothing, result) => { | 
					
						
							|  |  |  | 					result.resource.should.containEql('importwontwork') | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				obj.afterResolve.handler({ | 
					
						
							|  |  |  | 					resource: "importwontwork", | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					dependencies: [{ | 
					
						
							|  |  |  | 						critical: true | 
					
						
							|  |  |  | 					}] | 
					
						
							|  |  |  | 				}, spy); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it("default call where regex is correct", () => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				let obj = buildPluginWithParams(/selector/, (result) => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 					//noop
 | 
					
						
							|  |  |  | 				}, true, /filter/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				let x = (nothing, result) => { | 
					
						
							|  |  |  | 					result.resource.should.equal('selector') | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				obj.afterResolve.handler({ | 
					
						
							|  |  |  | 					resource: "selector", | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					dependencies: [{ | 
					
						
							|  |  |  | 						critical: true | 
					
						
							|  |  |  | 					}] | 
					
						
							|  |  |  | 				}, spy); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 03:16:30 +08:00
										 |  |  | 			it("default call where regex is correct and using function as newContent Resource", () => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				let obj = buildPluginWithParams(/selector/, (result) => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 					result.resource = "imadifferentselector" | 
					
						
							|  |  |  | 				}, true, /filter/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				let x = (nothing, result) => { | 
					
						
							| 
									
										
										
										
											2017-02-21 03:42:30 +08:00
										 |  |  | 					result.resource.should.containEql('selector') | 
					
						
							|  |  |  | 					result.resource.should.containEql('imadifferentselector') | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				let spy = sinon.spy(x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				obj.afterResolve.handler({ | 
					
						
							|  |  |  | 					resource: "selector", | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | 					dependencies: [{ | 
					
						
							|  |  |  | 						critical: true | 
					
						
							|  |  |  | 					}] | 
					
						
							|  |  |  | 				}, spy); | 
					
						
							| 
									
										
										
										
											2017-02-21 03:14:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				should(spy.called).be.exactly(true) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let buildPluginWithParams = (resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) => { | 
					
						
							|  |  |  | 	let instance = new ContextReplacementPlugin(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let pluginEnvironment = new PluginEnvironment(); | 
					
						
							|  |  |  | 	instance.apply(pluginEnvironment.getEnvironmentStub()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let contextModuleFactory = pluginEnvironment.getEventBindings()[0]; | 
					
						
							|  |  |  | 	pluginEnvironment.getEventBindings().length.should.be.exactly(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let contextModuleFactoryPluginEnv = new PluginEnvironment(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	contextModuleFactory.handler(contextModuleFactoryPluginEnv.getEnvironmentStub()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let contextModuleFactoryEventBindings = contextModuleFactoryPluginEnv.getEventBindings(); | 
					
						
							|  |  |  | 	contextModuleFactoryPluginEnv.getEventBindings().length.should.be.exactly(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let beforeResolve = contextModuleFactoryEventBindings[0]; | 
					
						
							|  |  |  | 	let afterResolve = contextModuleFactoryEventBindings[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		contextModuleFactory, | 
					
						
							|  |  |  | 		beforeResolve, | 
					
						
							|  |  |  | 		afterResolve | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-21 03:31:59 +08:00
										 |  |  | }; |