|
6 | 6 | */ |
7 | 7 | #if NET452 |
8 | 8 |
|
| 9 | +using System.Collections.Specialized; |
9 | 10 | using System.Web; |
10 | 11 | using System.Web.Mvc; |
11 | 12 | using Moq; |
@@ -64,20 +65,21 @@ class HtmlHelperMocks |
64 | 65 | public Mock<HtmlHelper> htmlHelper; |
65 | 66 | public Mock<HttpResponseBase> httpResponse; |
66 | 67 |
|
67 | | - public HtmlHelperMocks() |
| 68 | + public HtmlHelperMocks(HttpRequestBase request = null) |
68 | 69 | { |
69 | 70 | var viewDataContainer = new Mock<IViewDataContainer>(); |
70 | 71 | var viewContext = new Mock<ViewContext>(); |
71 | 72 | httpResponse = new Mock<HttpResponseBase>(); |
72 | 73 | htmlHelper = new Mock<HtmlHelper>(viewContext.Object, viewDataContainer.Object); |
73 | 74 | var httpContextBase = new Mock<HttpContextBase>(); |
74 | 75 | viewContext.Setup(x => x.HttpContext).Returns(httpContextBase.Object); |
| 76 | + httpContextBase.Setup(x => x.Request).Returns(request); |
75 | 77 | httpContextBase.Setup(x => x.Response).Returns(httpResponse.Object); |
76 | 78 | } |
77 | 79 | } |
78 | 80 |
|
79 | 81 | /// <summary> |
80 | | - /// Mocks alot of common functionality related to rendering a |
| 82 | + /// Mocks alot of common functionality related to rendering a |
81 | 83 | /// React Router component. |
82 | 84 | /// </summary> |
83 | 85 | class ReactRouterMocks |
@@ -227,6 +229,46 @@ public void ShouldRunCustomContextHandler() |
227 | 229 | htmlHelperMock.httpResponse.VerifySet(x => x.StatusCode = 200); |
228 | 230 | } |
229 | 231 |
|
| 232 | + [Theory] |
| 233 | + [InlineData(false)] |
| 234 | + [InlineData(true)] |
| 235 | + public void ShouldHandleQueryParams(bool withOverriddenPath) |
| 236 | + { |
| 237 | + var mocks = ConfigureMockReactEnvironment(); |
| 238 | + ConfigureMockConfiguration(); |
| 239 | + ConfigureReactIdGenerator(); |
| 240 | + |
| 241 | + mocks.Engine.Setup(x => x.Evaluate<string>("JSON.stringify(context);")) |
| 242 | + .Returns("{ status: 200 }"); |
| 243 | + |
| 244 | + var requestMock = new Mock<HttpRequestBase>(); |
| 245 | + requestMock.SetupGet(x => x.Path).Returns("/test"); |
| 246 | + var queryStringMock = new Mock<NameValueCollection>(); |
| 247 | + queryStringMock.Setup(x => x.ToString()).Returns("?a=1&b=2"); |
| 248 | + requestMock.SetupGet(x => x.QueryString).Returns(queryStringMock.Object); |
| 249 | + |
| 250 | + var htmlHelperMock = new HtmlHelperMocks(requestMock.Object); |
| 251 | + |
| 252 | + var result = HtmlHelperExtensions.ReactRouter( |
| 253 | + htmlHelper: htmlHelperMock.htmlHelper.Object, |
| 254 | + componentName: "ComponentName", |
| 255 | + props: new { }, |
| 256 | + path: withOverriddenPath ? "/test2?b=1&c=2" : null, |
| 257 | + contextHandler: (response, context) => response.StatusCode = context.status.Value |
| 258 | + ); |
| 259 | + |
| 260 | + htmlHelperMock.httpResponse.VerifySet(x => x.StatusCode = 200); |
| 261 | + |
| 262 | + if (withOverriddenPath) |
| 263 | + { |
| 264 | + mocks.Engine.Verify(x => x.Evaluate<string>(@"ReactDOMServer.renderToString(React.createElement(ComponentName, Object.assign({}, { location: '/test2?b=1&c=2', context: context })))")); |
| 265 | + } |
| 266 | + else |
| 267 | + { |
| 268 | + mocks.Engine.Verify(x => x.Evaluate<string>(@"ReactDOMServer.renderToString(React.createElement(ComponentName, Object.assign({}, { location: '/test?a=1&b=2', context: context })))")); |
| 269 | + } |
| 270 | + } |
| 271 | + |
230 | 272 | [Fact] |
231 | 273 | public void ShouldRedirectPermanent() |
232 | 274 | { |
|
0 commit comments