Skip to content
Prev Previous commit
Next Next commit
Update tests
  • Loading branch information
Gustav Tonér committed Jan 16, 2018
commit 6f1f987184a6b2b8a34ec47f2491b705c4825355
13 changes: 12 additions & 1 deletion tests/React.Tests/Core/ReactEnvironmentTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2014-Present, Facebook, Inc.
* All rights reserved.
*
Expand Down Expand Up @@ -137,6 +137,17 @@ public void CreatesIReactComponent()
Assert.Equal(";\r\n", environment.GetInitJavaScript());
}

[Fact]
public void ServerSideOnlyComponentRendersNoJavaScript()
{
var mocks = new Mocks();
var environment = mocks.CreateReactEnvironment();

environment.CreateComponent("HelloWorld", new { name = "Daniel" }, serverOnly: true);

Assert.Equal(string.Empty, environment.GetInitJavaScript());
}

public class Mocks
{
public Mock<PooledJsEngine> Engine { get; private set; }
Expand Down
244 changes: 124 additions & 120 deletions tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,123 +7,127 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

using Moq;
using React.Web.Mvc;
using Xunit;

namespace React.Tests.Mvc
{
public class HtmlHelperExtensionsTests
{
/// <summary>
/// Creates a mock <see cref="IReactEnvironment"/> and registers it with the IoC container
/// This is only required because <see cref="HtmlHelperExtensions"/> can not be
/// injected :(
/// </summary>
private Mock<IReactEnvironment> ConfigureMockEnvironment()
{
var environment = new Mock<IReactEnvironment>();
AssemblyRegistration.Container.Register(environment.Object);
return environment;
}

[Fact]
public void ReactWithInitShouldReturnHtmlAndScript()
{
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(false, false, null)).Returns("HTML");
component.Setup(x => x.RenderJavaScript()).Returns("JS");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new {},
null,
false
)).Returns(component.Object);

var result = HtmlHelperExtensions.ReactWithInit(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span"
);
Assert.Equal(
"HTML" + System.Environment.NewLine + "<script>JS</script>",
result.ToString()
);
}

[Fact]
public void EngineIsReturnedToPoolAfterRender()
{
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(true, true, null)).Returns("HTML");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new { },
null,
true
)).Returns(component.Object);

environment.Verify(x => x.ReturnEngineToPool(), Times.Never);
var result = HtmlHelperExtensions.React(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span",
clientOnly: true,
serverOnly: true
);
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == true), null), Times.Once);
environment.Verify(x => x.ReturnEngineToPool(), Times.Once);
}

[Fact]
public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
{
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(true, true, null)).Returns("HTML");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new {},
null,
true
)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span",
clientOnly: true,
serverOnly: true
);
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == true), null), Times.Once);
}

[Fact]
public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue() {
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(true, true, null)).Returns("HTML");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new { },
null,
true
)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span",
clientOnly: true,
serverOnly: true
);
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == true), null), Times.Once);
}
}
}
using Moq;
using Xunit;
using React.Web.Mvc;

namespace React.Tests.Mvc
{
public class HtmlHelperExtensionsTests
{
/// <summary>
/// Creates a mock <see cref="IReactEnvironment"/> and registers it with the IoC container
/// This is only required because <see cref="HtmlHelperExtensions"/> can not be
/// injected :(
/// </summary>
private Mock<IReactEnvironment> ConfigureMockEnvironment()
{
var environment = new Mock<IReactEnvironment>();
AssemblyRegistration.Container.Register(environment.Object);
return environment;
}

[Fact]
public void ReactWithInitShouldReturnHtmlAndScript()
{
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(false, false, null)).Returns("HTML");
component.Setup(x => x.RenderJavaScript()).Returns("JS");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new {},
null,
false,
false
)).Returns(component.Object);

var result = HtmlHelperExtensions.ReactWithInit(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span"
);
Assert.Equal(
"HTML" + System.Environment.NewLine + "<script>JS</script>",
result.ToString()
);
}

[Fact]
public void EngineIsReturnedToPoolAfterRender()
{
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(true, true, null)).Returns("HTML");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new { },
null,
true,
false
)).Returns(component.Object);

environment.Verify(x => x.ReturnEngineToPool(), Times.Never);
var result = HtmlHelperExtensions.React(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span",
clientOnly: true,
serverOnly: false
);
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == false), null), Times.Once);
environment.Verify(x => x.ReturnEngineToPool(), Times.Once);
}

[Fact]
public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
{
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(true, true, null)).Returns("HTML");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new {},
null,
true,
false
)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span",
clientOnly: true,
serverOnly: false
);
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true), It.Is<bool>(z => z == false), null), Times.Once);
}

[Fact]
public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue() {
var component = new Mock<IReactComponent>();
component.Setup(x => x.RenderHtml(false, true, null)).Returns("HTML");
var environment = ConfigureMockEnvironment();
environment.Setup(x => x.CreateComponent(
"ComponentName",
new { },
null,
false,
true
)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
htmlHelper: null,
componentName: "ComponentName",
props: new { },
htmlTag: "span",
clientOnly: false,
serverOnly: true
);
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == false), It.Is<bool>(z => z == true), null), Times.Once);
}
}
}