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

Commit 917d849

Browse files
author
NTaylorMullen
committed
Add AnchorTagHelper tests.
- Fixed a typo in resource string.
1 parent d52f64f commit 917d849

File tree

4 files changed

+233
-18
lines changed

4 files changed

+233
-18
lines changed

src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
134134
// Restores bound HTML attributes when we detect that a user is using the AnchorTagHelper as a normal <a> tag.
135135
private void RestoreBoundHtmlAttributes(TagHelperContext context, TagHelperOutput output)
136136
{
137-
if (Action != null)
138-
{
139-
output.RestoreBoundHtmlAttribute(nameof(Action), context);
140-
}
141-
142-
if (Controller != null)
143-
{
144-
output.RestoreBoundHtmlAttribute(nameof(Controller), context);
145-
}
146-
147137
if (Protocol != null)
148138
{
149139
output.RestoreBoundHtmlAttribute(nameof(Protocol), context);
@@ -158,11 +148,6 @@ private void RestoreBoundHtmlAttributes(TagHelperContext context, TagHelperOutpu
158148
{
159149
output.RestoreBoundHtmlAttribute(nameof(Fragment), context);
160150
}
161-
162-
if (Route != null)
163-
{
164-
output.RestoreBoundHtmlAttribute(nameof(Route), context);
165-
}
166151
}
167152
}
168153
}

src/Microsoft.AspNet.Mvc.TagHelpers/Properties/Resources.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.AspNet.Mvc.TagHelpers/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
<data name="AnchorTagHelper_CannotDetermineHrefOneSpecified" xml:space="preserve">
121-
<value>Cannot determine an {4} for {0}. A {0} with a specified {4] must not have a {1}, {2} or {3} attribute.</value>
121+
<value>Cannot determine an {4} for {0}. A {0} with a specified {4} must not have a {1}, {2} or {3} attribute.</value>
122122
</data>
123123
<data name="AnchorTagHelper_CannotDetermineHrefRouteActionOrControllerSpecified" xml:space="preserve">
124124
<value>Cannot determine an {4} for {0}. A {0} with a specified {1} must not have a {2} or {3} attribute.</value>
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
using Microsoft.AspNet.Mvc.Rendering;
8+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
9+
using Moq;
10+
using Xunit;
11+
12+
namespace Microsoft.AspNet.Mvc.TagHelpers
13+
{
14+
public class AnchorTagHelperTest
15+
{
16+
[Fact]
17+
public void Process_CallsIntoRouteLinkWithExpectedParameters()
18+
{
19+
// Arrange
20+
var anchorTagHelper = new AnchorTagHelper
21+
{
22+
Route = "Default",
23+
Protocol = "http",
24+
Host = "contoso.com",
25+
Fragment = "hello=world"
26+
};
27+
var called = false;
28+
var context = new TagHelperContext(
29+
allAttributes: new Dictionary<string, object>());
30+
var output = new TagHelperOutput(
31+
"a",
32+
attributes: new Dictionary<string, string>(),
33+
content: string.Empty);
34+
35+
var generator = new Mock<IHtmlGenerator>();
36+
generator.Setup(mock =>
37+
mock.GenerateRouteLink(It.IsAny<string>(),
38+
It.IsAny<string>(),
39+
It.IsAny<string>(),
40+
It.IsAny<string>(),
41+
It.IsAny<string>(),
42+
It.IsAny<object>(),
43+
It.IsAny<object>()))
44+
.Callback<string, string, string, string, string, object, object>(
45+
(linkText,
46+
routeName,
47+
protocol,
48+
hostname,
49+
fragment,
50+
routeValues,
51+
htmlAttributes) =>
52+
{
53+
called = true;
54+
55+
Assert.Empty(linkText);
56+
Assert.Equal("Default", routeName);
57+
Assert.Equal("http", protocol);
58+
Assert.Equal("contoso.com", hostname);
59+
Assert.Equal("hello=world", fragment);
60+
Assert.Null(routeValues);
61+
Assert.Null(htmlAttributes);
62+
})
63+
.Returns(new TagBuilder("a"));
64+
65+
SetGenerator(anchorTagHelper, generator.Object);
66+
67+
// Act & Assert
68+
anchorTagHelper.Process(context, output);
69+
70+
Assert.True(called);
71+
}
72+
73+
[Fact]
74+
public void Process_CallsIntoActionLinkWithExpectedParameters()
75+
{
76+
// Arrange
77+
var anchorTagHelper = new AnchorTagHelper
78+
{
79+
Action = "Index",
80+
Controller = "Home",
81+
Protocol = "http",
82+
Host = "contoso.com",
83+
Fragment = "hello=world"
84+
};
85+
var called = false;
86+
var context = new TagHelperContext(
87+
allAttributes: new Dictionary<string, object>());
88+
var output = new TagHelperOutput(
89+
"a",
90+
attributes: new Dictionary<string, string>(),
91+
content: string.Empty);
92+
93+
var generator = new Mock<IHtmlGenerator>();
94+
generator.Setup(mock =>
95+
mock.GenerateActionLink(It.IsAny<string>(),
96+
It.IsAny<string>(),
97+
It.IsAny<string>(),
98+
It.IsAny<string>(),
99+
It.IsAny<string>(),
100+
It.IsAny<string>(),
101+
It.IsAny<object>(),
102+
It.IsAny<object>()))
103+
.Callback<string, string, string, string, string, string, object, object>(
104+
(linkText,
105+
actionName,
106+
controllerName,
107+
protocol,
108+
hostname,
109+
fragment,
110+
routeValues,
111+
htmlAttributes) =>
112+
{
113+
called = true;
114+
115+
Assert.Empty(linkText);
116+
Assert.Equal("Index", actionName);
117+
Assert.Equal("Home", controllerName);
118+
Assert.Equal("http", protocol);
119+
Assert.Equal("contoso.com", hostname);
120+
Assert.Equal("hello=world", fragment);
121+
Assert.Null(routeValues);
122+
Assert.Null(htmlAttributes);
123+
})
124+
.Returns(new TagBuilder("a"));
125+
126+
SetGenerator(anchorTagHelper, generator.Object);
127+
128+
// Act & Assert
129+
anchorTagHelper.Process(context, output);
130+
131+
Assert.True(called);
132+
}
133+
134+
[Fact]
135+
public void Process_RestoresBoundHtmlAttributesIfHrefProvided()
136+
{
137+
// Arrange
138+
var anchorTagHelper = new AnchorTagHelper()
139+
{
140+
Protocol = "http",
141+
Host = "contoso.com",
142+
Fragment = "hello=world"
143+
};
144+
var context = new TagHelperContext(
145+
allAttributes: new Dictionary<string, object>()
146+
{
147+
{ "pRoToCol", "http" },
148+
{ "hOsT", "contoso.com" },
149+
{ "FrAgMeNt", "hello=world" },
150+
});
151+
var output = new TagHelperOutput(
152+
"a",
153+
attributes: new Dictionary<string, string>()
154+
{
155+
{ "href", "http://www.contoso.com" }
156+
},
157+
content: string.Empty);
158+
159+
// Act
160+
anchorTagHelper.Process(context, output);
161+
162+
// Assert
163+
var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("pRoToCol"));
164+
Assert.Equal("http", attribute.Value);
165+
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("hOsT"));
166+
Assert.Equal("contoso.com", attribute.Value);
167+
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("FrAgMeNt"));
168+
Assert.Equal("hello=world", attribute.Value);
169+
}
170+
171+
public void Process_ThrowsIfHrefIsAmbiguous(string propertyName)
172+
{
173+
// Arrange
174+
var anchorTagHelper = new AnchorTagHelper();
175+
typeof(AnchorTagHelper).GetProperty(propertyName).SetValue(anchorTagHelper, "Home");
176+
var output = new TagHelperOutput(
177+
"a",
178+
attributes: new Dictionary<string, string>()
179+
{
180+
{ "href", "http://www.contoso.com" }
181+
},
182+
content: string.Empty);
183+
var expectedErrorMessage = "Cannot determine an href for AnchorTagHelper. A AnchorTagHelper with a " +
184+
"specified href must not have a Action, Controller or Route attribute.";
185+
186+
// Act & Assert
187+
var ex = Assert.Throws<InvalidOperationException>(() =>
188+
{
189+
anchorTagHelper.Process(context: null, output: output);
190+
});
191+
192+
Assert.Equal(expectedErrorMessage, ex.Message);
193+
}
194+
195+
[Theory]
196+
[InlineData("Action")]
197+
[InlineData("Controller")]
198+
public void Process_ThrowsIfRouteAndActionOrControllerProvided(string propertyName)
199+
{
200+
// Arrange
201+
var anchorTagHelper = new AnchorTagHelper
202+
{
203+
Route = "Default"
204+
};
205+
typeof(AnchorTagHelper).GetProperty(propertyName).SetValue(anchorTagHelper, "Home");
206+
var output = new TagHelperOutput(
207+
"a",
208+
attributes: new Dictionary<string, string>(),
209+
content: string.Empty);
210+
var expectedErrorMessage = "Cannot determine an href for AnchorTagHelper. A AnchorTagHelper with a " +
211+
"specified Route must not have a Action or Controller attribute.";
212+
213+
// Act & Assert
214+
var ex = Assert.Throws<InvalidOperationException>(() =>
215+
{
216+
anchorTagHelper.Process(context: null, output: output);
217+
});
218+
219+
Assert.Equal(expectedErrorMessage, ex.Message);
220+
}
221+
222+
private void SetGenerator(ITagHelper tagHelper, IHtmlGenerator generator)
223+
{
224+
var tagHelperType = tagHelper.GetType();
225+
226+
tagHelperType.GetProperty("Generator", BindingFlags.NonPublic | BindingFlags.Instance)
227+
.SetValue(tagHelper, generator);
228+
}
229+
}
230+
}

0 commit comments

Comments
 (0)