Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Add Html.Partial - sync versions of Html.PartialAsync
Browse files Browse the repository at this point in the history
Fixes #1107
  • Loading branch information
pranavkm committed Oct 14, 2014
1 parent d9ebb37 commit 26b9ca4
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,98 @@ public static class HtmlHelperPartialExtensions
return htmlHelper.PartialAsync(partialViewName, model, viewData: null);
}

/// <summary>
/// Returns HTML markup for the specified partial view.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
/// <param name="partialViewName">
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>.
/// </param>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName)
{
return htmlHelper.Partial(partialViewName, htmlHelper.ViewData.Model, viewData: null);
}

/// <summary>
/// Returns HTML markup for the specified partial view.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
/// <param name="partialViewName">
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>.
/// </param>
/// <param name="viewData">A <see cref="ViewDataDictionary"/> to pass into the partial view.</param>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName,
ViewDataDictionary viewData)
{
return htmlHelper.Partial(partialViewName, htmlHelper.ViewData.Model, viewData: viewData);
}

/// <summary>
/// Returns HTML markup for the specified partial view.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance this method extends.</param>
/// <param name="partialViewName">
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>.
/// </param>
/// <param name="model">A model to pass into the partial view.</param>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName,
object model)
{
return htmlHelper.Partial(partialViewName, model, viewData: null);
}

/// <summary>
/// Returns HTML markup for the specified partial view.
/// </summary>
/// <param name="partialViewName">
/// The name of the partial view used to create the HTML markup. Must not be <c>null</c>.
/// </param>
/// <param name="model">A model to pass into the partial view.</param>
/// <param name="viewData">A <see cref="ViewDataDictionary"/> to pass into the partial view.</param>
/// <returns>
/// Returns a new <see cref="HtmlString"/> containing the created HTML.
/// </returns>
/// <remarks>
/// This method synchronously calls and blocks on
/// <see cref="IHtmlHelper.PartialAsync(string, object, ViewDataDictionary)"/>
/// </remarks>
public static HtmlString Partial(
[NotNull] this IHtmlHelper htmlHelper,
[NotNull] string partialViewName,
object model,
ViewDataDictionary viewData)
{
var result = htmlHelper.PartialAsync(partialViewName, model, viewData: viewData);
return TaskHelper.WaitAndThrowIfFaulted(result);
}

/// <summary>
/// Renders HTML markup for the specified partial view.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.ModelBinding;
using Moq;
using Xunit;

namespace Microsoft.AspNet.Mvc.Rendering
{
public class HtmlHelperPartialExtensionsTest
{
public static TheoryData PartialExtensionMethods
{
get
{
var vdd = new ViewDataDictionary(new EmptyModelMetadataProvider());
var data = new TheoryData<Func<IHtmlHelper, HtmlString>>();
data.Add(helper => helper.Partial("test"));
data.Add(helper => helper.Partial("test", new object()));
data.Add(helper => helper.Partial("test", vdd));
data.Add(helper => helper.Partial("test", new object(), vdd));

return data;
}
}

[Theory]
[MemberData(nameof(PartialExtensionMethods))]
public void PartialMethods_DoesNotWrapThrownException(Func<IHtmlHelper, HtmlString> partialMethod)
{
// Arrange
var expected = new InvalidOperationException();
var helper = new Mock<IHtmlHelper>();
helper.Setup(h => h.PartialAsync("test", It.IsAny<object>(), It.IsAny<ViewDataDictionary>()))
.Callback(() =>
{
helper.ToString();
throw expected;
});
helper.SetupGet(h => h.ViewData)
.Returns(new ViewDataDictionary(new EmptyModelMetadataProvider()));

// Act and Assert
var actual = Assert.Throws<InvalidOperationException>(() => partialMethod(helper.Object));
Assert.Same(expected, actual);
}

[Fact]
public void Partial_InvokesPartialAsyncWithCurrentModel()
{
// Arrange
var expected = new HtmlString("value");
var model = new object();
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider())
{
Model = model
};
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", model, null))
.Returns(Task.FromResult(expected))
.Verifiable();
helper.SetupGet(h => h.ViewData)
.Returns(viewData);

// Act
var actual = helper.Object.Partial("test");

// Assert
Assert.Same(expected, actual);
helper.Verify();
}

[Fact]
public void PartialWithModel_InvokesPartialAsyncWithPassedInModel()
{
// Arrange
var expected = new HtmlString("value");
var model = new object();
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", model, null))
.Returns(Task.FromResult(expected))
.Verifiable();

// Act
var actual = helper.Object.Partial("test", model);

// Assert
Assert.Same(expected, actual);
helper.Verify();
}

[Fact]
public void PartialWithViewData_InvokesPartialAsyncWithPassedInViewData()
{
// Arrange
var expected = new HtmlString("value");
var model = new object();
var passedInViewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider())
{
Model = model
};
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", model, passedInViewData))
.Returns(Task.FromResult(expected))
.Verifiable();
helper.SetupGet(h => h.ViewData)
.Returns(viewData);

// Act
var actual = helper.Object.Partial("test", passedInViewData);

// Assert
Assert.Same(expected, actual);
helper.Verify();
}

[Fact]
public void PartialWithViewDataAndModel_InvokesPartialAsyncWithPassedInViewDataAndModel()
{
// Arrange
var expected = new HtmlString("value");
var passedInModel = new object();
var passedInViewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var helper = new Mock<IHtmlHelper>(MockBehavior.Strict);
helper.Setup(h => h.PartialAsync("test", passedInModel, passedInViewData))
.Returns(Task.FromResult(expected))
.Verifiable();

// Act
var actual = helper.Object.Partial("test", passedInModel, passedInViewData);

// Assert
Assert.Same(expected, actual);
helper.Verify();
}
}
}

0 comments on commit 26b9ca4

Please sign in to comment.