Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/React.Core/ReactComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ public virtual string RenderJavaScript()
/// <returns>JavaScript</returns>
public virtual void RenderJavaScript(TextWriter writer)
{
writer.Write(ClientOnly ? "ReactDOM.render(" : "ReactDOM.hydrate(");
writer.Write(
!_configuration.UseServerSideRendering || ClientOnly ? "ReactDOM.render(" : "ReactDOM.hydrate(");
WriteComponentInitialiser(writer);
writer.Write(", document.getElementById(\"");
writer.Write(ContainerId);
Expand Down
58 changes: 43 additions & 15 deletions tests/React.Tests/Core/ReactComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void RenderHtmlShouldThrowExceptionIfComponentDoesNotExist()
{
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(false);
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var reactIdGenerator = new Mock<IReactIdGenerator>();

Expand All @@ -38,7 +38,7 @@ public void RenderHtmlShouldCallRenderComponent()
{
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var reactIdGenerator = new Mock<IReactIdGenerator>();

Expand All @@ -58,7 +58,7 @@ public void RenderHtmlShouldWrapComponentInDiv()
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
environment.Setup(x => x.Execute<string>(@"ReactDOMServer.renderToString(React.createElement(Foo, {""hello"":""World""}))"))
.Returns("[HTML]");
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var reactIdGenerator = new Mock<IReactIdGenerator>();

Expand All @@ -78,7 +78,7 @@ public void RenderHtmlShouldNotRenderComponentHtml()
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
environment.Setup(x => x.Execute<string>(@"React.renderToString(React.createElement(Foo, {""hello"":""World""}))"))
.Returns("[HTML]");
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
var reactIdGenerator = new Mock<IReactIdGenerator>();

var component = new ReactComponent(environment.Object, config.Object, reactIdGenerator.Object, "Foo", "container")
Expand All @@ -96,7 +96,7 @@ public void RenderHtmlShouldNotRenderClientSideAttributes()
{
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var reactIdGenerator = new Mock<IReactIdGenerator>();

Expand All @@ -112,7 +112,7 @@ public void RenderHtmlShouldNotRenderClientSideAttributes()
[Fact]
public void RenderHtmlShouldWrapComponentInCustomElement()
{
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
Expand All @@ -133,7 +133,7 @@ public void RenderHtmlShouldWrapComponentInCustomElement()
[Fact]
public void RenderHtmlShouldNotRenderComponentWhenContainerOnly()
{
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
Expand All @@ -154,7 +154,7 @@ public void RenderHtmlShouldNotRenderComponentWhenContainerOnly()
[Fact]
public void RenderHtmlShouldNotWrapComponentWhenServerSideOnly()
{
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
Expand All @@ -174,7 +174,7 @@ public void RenderHtmlShouldNotWrapComponentWhenServerSideOnly()
[Fact]
public void RenderHtmlShouldAddClassToElement()
{
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var environment = new Mock<IReactEnvironment>();
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
Expand All @@ -197,7 +197,7 @@ public void RenderHtmlShouldAddClassToElement()
public void RenderJavaScriptShouldCallRenderComponent()
{
var environment = new Mock<IReactEnvironment>();
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
var reactIdGenerator = new Mock<IReactIdGenerator>();

var component = new ReactComponent(environment.Object, config.Object, reactIdGenerator.Object, "Foo", "container")
Expand All @@ -216,7 +216,7 @@ public void RenderJavaScriptShouldCallRenderComponent()
public void RenderJavaScriptShouldCallRenderComponentWithReactDOMRender()
{
var environment = new Mock<IReactEnvironment>();
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
var reactIdGenerator = new Mock<IReactIdGenerator>();

var component = new ReactComponent(environment.Object, config.Object, reactIdGenerator.Object, "Foo", "container")
Expand All @@ -236,7 +236,7 @@ public void RenderJavaScriptShouldCallRenderComponentWithReactDOMRender()
public void RenderJavaScriptShouldCallRenderComponentwithReactDOMHydrate()
{
var environment = new Mock<IReactEnvironment>();
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
var reactIdGenerator = new Mock<IReactIdGenerator>();

var component = new ReactComponent(environment.Object, config.Object, reactIdGenerator.Object, "Foo", "container")
Expand All @@ -252,6 +252,27 @@ public void RenderJavaScriptShouldCallRenderComponentwithReactDOMHydrate()
);
}

[Fact]
public void RenderJavaScriptShouldCallRenderComponentWithReactDomRenderWhenSsrDisabled()
{
var environment = new Mock<IReactEnvironment>();
var config = CreateDefaultConfigMock();
config.SetupGet(x => x.UseServerSideRendering).Returns(false);

var reactIdGenerator = new Mock<IReactIdGenerator>();
var component = new ReactComponent(environment.Object, config.Object, reactIdGenerator.Object, "Foo", "container")
{
ClientOnly = false,
Props = new {hello = "World"}
};
var result = component.RenderJavaScript();

Assert.Equal(
@"ReactDOM.render(React.createElement(Foo, {""hello"":""World""}), document.getElementById(""container""))",
result
);
}

[Theory]
[InlineData("Foo", true)]
[InlineData("Foo.Bar", true)]
Expand All @@ -278,7 +299,7 @@ public void TestEnsureComponentNameValid(string input, bool expected)
public void GeneratesContainerIdIfNotProvided()
{
var environment = new Mock<IReactEnvironment>();
var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
var reactIdGenerator = new Mock<IReactIdGenerator>();
reactIdGenerator.Setup(x => x.Generate()).Returns("customReactId");

Expand All @@ -294,7 +315,7 @@ public void ExceptionThrownIsHandled()
environment.Setup(x => x.Execute<string>(@"ReactDOMServer.renderToString(React.createElement(Foo, {""hello"":""World""}))"))
.Throws(new JsRuntimeException("'undefined' is not an object"));

var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.ExceptionHandler).Returns(() => throw new ReactServerRenderingException("test"));

Expand Down Expand Up @@ -347,7 +368,7 @@ public void RenderFunctionsCalled()
environment.Setup(x => x.Execute<string>(@"postrender();"))
.Returns("postrender-result");

var config = new Mock<IReactSiteConfiguration>();
var config = CreateDefaultConfigMock();
config.Setup(x => x.UseServerSideRendering).Returns(true);
var reactIdGenerator = new Mock<IReactIdGenerator>();

Expand Down Expand Up @@ -386,6 +407,13 @@ public void ChainedRenderFunctionsCalled()
Assert.Equal("postrender-result", firstInstance.PostRenderResult);
Assert.Equal("postrender-result", secondInstance.PostRenderResult);
}

private static Mock<IReactSiteConfiguration> CreateDefaultConfigMock()
{
var configMock = new Mock<IReactSiteConfiguration>();
configMock.SetupGet(x => x.UseServerSideRendering).Returns(true);
return configMock;
}

private sealed class TestRenderFunctions : RenderFunctionsBase
{
Expand Down