Add unit tests for link tag helpers
Testing these: https://github.com/TASVideos/tasvideos/blob/ffe8999d7e52e2016b52d92684ec4447916707c4/TASVideos/TagHelpers/WikiLinkTagHelpers.cs#L8-L91
So I got the test for ProfileLinkTagHelper working, but now after the third commit none of the others do...
edit: Rebased, still passing at https://github.com/TASVideos/tasvideos/pull/2197/commits/dc3115ef681124cd02878a523de3013f8264447f and failing at https://github.com/TASVideos/tasvideos/pull/2197/commits/4c16f806b5d39cfd7b1c3e9ca77cb2e1af972578.
Assert.AreEqual failed. Expected:<<a href="/123G">some game</a>>. Actual:<<game-link href="">some game</game-link>>.
Assert.AreEqual failed. Expected:<<a href="/1234M">some movie</a>>. Actual:<<pub-link href="">some movie</pub-link>>.
Assert.AreEqual failed. Expected:<<a href="/1234S">some movie</a>>. Actual:<<sub-link href="">some movie</sub-link>>.
Assert.AreEqual failed. Expected:<<a href="/GameResources/NES/SuperMarioBros">GameResources/NES/SuperMarioBros</a>>. Actual:<<wiki-link href="">GameResources/NES/SuperMarioBros</wiki-link>>.
Assert.AreEqual failed. Expected:<<a href="/WelcomeToTASVideos">WelcomeToTASVideos</a>>. Actual:<<wiki-link href="">WelcomeToTASVideos</wiki-link>>.
So basically, if you want to use Page, you actually have to give it a page that exists. For example /123M is not a page that exists. It's actually /Publications/View and we only reroute it like
options.Conventions.AddPageRoute("/Publications/View", "{id:int}M").
Also the output TagName needs to be set to a, otherwise it stays at the original one like pub-link.
For example:
--- a/TASVideos/TagHelpers/WikiLinkTagHelpers.cs
+++ b/TASVideos/TagHelpers/WikiLinkTagHelpers.cs
@@ -11,7 +11,9 @@ public class PubLinkTagHelper(IHtmlGenerator generator) : AnchorTagHelper(genera
public override void Process(TagHelperContext context, TagHelperOutput output)
{
- Page = $"/{Id}M";
+ output.TagName = "a";
+ Page = "/Publications/View";
+ RouteValues.Add("Id", Id.ToString());
base.Process(context, output);
}
}
Though I'm wondering why we don't just set the href directly with output.Attributes.Add("href", $"/{Id}M"); instead of using Page, like it was done before this PR?
Thank you, that was the small detail I was missing. And since it doesn't work for wiki pages, I've reverted the change to <wiki-link/>, but the rest are now working.
Though I'm wondering why we don't just set the href directly with
output.Attributes.Add("href", $"/{Id}M");instead of using Page, like it was done before this PR?
The idea was to use the same mechanism as <a asp-page/>. After this PR I wanted to add more link helpers, but existing calls mostly use that rather than <a href/>. Also, I'm hoping that <a asp-page/> would help with tracking page links (for generating referrer lists).
Moved that to a separate PR, and rewrote the tests here to be async.