public async Task ProcessAsync_CorrectlyInfersButtonElementType() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper( new DefaultGovUkHtmlGenerator(), Mock.Of <IUrlHelperFactory>()) { Type = "button" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal("button", output.TagName); }
public async Task ProcessAsync_PreventDoubleClickSpecifiedForAnchorElementThrowsInvalidOperationException() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper( new DefaultGovUkHtmlGenerator(), Mock.Of <IUrlHelperFactory>()) { PreventDoubleClick = true, Href = "https://place.com" }; // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output)); Assert.Equal("Cannot specify the 'prevent-double-click' attribute for 'a' elements.", ex.Message); }
public async Task ProcessAsync_FormActionSpecifiedAddsAttributeToOutput(string type) { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper( new DefaultGovUkHtmlGenerator(), Mock.Of <IUrlHelperFactory>()) { FormAction = "foo", Type = type }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal("button", output.TagName); Assert.Equal("foo", output.Attributes["formaction"].Value); }
public async Task ProcessAsync_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper(); // Act await tagHelper.ProcessAsync(context, output); // Assert var expectedHtml = @" <button class=""govuk-button"" data-module=""govuk-button""> Button text </button>"; AssertEx.HtmlEqual(expectedHtml, output.RenderToString()); }
public async Task ProcessAsync_AnchorDisabledSpecifiedAddsDisabledAttributesToOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper( new DefaultGovUkHtmlGenerator(), Mock.Of <IUrlHelperFactory>()) { Disabled = true, Href = "https://place.com" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal("a", output.TagName); Assert.Contains("govuk-button--disabled", output.Attributes["class"].Value.ToString().Split(' ')); }
public async Task ProcessAsync_PreventDoubleClickSpecifiedAddsAttributesToOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper( new DefaultGovUkHtmlGenerator(), Mock.Of <IUrlHelperFactory>()) { PreventDoubleClick = true }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal("true", output.Attributes["data-prevent-double-click"].Value); }
public async Task ProcessAsync_Disabled_AddsDisabledAttributesToOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper() { Disabled = true }; // Act await tagHelper.ProcessAsync(context, output); // Assert var element = output.RenderToElement(); Assert.Contains("govuk-button--disabled", element.ClassList); Assert.Equal("disabled", element.Attributes["disabled"].Value); Assert.Equal("true", element.Attributes["aria-disabled"].Value); }
public async Task ProcessAsync_IsStartButtonAddsIconToOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper( new DefaultGovUkHtmlGenerator(), Mock.Of <IUrlHelperFactory>()) { IsStartButton = true }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Contains("govuk-button--start", output.Attributes["class"].Value.ToString().Split(' ')); Assert.Contains("<svg class=\"govuk-button__start-icon\"", output.Content.GetContent()); }
public void TestTagHelper() { //Arrange var context = new TagHelperContext( new TagHelperAttributeList(), new Dictionary <object, object>(), "myuniqueid"); var output = new TagHelperOutput("button", new TagHelperAttributeList(), (cahce, encoder) => Task.FromResult <TagHelperContent> (new DefaultTagHelperContent())); //Act var tagHelper = new ButtonTagHelper { BsButtonColor = "testValue" }; tagHelper.Process(context, output); //Assert Assert.Equal($"btn btn-{tagHelper.BsButtonColor}", output.Attributes["class"].Value); }
public async Task ProcessAsync_ButtonElementGeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper( new DefaultGovUkHtmlGenerator(), Mock.Of <IUrlHelperFactory>()); // Act await tagHelper.ProcessAsync(context, output); // Assert var html = output.AsString(); Assert.Equal( "<button class=\"govuk-button\" data-module=\"govuk-button\" type=\"submit\">" + "Button text" + "</button>", html); }
/// <summary> /// Custom implementation /// </summary> /// <param name="context"></param> /// <param name="output"></param> /// <returns></returns> public async void CustomProcess(ButtonTagHelper baseClassInstance, TagBuilder builder, TagHelperContext context, TagHelperOutput output) { builder.MergeAttribute("data-custommerge", "true"); builder.Attributes.Add("data-customadd", "true"); var childContent = output.Content.IsModified ? output.Content.GetContent() : (await output.GetChildContentAsync()).GetContent(); }
public override void Process(TagHelperContext context, TagHelperOutput output) { if (ViewContext.ViewData.Model is ListView listView) { if ((listView.ItemsCollection?.Count() ?? 0) > 0) { foreach (var currentObject in listView.ItemsCollection) { listView.SetCurrentObject(currentObject); var rowOutput = CreateTagHelperOutput("tr"); rowOutput.Content.AppendHtml("<th scope=\"row\" class=\"col-0 pl-0 pr-0\"> </th>"); //Buttons if ((listView.Buttons?.Length ?? 0) > 0 && listView.Buttons.Any(x => x.IsApplicable("Inline"))) { rowOutput.Content.AppendHtml("<th scope=\"row\" class=\"col-fit text-center\">"); var buttonTagHelper = new ButtonTagHelper(UrlHelperFactory, ApplicationService, ExpressionsService) { For = listView.Buttons, Target = "Inline", _HideText = "true", CurrentObject = currentObject, ViewContext = ViewContext }; var buttonOutput = CreateTagHelperOutput("div"); buttonTagHelper.Process(context, buttonOutput); rowOutput.Content.AppendHtml(buttonOutput.Content.GetContent()); rowOutput.Content.AppendHtml("</th>"); } //Columns var columnTagHelper = new TableListViewColumnTagHelper(UrlHelperFactory, ApplicationService, ExpressionsService) { For = listView.Columns, IsSubListView = IsSubListView, ViewContext = ViewContext }; columnTagHelper.Process(context, rowOutput); var backColor = listView.GetApplicableBackColor(ExpressionsService, currentObject); output.Content.AppendHtml($"<tr class=\"{backColor}\">{rowOutput.Content.GetContent() }</tr>"); } listView.ClearCurrentObject(); } else if (!string.IsNullOrWhiteSpace(listView.MessageOnEmpty)) { output.Content.AppendHtml( $"<tr>" + $"<td class=\"col text-center\" colspan=\"{1 + ((listView?.Buttons?.Length ?? 0) > 0 && listView.Buttons.Any(x => x.IsApplicable("Inline")) ? 1 : 0) + (listView?.Columns?.Length ?? 0)}\">{listView.MessageOnEmpty}</td>" + "</tr>"); } } }
/// <summary> /// 获取结果 /// </summary> private string GetResult(ButtonTagHelper button, TagHelperAttributeList contextAttributes = null, TagHelperAttributeList outputAttributes = null, TagHelperContent content = null) { var context = new TagHelperContext("", contextAttributes ?? new TagHelperAttributeList(), new Dictionary <object, object>(), Id.Guid()); var output = new TagHelperOutput("", outputAttributes ?? new TagHelperAttributeList(), (useCachedResult, encoder) => Task.FromResult(content ?? new DefaultTagHelperContent())); button.Process(context, output); var writer = new StringWriter(); output.WriteTo(writer, HtmlEncoder.Default); var result = writer.ToString(); _output.WriteLine(result); return(result); }
public void Process_ShouldSetBsButtonClass() { var context = new TagHelperContext(new TagHelperAttributeList(), new Dictionary <object, object>(), "myuniqueid"); var output = new TagHelperOutput("button", new TagHelperAttributeList(), (cache, encoder) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent())); var testee = new ButtonTagHelper { BsButtonColor = "danger" }; testee.Process(context, output); output.Attributes["class"].Value.Should().Be($"btn btn-{testee.BsButtonColor}"); }
public void TestButtonTagHelper() { //arrange var context = new TagHelperContext(new TagHelperAttributeList(), new Dictionary <object, object>(), "uniqueid"); var output = new TagHelperOutput("button", new TagHelperAttributeList(), (cache, encoder) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent())); //act var helper = new ButtonTagHelper { BsButtonColor = "valueToBeTested" }; helper.Process(context, output); //assert Assert.AreEqual($"btn btn-{helper.BsButtonColor}", output.Attributes["class"].Value); }
public async Task WhenButtonHelperCalled_ThenCorrectClassCalled() { var tagHelper = Substitute.For <IMockViewComponentHelper>(); var componentTag = new ButtonTagHelper(tagHelper) { Id = "Id", AdditionalClass = "AdditionalClass", Disabled = false, Text = "ButtonText" }; string id = componentTag.Id; string additionalClass = componentTag.AdditionalClass; bool disabled = componentTag.Disabled; string text = componentTag.Text; await ViewComponentTestHelper.CallTagHelper("Button", tagHelper, componentTag); }
public async Task ProcessAsync_IsStartButton_AddsIconToOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ButtonTagHelper() { IsStartButton = true }; // Act await tagHelper.ProcessAsync(context, output); // Assert var element = output.RenderToElement(); Assert.Contains("govuk-button--start", element.ClassList); Assert.Collection( element.QuerySelectorAll("svg"), svg => Assert.Contains("govuk-button__start-icon", svg.ClassList)); }
/// <summary> /// 测试初始化 /// </summary> public ButtonTagHelperTest(ITestOutputHelper output) { _output = output; _component = new ButtonTagHelper(); }
public async Task ProcessAsync_AspLinkAttributesSpecifiedWithButtonElementTypeGeneratesFormActionAttribute( string action, string controller, string area, string fragment, string host, string page, string pageHandler, string protocol, string route, IDictionary <string, string> routeValues) { // Arrange var context = new TagHelperContext( tagName: "govuk-button", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-button", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent("Button text"); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var urlHelperFactory = new Mock <IUrlHelperFactory>(); urlHelperFactory .Setup(mock => mock.GetUrlHelper(It.IsAny <ActionContext>())) .Returns((ActionContext actionContext) => { var urlHelper = new Mock <IUrlHelper>(); urlHelper.SetupGet(mock => mock.ActionContext).Returns(actionContext); urlHelper .Setup(mock => mock.Action( /*actionContext: */ It.IsAny <UrlActionContext>())) .Returns("http://place.com"); urlHelper .Setup(mock => mock.Link( /*routeName: */ It.IsAny <string>(), /*values: */ It.IsAny <object>())) .Returns("http://place.com"); urlHelper .Setup(mock => mock.RouteUrl( /*routeContext: */ It.IsAny <UrlRouteContext>())) .Returns("http://place.com"); return(urlHelper.Object); }); var viewContext = new ViewContext() { RouteData = new Microsoft.AspNetCore.Routing.RouteData() }; var tagHelper = new ButtonTagHelper(new DefaultGovUkHtmlGenerator(), urlHelperFactory.Object) { Action = action, Area = area, Controller = controller, Fragment = fragment, Host = host, Page = page, PageHandler = pageHandler, Protocol = protocol, Route = route, RouteValues = routeValues, Type = "button", ViewContext = viewContext }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal("button", output.TagName); Assert.Equal("http://place.com", output.Attributes["formaction"].Value); }
/// <summary> /// 测试初始化 /// </summary> public ButtonTagHelperTest(ITestOutputHelper output) { _output = output; _button = new ButtonTagHelper(); }