public async Task ProcessAsync_NoTitleDescriptionOrItems_RendersNothing() { // Arrange var context = new TagHelperContext( tagName: "govuk-error-summary", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-error-summary", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ErrorSummaryTagHelper(new DefaultGovUkHtmlGenerator()); // Act await tagHelper.ProcessAsync(context, output); // Assert var html = output.AsString(); Assert.Empty(html); }
public async Task ProcessAsync_NoTitleThrowsInvalidOperationException() { // Arrange var context = new TagHelperContext( tagName: "govuk-error-summary", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-error-summary", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ErrorSummaryTagHelper(new DefaultGovUkHtmlGenerator()); // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output)); Assert.Equal("Missing <govuk-error-summary-title> element.", ex.Message); }
public async Task ProcessAsync_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-error-summary", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-error-summary", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var errorSummaryContext = (ErrorSummaryContext)context.Items[typeof(ErrorSummaryContext)]; errorSummaryContext.SetTitle(attributes: null, new HtmlString("Title")); errorSummaryContext.SetDescription(attributes: null, new HtmlString("Description")); errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("First message"), Href = "#Field1" }); errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("Second message"), Href = "#Field2" }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ErrorSummaryTagHelper(); // Act await tagHelper.ProcessAsync(context, output); // Assert var expectedHtml = @" <div aria-labelledby=""error-summary-title"" class=""govuk-error-summary"" data-module=""govuk-error-summary"" role=""alert""> <h2 class=""govuk-error-summary__title"" id=""error-summary-title"">Title</h2> <div class=""govuk-error-summary__body""> <p>Description</p> <ul class=""govuk-error-summary__list govuk-list""> <li><a href=""#Field1"">First message</a></li> <li><a href=""#Field2"">Second message</a></li> </ul> </div> </div>"; AssertEx.HtmlEqual(expectedHtml, output.RenderToString()); }
public async Task WhenErrorSummaryHelperCalled_ThenCorrectClassCalled() { var tagHelper = Substitute.For <IMockViewComponentHelper>(); var componentTag = new ErrorSummaryTagHelper(tagHelper) { Id = "Id", Hidden = true }; await ViewComponentTestHelper.CallTagHelper("ErrorSummary", tagHelper, componentTag); }
public async Task ProcessAsync_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-error-summary", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-error-summary", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var errorSummaryContext = (ErrorSummaryContext)context.Items[typeof(ErrorSummaryContext)]; errorSummaryContext.TrySetTitle(attributes: null, new HtmlString("Title")); errorSummaryContext.TrySetDescription(attributes: null, new HtmlString("Description")); errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("First message") }); errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("Second message") }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ErrorSummaryTagHelper(new DefaultGovUkHtmlGenerator()); // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal( "<div aria-labelledby=\"error-summary-title\" class=\"govuk-error-summary\" data-module=\"govuk-error-summary\" role=\"alert\" tabindex=\"-1\">" + "<h2 class=\"govuk-error-summary__title\" id=\"error-summary-title\">Title</h2>" + "<div class=\"govuk-error-summary__body\">" + "<p>Description</p>" + "<ul class=\"govuk-error-summary__list govuk-list\">" + "<li>First message</li>" + "<li>Second message</li>" + "</ul>" + "</div>" + "</div>", output.AsString()); }
public async Task ProcessAsync_WithDisableAutoFocus_RendersDataAttribute() { // Arrange var context = new TagHelperContext( tagName: "govuk-error-summary", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-error-summary", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var errorSummaryContext = (ErrorSummaryContext)context.Items[typeof(ErrorSummaryContext)]; errorSummaryContext.SetTitle(attributes: null, new HtmlString("Title")); errorSummaryContext.SetDescription(attributes: null, new HtmlString("Description")); errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("First message"), Href = "#Field1" }); errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("Second message"), Href = "#Field2" }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ErrorSummaryTagHelper() { DisableAutoFocus = true }; // Act await tagHelper.ProcessAsync(context, output); // Assert var element = output.RenderToElement(); Assert.NotNull(element.GetAttribute("data-disable-auto-focus")); }
public async Task ProcessAsync_ItemWithEmptyLink_DoesNotRenderAnchorTag() { // Arrange var context = new TagHelperContext( tagName: "govuk-error-summary", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-error-summary", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var errorSummaryContext = (ErrorSummaryContext)context.Items[typeof(ErrorSummaryContext)]; errorSummaryContext.SetTitle(attributes: null, new HtmlString("Title")); errorSummaryContext.SetDescription(attributes: null, new HtmlString("Description")); errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("Message"), Href = null }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ErrorSummaryTagHelper(); // Act await tagHelper.ProcessAsync(context, output); // Assert var element = output.RenderToElement(); var itemElement = element.QuerySelector("li"); Assert.NotEqual("a", itemElement.FirstChild.NodeName, StringComparer.OrdinalIgnoreCase); }
public async Task ProcessAsync_NoTitleSpecified_UsesDefaultTitle() { // Arrange var context = new TagHelperContext( tagName: "govuk-error-summary", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-error-summary", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var errorSummaryContext = (ErrorSummaryContext)context.Items[typeof(ErrorSummaryContext)]; errorSummaryContext.AddItem(new ErrorSummaryItem() { Content = new HtmlString("First message") }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new ErrorSummaryTagHelper(new DefaultGovUkHtmlGenerator()); // Act await tagHelper.ProcessAsync(context, output); // Assert var html = output.AsString(); var node = HtmlNode.CreateNode(html); var h2 = node.ChildNodes.FindFirst("h2"); Assert.Equal("There is a problem", h2.InnerHtml); }