|
1 |
| -/* |
| 1 | +/* |
2 | 2 | * Copyright (c) 2014-Present, Facebook, Inc.
|
3 | 3 | * All rights reserved.
|
4 | 4 | *
|
5 | 5 | * This source code is licensed under the BSD-style license found in the
|
6 | 6 | * LICENSE file in the root directory of this source tree. An additional grant
|
7 | 7 | * of patent rights can be found in the PATENTS file in the same directory.
|
8 |
| - */ |
9 |
| - |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using JavaScriptEngineSwitcher.Core; |
10 | 12 | using Moq;
|
11 |
| -using Xunit; |
12 | 13 | using React.Exceptions;
|
13 |
| - |
| 14 | +using Xunit; |
| 15 | + |
14 | 16 | namespace React.Tests.Core
|
15 | 17 | {
|
16 | 18 | public class ReactComponentTest
|
@@ -190,7 +192,44 @@ public void GeneratesContainerIdIfNotProvided()
|
190 | 192 |
|
191 | 193 | var component = new ReactComponent(environment.Object, config.Object, "Foo", null);
|
192 | 194 | Assert.StartsWith("react_", component.ContainerId);
|
193 |
| - } |
| 195 | + } |
| 196 | + |
| 197 | + [Fact] |
| 198 | + public void ExceptionThrownIsHandled() |
| 199 | + { |
| 200 | + var environment = new Mock<IReactEnvironment>(); |
| 201 | + environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true); |
| 202 | + environment.Setup(x => x.Execute<string>(@"ReactDOMServer.renderToString(React.createElement(Foo, {""hello"":""World""}))")) |
| 203 | + .Throws(new JsRuntimeException("'undefined' is not an object")); |
| 204 | + |
| 205 | + var config = new Mock<IReactSiteConfiguration>(); |
| 206 | + config.Setup(x => x.UseServerSideRendering).Returns(true); |
194 | 207 |
|
| 208 | + var component = new ReactComponent(environment.Object, config.Object, "Foo", "container") |
| 209 | + { |
| 210 | + Props = new { hello = "World" } |
| 211 | + }; |
| 212 | + |
| 213 | + // Default behavior |
| 214 | + bool exceptionCaught = false; |
| 215 | + try |
| 216 | + { |
| 217 | + component.RenderHtml(); |
| 218 | + } |
| 219 | + catch (ReactServerRenderingException) |
| 220 | + { |
| 221 | + exceptionCaught = true; |
| 222 | + } |
| 223 | + |
| 224 | + Assert.True(exceptionCaught); |
| 225 | + |
| 226 | + // Custom exception handler set |
| 227 | + Exception caughtException = null; |
| 228 | + config.Setup(x => x.ExceptionHandler).Returns(ex => caughtException = ex); |
| 229 | + |
| 230 | + var result = component.RenderHtml(); |
| 231 | + Assert.Equal(@"<div id=""container""></div>", result); |
| 232 | + Assert.NotNull(caughtException); |
| 233 | + } |
195 | 234 | }
|
196 | 235 | }
|
0 commit comments