Skip to content

Commit cc50488

Browse files
Add test for query param handling in React Router
1 parent b62e50b commit cc50488

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

tests/React.Tests/Router/HtmlHelperExtensionsTest.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#if NET452
88

9+
using System.Collections.Specialized;
910
using System.Web;
1011
using System.Web.Mvc;
1112
using Moq;
@@ -64,20 +65,21 @@ class HtmlHelperMocks
6465
public Mock<HtmlHelper> htmlHelper;
6566
public Mock<HttpResponseBase> httpResponse;
6667

67-
public HtmlHelperMocks()
68+
public HtmlHelperMocks(HttpRequestBase request = null)
6869
{
6970
var viewDataContainer = new Mock<IViewDataContainer>();
7071
var viewContext = new Mock<ViewContext>();
7172
httpResponse = new Mock<HttpResponseBase>();
7273
htmlHelper = new Mock<HtmlHelper>(viewContext.Object, viewDataContainer.Object);
7374
var httpContextBase = new Mock<HttpContextBase>();
7475
viewContext.Setup(x => x.HttpContext).Returns(httpContextBase.Object);
76+
httpContextBase.Setup(x => x.Request).Returns(request);
7577
httpContextBase.Setup(x => x.Response).Returns(httpResponse.Object);
7678
}
7779
}
7880

7981
/// <summary>
80-
/// Mocks alot of common functionality related to rendering a
82+
/// Mocks alot of common functionality related to rendering a
8183
/// React Router component.
8284
/// </summary>
8385
class ReactRouterMocks
@@ -227,6 +229,46 @@ public void ShouldRunCustomContextHandler()
227229
htmlHelperMock.httpResponse.VerifySet(x => x.StatusCode = 200);
228230
}
229231

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+
230272
[Fact]
231273
public void ShouldRedirectPermanent()
232274
{

0 commit comments

Comments
 (0)