|
| 1 | +using Moq; |
| 2 | +using React.Exceptions; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace React.Tests.Core |
| 6 | +{ |
| 7 | + public class ReactComponentTest |
| 8 | + { |
| 9 | + [Fact] |
| 10 | + public void RenderHtmlShouldThrowExceptionIfComponentDoesNotExist() |
| 11 | + { |
| 12 | + var environment = new Mock<IReactEnvironment>(); |
| 13 | + environment.Setup(x => x.HasVariable("Foo")).Returns(false); |
| 14 | + var component = new ReactComponent(environment.Object, "Foo", "container"); |
| 15 | + |
| 16 | + Assert.Throws<ReactInvalidComponentException>(() => |
| 17 | + { |
| 18 | + component.RenderHtml(); |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void RenderHtmlShouldCallRenderComponent() |
| 24 | + { |
| 25 | + var environment = new Mock<IReactEnvironment>(); |
| 26 | + environment.Setup(x => x.HasVariable("Foo")).Returns(true); |
| 27 | + |
| 28 | + var component = new ReactComponent(environment.Object, "Foo", "container") |
| 29 | + { |
| 30 | + Props = new { hello = "World" } |
| 31 | + }; |
| 32 | + component.RenderHtml(); |
| 33 | + |
| 34 | + environment.Verify(x => x.Execute<string>(@"React.renderComponentToString(Foo({""hello"":""World""}))")); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void RenderHtmlShouldWrapComponentInDiv() |
| 39 | + { |
| 40 | + var environment = new Mock<IReactEnvironment>(); |
| 41 | + environment.Setup(x => x.HasVariable("Foo")).Returns(true); |
| 42 | + environment.Setup(x => x.Execute<string>(@"React.renderComponentToString(Foo({""hello"":""World""}))")) |
| 43 | + .Returns("[HTML]"); |
| 44 | + |
| 45 | + var component = new ReactComponent(environment.Object, "Foo", "container") |
| 46 | + { |
| 47 | + Props = new { hello = "World" } |
| 48 | + }; |
| 49 | + var result = component.RenderHtml(); |
| 50 | + |
| 51 | + Assert.Equal(@"<div id=""container"">[HTML]</div>", result); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void RenderJavaScriptShouldCallRenderComponent() |
| 56 | + { |
| 57 | + var environment = new Mock<IReactEnvironment>(); |
| 58 | + |
| 59 | + var component = new ReactComponent(environment.Object, "Foo", "container") |
| 60 | + { |
| 61 | + Props = new { hello = "World" } |
| 62 | + }; |
| 63 | + var result = component.RenderJavaScript(); |
| 64 | + |
| 65 | + Assert.Equal( |
| 66 | + @"React.renderComponent(Foo({""hello"":""World""}), document.getElementById(""container""))", |
| 67 | + result |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments