public void ExecuteWithLargerStackIfRequiredWithNoNewThread() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); environment.ExecuteWithLargerStackIfRequired<int>("foo"); mocks.Engine.Verify(x => x.CallFunction<int>("foo"), Times.Exactly(1)); }
public void ExecuteWithLargerStackIfRequiredWithNoNewThread() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); environment.ExecuteWithLargerStackIfRequired<int>("1+1"); mocks.Engine.Verify(x => x.Evaluate<int>("1+1")); }
public void ShouldNotTransformJsxIfNoAnnotationPresent() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); var input = "<div>Hello World</div>"; var output = environment.TransformJsx(input); Assert.Equal(input, output); }
public void GeneratesContainerIdIfNotProvided() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); mocks.Config.Setup(x => x.Scripts).Returns(new List<string>()); var component1 = environment.CreateComponent("ComponentName", new { }); var component2 = environment.CreateComponent("ComponentName", new { }); Assert.AreEqual("react1", component1.ContainerId); Assert.AreEqual("react2", component2.ContainerId); }
public void ExecuteWithBabelShouldThrowIfBabelDisabled() { var mocks = new Mocks(); mocks.Config.Setup(x => x.LoadBabel).Returns(false); var environment = mocks.CreateReactEnvironment(); Assert.Throws<BabelNotLoadedException>(() => { environment.ExecuteWithBabel<string>("foobar"); }); }
public void ShouldTransformJsxIfAnnotationPresent() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); const string input = "/** @jsx React.DOM */ <div>Hello World</div>"; environment.TransformJsx(input); mocks.Engine.Verify(x => x.Evaluate<string>( @"global.JSXTransformer.transform(""/** @jsx React.DOM */ <div>Hello World</div>"").code" )); }
public void ExecuteWithLargerStackIfRequiredShouldBubbleExceptions() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); // Always fail mocks.Engine.Setup(x => x.CallFunction<int>("foobar")) .Throws(new Exception("Something bad happened :(")); Assert.Throws<Exception>(() => { environment.ExecuteWithLargerStackIfRequired<int>("foobar"); }); }
public void ExecuteWithBabelShouldBubbleExceptions() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); // Always fail mocks.Engine.Setup(x => x.CallFunction <int>("foobar")) .Throws(new Exception("Something bad happened :(")); Assert.Throws <Exception>(() => { environment.ExecuteWithBabel <int>("foobar"); }); }
public void ReturnsEngineToPool() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); mocks.Config.Setup(x => x.ReuseJavaScriptEngines).Returns(true); environment.CreateComponent("ComponentName", new { }); mocks.EngineFactory.Verify(x => x.GetEngine(), Times.Once); environment.ReturnEngineToPool(); environment.CreateComponent("ComponentName", new { }); mocks.EngineFactory.Verify(x => x.GetEngine(), Times.AtLeast(2)); }
public void StyleTagsReturned() { var mocks = new Mocks(); mocks.Config.SetupGet(x => x.ReactAppBuildPath).Returns("~/dist"); mocks.FileSystem.Setup(x => x.ReadAsString("~/dist/asset-manifest.json")).Returns(_testAppManifest); var environment = mocks.CreateReactEnvironment(); var styles = environment.GetStylePaths().ToList(); Assert.Equal(2, styles.Count); Assert.Equal("static/css/main.43b75f57.chunk.css", styles[0]); Assert.Equal("static/css/another-stylesheet.css", styles[1]); }
public void ScriptTagsReturned() { var mocks = new Mocks(); mocks.Config.SetupGet(x => x.ReactAppBuildPath).Returns("~/dist"); mocks.FileSystem.Setup(x => x.ReadAsString("~/dist/asset-manifest.json")).Returns(_testAppManifest); var environment = mocks.CreateReactEnvironment(); var scripts = environment.GetScriptPaths().ToList(); Assert.Equal(2, scripts.Count); Assert.Equal("static/js/runtime-main.62ca1b0d.js", scripts[0]); Assert.Equal("static/js/main.04394e4f.chunk.js", scripts[1]); }
public void UsesProvidedContainerId() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); mocks.Config.Setup(x => x.Scripts).Returns(new List <string>()); mocks.ReactIdGenerator.Setup(x => x.Generate()).Returns("react_customId"); var component1 = environment.CreateComponent("ComponentName", new { }, "foo"); var component2 = environment.CreateComponent("ComponentName", new { }); Assert.Equal("foo", component1.ContainerId); Assert.Equal("react_customId", component2.ContainerId); }
public void SSRInitSkippedIfNoComponents(bool renderComponent, int ssrTimes) { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); if (renderComponent) { environment.CreateComponent("HelloWorld", new { name = "Daniel" }, clientOnly: true).RenderHtml(); } environment.GetInitJavaScript(); mocks.Engine.Verify(x => x.Evaluate <string>("console.getCalls()"), Times.Exactly(ssrTimes)); }
public void ExecuteWithLargerStackIfRequiredWithNewThread() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); // Fail the first time Evaluate is called, succeed the second // http://stackoverflow.com/a/7045636 mocks.Engine.Setup(x => x.CallFunction<int>("foo")) .Callback(() => mocks.Engine.Setup(x => x.CallFunction<int>("foo"))) .Throws(new Exception("Out of stack space")); environment.ExecuteWithLargerStackIfRequired<int>("foo"); mocks.EngineFactory.Verify( x => x.GetEngineForCurrentThread(It.IsAny<Action<IJsEngine>>()), Times.Exactly(2), "Two engines should be created (initial thread and new thread)" ); mocks.EngineFactory.Verify( x => x.DisposeEngineForCurrentThread(), Times.Exactly(1), "Inner engine should be disposed" ); }
public void ExecuteWithBabelWithNewThread() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); // Fail the first time Evaluate is called, succeed the second // http://stackoverflow.com/a/7045636 mocks.Engine.Setup(x => x.CallFunction <int>("foo")) .Callback(() => mocks.Engine.Setup(x => x.CallFunction <int>("foo"))) .Throws(new Exception("Out of stack space")); environment.ExecuteWithBabel <int>("foo"); mocks.EngineFactory.Verify( x => x.GetEngineForCurrentThread(), Times.Exactly(2), "Two engines should be created (initial thread and new thread)" ); mocks.EngineFactory.Verify( x => x.DisposeEngineForCurrentThread(), Times.Exactly(1), "Inner engine should be disposed" ); }
public void UsesProvidedContainerId() { var mocks = new Mocks(); var environment = mocks.CreateReactEnvironment(); mocks.Config.Setup(x => x.Scripts).Returns(new List<string>()); var component1 = environment.CreateComponent("ComponentName", new { }, "foo"); var component2 = environment.CreateComponent("ComponentName", new { }); Assert.AreEqual("foo", component1.ContainerId); StringAssert.StartsWith("react_", component2.ContainerId); }