public async Task ProcessAsync_WithValue_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-date-input", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-date-input", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new DateInputTagHelper(Options.Create(new GovUkFrontendAspNetCoreOptions()), new DateInputParseErrorsProvider()) { Id = "my-id", NamePrefix = "my-name", Value = new Date(2020, 4, 1) }; // Act await tagHelper.ProcessAsync(context, output); // Assert var expectedHtml = @" <div class=""govuk-form-group""> <div class=""govuk-date-input"" id=""my-id""> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Day"">Day</label> <input class=""govuk-input--width-2 govuk-date-input__input govuk-input"" id=""my-id.Day"" inputmode=""numeric"" name=""my-name.Day"" type=""text"" value=""1""> </div> </div> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Month"">Month</label> <input class=""govuk-input--width-2 govuk-date-input__input govuk-input"" id=""my-id.Month"" inputmode=""numeric"" name=""my-name.Month"" type=""text"" value=""4""> </div> </div> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Year"">Year</label> <input class=""govuk-input--width-4 govuk-date-input__input govuk-input"" id=""my-id.Year"" inputmode=""numeric"" name=""my-name.Year"" type=""text"" value=""2020""> </div> </div> </div> </div>"; AssertEx.HtmlEqual(expectedHtml, output.RenderToString()); }
public async Task ProcessAsync_NullValue_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-date-input", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-date-input", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var formGroupContext = (FormGroupBuilder)context.Items[typeof(FormGroupBuilder)]; formGroupContext.TrySetLabel( isPageHeading: false, attributes: null, content: new HtmlString("The label")); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new DateInputTagHelper(new DefaultGovUkHtmlGenerator()) { IdPrefix = "my-id-prefix", DescribedBy = "describedby", Name = "my-id", Value = null }; // Act await tagHelper.ProcessAsync(context, output); // Assert var html = output.AsString(); var node = HtmlNode.CreateNode(html); var container = node.ChildNodes.FindFirst("div"); var day = container.SelectNodes("//input").First(); var month = container.SelectNodes("//input").Skip(1).First(); var year = container.SelectNodes("//input").Skip(2).First(); Assert.Equal("", day.Attributes["value"].Value); Assert.Equal("", month.Attributes["value"].Value); Assert.Equal("", year.Attributes["value"].Value); }
public async Task ProcessAsync_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-date-input", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-date-input", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var formGroupContext = (FormGroupBuilder)context.Items[typeof(FormGroupBuilder)]; formGroupContext.TrySetLabel( isPageHeading: false, attributes: null, content: new HtmlString("The label")); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new DateInputTagHelper(new DefaultGovUkHtmlGenerator()) { IdPrefix = "my-id", DescribedBy = "describedby", Name = "my-name", Value = new Date(2020, 4, 1) }; // Act await tagHelper.ProcessAsync(context, output); // Assert var html = output.AsString(); var node = HtmlNode.CreateNode(html); var container = node.ChildNodes.FindFirst("div"); Assert.Equal( "<div class=\"govuk-date-input\" id=\"my-id\">" + "<div class=\"govuk-date-input__item\">" + "<div class=\"govuk-form-group\">" + "<label class=\"govuk-date-input__label govuk-label\" for=\"my-id.Day\">Day</label>" + "<input class=\"govuk-input--width-2 govuk-date-input__input govuk-input\" id=\"my-id.Day\" inputmode=\"numeric\" name=\"my-name.Day\" pattern=\"[0-9]*\" type=\"text\" value=\"1\">" + "</div>" + "</div>" + "<div class=\"govuk-date-input__item\">" + "<div class=\"govuk-form-group\">" + "<label class=\"govuk-date-input__label govuk-label\" for=\"my-id.Month\">Month</label>" + "<input class=\"govuk-input--width-2 govuk-date-input__input govuk-input\" id=\"my-id.Month\" inputmode=\"numeric\" name=\"my-name.Month\" pattern=\"[0-9]*\" type=\"text\" value=\"4\">" + "</div>" + "</div>" + "<div class=\"govuk-date-input__item\">" + "<div class=\"govuk-form-group\">" + "<label class=\"govuk-date-input__label govuk-label\" for=\"my-id.Year\">Year</label>" + "<input class=\"govuk-input--width-4 govuk-date-input__input govuk-input\" id=\"my-id.Year\" inputmode=\"numeric\" name=\"my-name.Year\" pattern=\"[0-9]*\" type=\"text\" value=\"2020\">" + "</div>" + "</div>" + "</div>", container.OuterHtml); }
public async Task ProcessAsync_HaveErrorClassesWhenErrorSpecified( DateInputErrorItems?specifiedErrorItems, bool expectDayError, bool expectMonthError, bool expectYearError) { // Arrange var context = new TagHelperContext( tagName: "govuk-date-input", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-date-input", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var formGroupContext = (FormGroupBuilder)context.Items[typeof(FormGroupBuilder)]; formGroupContext.TrySetLabel( isPageHeading: false, attributes: null, content: new HtmlString("The label")); formGroupContext.TrySetErrorMessage( visuallyHiddenText: null, attributes: null, content: new HtmlString("Error")); if (specifiedErrorItems != null) { var dateInputContext = (DateInputContext)context.Items[typeof(DateInputContext)]; dateInputContext.SetErrorItems(specifiedErrorItems.Value); } var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new DateInputTagHelper(new DefaultGovUkHtmlGenerator()) { IdPrefix = "my-id", DescribedBy = "describedby", Name = "my-name", Value = new Date(2020, 4, 1) }; // Act await tagHelper.ProcessAsync(context, output); // Assert var html = output.AsString(); var node = HtmlNode.CreateNode(html); var container = node.ChildNodes.FindFirst("div"); var day = container.SelectNodes("//input").First(); var month = container.SelectNodes("//input").Skip(1).First(); var year = container.SelectNodes("//input").Skip(2).First(); AssertHaveErrorClass(day, expectDayError); AssertHaveErrorClass(month, expectMonthError); AssertHaveErrorClass(year, expectYearError);
public async Task ProcessAsync_WithFieldset_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-date-input", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-date-input", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var dateInputContext = context.GetContextItem <DateInputContext>(); dateInputContext.OpenFieldset(); var dateInputFieldsetContext = new DateInputFieldsetContext(attributes: null); dateInputFieldsetContext.SetLegend(isPageHeading: false, attributes: null, content: new HtmlString("Legend")); dateInputContext.CloseFieldset(dateInputFieldsetContext); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new DateInputTagHelper(Options.Create(new GovUkFrontendAspNetCoreOptions()), new DateInputParseErrorsProvider()) { DescribedBy = "describedby", Id = "my-id", NamePrefix = "my-name", Value = new Date(2020, 4, 1) }; // Act await tagHelper.ProcessAsync(context, output); // Assert var expectedHtml = @" <div class=""govuk-form-group""> <fieldset class=""govuk-fieldset"" role=""group"" aria-describedby=""describedby""> <legend class=""govuk-fieldset__legend""> Legend </legend> <div class=""govuk-date-input"" id=""my-id""> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Day"">Day</label> <input class=""govuk-input--width-2 govuk-date-input__input govuk-input"" id=""my-id.Day"" inputmode=""numeric"" name=""my-name.Day"" type=""text"" value=""1""> </div> </div> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Month"">Month</label> <input class=""govuk-input--width-2 govuk-date-input__input govuk-input"" id=""my-id.Month"" inputmode=""numeric"" name=""my-name.Month"" type=""text"" value=""4""> </div> </div> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Year"">Year</label> <input class=""govuk-input--width-4 govuk-date-input__input govuk-input"" id=""my-id.Year"" inputmode=""numeric"" name=""my-name.Year"" type=""text"" value=""2020""> </div> </div> </div> </fieldset> </div>"; AssertEx.HtmlEqual(expectedHtml, output.RenderToString()); }
public async Task ProcessAsync_WithAspForAndValue_UsesValueAttribute() { // Arrange var context = new TagHelperContext( tagName: "govuk-date-input", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-date-input", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), new Model() { Date = new Date(2020, 4, 1) }) .GetExplorerForProperty(nameof(Model.Date)); var viewContext = new ViewContext(); var tagHelper = new DateInputTagHelper(Options.Create(new GovUkFrontendAspNetCoreOptions()), new DateInputParseErrorsProvider()) { AspFor = new ModelExpression(nameof(Model.Date), modelExplorer), Id = "my-id", NamePrefix = "my-name", ViewContext = viewContext, Value = new Date(2022, 5, 3) }; // Act await tagHelper.ProcessAsync(context, output); // Assert var expectedHtml = @" <div class=""govuk-form-group""> <div class=""govuk-date-input"" id=""my-id""> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Day"">Day</label> <input class=""govuk-input--width-2 govuk-date-input__input govuk-input"" id=""my-id.Day"" inputmode=""numeric"" name=""my-name.Day"" type=""text"" value=""3""> </div> </div> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Month"">Month</label> <input class=""govuk-input--width-2 govuk-date-input__input govuk-input"" id=""my-id.Month"" inputmode=""numeric"" name=""my-name.Month"" type=""text"" value=""5""> </div> </div> <div class=""govuk-date-input__item""> <div class=""govuk-form-group""> <label class=""govuk-date-input__label govuk-label"" for=""my-id.Year"">Year</label> <input class=""govuk-input--width-4 govuk-date-input__input govuk-input"" id=""my-id.Year"" inputmode=""numeric"" name=""my-name.Year"" type=""text"" value=""2022""> </div> </div> </div> </div>"; AssertEx.HtmlEqual(expectedHtml, output.RenderToString()); }