using System.Web.Mvc;
using NUnit.Framework;
using Umbraco.Web;
namespace Umbraco.Tests.Web.Mvc
{
[TestFixture]
public class HtmlHelperExtensionMethodsTests
{
private const string SampleWithAnchorElement = "Hello world, this is some text with a link";
private const string SampleWithBoldAndAnchorElements = "Hello world, this is some text with a link";
[SetUp]
public virtual void Initialize()
{
//create an empty htmlHelper
_htmlHelper = new HtmlHelper(new ViewContext(), new ViewPage());
}
private HtmlHelper _htmlHelper;
[Test]
public void Wrap_Simple()
{
var output = _htmlHelper.Wrap("div", "hello world");
Assert.AreEqual("
hello world
", output.ToHtmlString());
}
[Test]
public void Wrap_Object_Attributes()
{
var output = _htmlHelper.Wrap("div", "hello world", new {style = "color:red;", onclick = "void();"});
Assert.AreEqual("hello world
", output.ToHtmlString());
}
[Test]
public static void Truncate_Simple()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.Truncate(SampleWithAnchorElement, 25).ToString();
Assert.AreEqual("Hello world, this is some…", result);
}
[Test]
public static void When_Truncating_A_String_Ends_With_A_Space_We_Should_Trim_The_Space_Before_Appending_The_Ellipsis()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.Truncate(SampleWithAnchorElement, 26).ToString();
Assert.AreEqual("Hello world, this is some…", result);
}
[Test]
public static void Truncate_Inside_Word()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.Truncate(SampleWithAnchorElement, 24).ToString();
Assert.AreEqual("Hello world, this is som…", result);
}
[Test]
public static void Truncate_With_Tag()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.Truncate(SampleWithAnchorElement, 35).ToString();
Assert.AreEqual("Hello world, this is some text with…", result);
}
[Test]
public static void Truncate_By_Words()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.TruncateByWords(SampleWithAnchorElement, 4).ToString();
Assert.AreEqual("Hello world, this is…", result);
}
[Test]
public static void Truncate_By_Words_With_Tag()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.TruncateByWords(SampleWithBoldAndAnchorElements, 4).ToString();
Assert.AreEqual("Hello world, this is…", result);
}
[Test]
public static void Truncate_By_Words_Mid_Tag()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.TruncateByWords(SampleWithAnchorElement, 7).ToString();
Assert.AreEqual("Hello world, this is some text with…", result);
}
[Test]
public static void Strip_All_Html()
{
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.StripHtml(SampleWithBoldAndAnchorElements, null).ToString();
Assert.AreEqual("Hello world, this is some text with a link", result);
}
[Test]
public static void Strip_Specific_Html()
{
string[] tags = { "b" };
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.StripHtml(SampleWithBoldAndAnchorElements, tags).ToString();
Assert.AreEqual(SampleWithAnchorElement, result);
}
[Test]
public static void Strip_Invalid_Html()
{
const string text = "Hello world, is some text with a link";
var helper = new HtmlHelper(new ViewContext(), new ViewPage());
var result = helper.StripHtml(text).ToString();
Assert.AreEqual("Hello world, is some text with a link", result);
}
}
}