コード例 #1
0
        public async Task ProcessAsync_NoNameOrAspForThrowsInvalidOperationException()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(Mock.Of <IGovUkHtmlGenerator>())
            {
                IdPrefix = "prefix"
            };

            // Act & Assert
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output));

            Assert.Equal("At least one of the 'name' and 'asp-for' attributes must be specified.", ex.Message);
        }
コード例 #2
0
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var radiosContext = (RadiosContext)context.Items[typeof(RadiosContext)];

                radiosContext.AddItem(new RadiosItem()
                {
                    IsChecked  = false,
                    Content    = new HtmlString("First"),
                    IsDisabled = false,
                    Id         = "first",
                    Value      = "first"
                });

                radiosContext.AddItem(new RadiosItemDivider()
                {
                    Content = new HtmlString("Divider")
                });

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(new DefaultGovUkHtmlGenerator())
            {
                IdPrefix = "my-id",
                Name     = "testradios"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var html = output.AsString();

            Assert.Equal(
                "<div class=\"govuk-form-group\">" +
                "<div class=\"govuk-radios\">" +
                "<div class=\"govuk-radios__item\">" +
                "<input class=\"govuk-radios__input\" id=\"first\" name=\"testradios\" type=\"radio\" value=\"first\" />" +
                "<label class=\"govuk-radios__label govuk-label\" for=\"first\">First</label>" +
                "</div>" +
                "<div class=\"govuk-radios__divider\">Divider</div>" +
                "</div>" +
                "</div>",
                html);
        }
コード例 #3
0
        public async Task ProcessAsync_WithCheckedItemConditional_GeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var radiosContext = context.GetContextItem <RadiosContext>();

                radiosContext.AddItem(new RadiosItem()
                {
                    Checked      = true,
                    LabelContent = new HtmlString("First"),
                    Conditional  = new RadiosItemConditional()
                    {
                        Content = new HtmlString("Item 1 conditional")
                    },
                    Id    = "first",
                    Value = "first"
                });

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(new ComponentGenerator(), new DefaultModelHelper())
            {
                IdPrefix = "my-id",
                Name     = "testradios"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var expectedHtml = @"
<div class=""govuk-form-group"">
    <div class=""govuk-radios"" data-module=""govuk-radios"">
        <div class=""govuk-radios__item"">
            <input class=""govuk-radios__input"" id=""first"" name=""testradios"" type=""radio"" value=""first"" checked=""checked"" data-aria-controls=""conditional-first"" />
            <label class=""govuk-radios__label govuk-label"" for=""first"">First</label>
        </div>
        <div class=""govuk-radios__conditional"" id=""conditional-first"">Item 1 conditional</div>
    </div>
</div>";

            AssertEx.HtmlEqual(expectedHtml, output.RenderToString());
        }
コード例 #4
0
        public async Task ProcessAsync_CheckedItemGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var radiosContext = (RadiosContext)context.Items[typeof(RadiosContext)];

                radiosContext.AddItem(new RadiosItem()
                {
                    IsChecked  = true,
                    Content    = new HtmlString("First"),
                    IsDisabled = false,
                    Id         = "first",
                    Value      = "first"
                });

                radiosContext.AddItem(new RadiosItemDivider()
                {
                    Content = new HtmlString("Divider")
                });

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(new DefaultGovUkHtmlGenerator())
            {
                IdPrefix    = "my-id",
                DescribedBy = "describedby",
                Name        = "testradios"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var html  = output.AsString();
            var node  = HtmlNode.CreateNode(html);
            var input = node.ChildNodes.FindFirst("input");

            Assert.Equal("checked", input.Attributes["checked"].Value);
        }
コード例 #5
0
        public async Task ProcessAsync_WithFieldsetGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var radiosContext = (RadiosContext)context.Items[typeof(RadiosContext)];

                radiosContext.SetFieldset(new RadiosFieldset()
                {
                    IsPageHeading = false,
                    LegendContent = new HtmlString("Legend")
                });

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(new DefaultGovUkHtmlGenerator())
            {
                DescribedBy = "describedby",
                IdPrefix    = "my-id",
                Name        = "testradios"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var html = output.AsString();

            Assert.Equal(
                "<div class=\"govuk-form-group\">" +
                "<fieldset aria-describedby=\"describedby\" class=\"govuk-fieldset\">" +
                "<legend class=\"govuk-fieldset__legend\">Legend</legend>" +
                "<div class=\"govuk-radios\">" +
                "</div>" +
                "</fieldset>" +
                "</div>",
                html);
        }
コード例 #6
0
        public async Task ProcessAsync_WithErrorMessageGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var formGroupContext = (FormGroupBuilder)context.Items[typeof(FormGroupBuilder)];
                formGroupContext.TrySetErrorMessage(
                    visuallyHiddenText: null,
                    attributes: null,
                    new HtmlString("A error"));

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(new DefaultGovUkHtmlGenerator())
            {
                IdPrefix = "my-id",
                Name     = "testradios"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var html = output.AsString();

            Assert.Equal(
                "<div class=\"govuk-form-group--error govuk-form-group\">" +
                "<span class=\"govuk-error-message\" id=\"my-id-error\"><span class=\"govuk-visually-hidden\">Error</span>A error</span>" +
                "<div class=\"govuk-radios\">" +
                "</div>" +
                "</div>",
                html);
        }
コード例 #7
0
        public async Task ProcessAsync_WithError_GeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var radiosContext = context.GetContextItem <RadiosContext>();

                radiosContext.SetErrorMessage(visuallyHiddenText: null, attributes: null, content: new HtmlString("A error"));

                radiosContext.AddItem(new RadiosItem()
                {
                    Checked      = false,
                    LabelContent = new HtmlString("First"),
                    Disabled     = true,
                    Id           = "first",
                    Value        = "first"
                });

                radiosContext.AddItem(new RadiosItem()
                {
                    Checked      = true,
                    LabelContent = new HtmlString("Second"),
                    Disabled     = false,
                    Id           = "second",
                    Value        = "second"
                });

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(new ComponentGenerator(), new DefaultModelHelper())
            {
                IdPrefix = "my-id",
                Name     = "testradios"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var expectedHtml = @"
<div class=""govuk-form-group govuk-form-group--error"">
    <p class=""govuk-error-message"" id=""my-id-error""><span class=""govuk-visually-hidden"">Error:</span>A error</p>
    <div class=""govuk-radios"" data-module=""govuk-radios"">
        <div class=""govuk-radios__item"">
            <input class=""govuk-radios__input"" id=""first"" name=""testradios"" type=""radio"" value=""first"" disabled=""disabled"" />
            <label class=""govuk-radios__label govuk-label"" for=""first"">First</label>
        </div>
        <div class=""govuk-radios__item"">
            <input class=""govuk-radios__input"" id=""second"" name=""testradios"" type=""radio"" value=""second"" checked=""checked"" />
            <label class=""govuk-radios__label govuk-label"" for=""second"">Second</label>
        </div>
    </div>
</div>";

            AssertEx.HtmlEqual(expectedHtml, output.RenderToString());
        }
コード例 #8
0
        public async Task ProcessAsync_WithFieldset_GeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-radios",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var radiosContext = context.GetContextItem <RadiosContext>();

                radiosContext.OpenFieldset();
                var radiosFieldsetContext = new RadiosFieldsetContext(attributes: null);
                radiosFieldsetContext.SetLegend(isPageHeading: false, attributes: null, content: new HtmlString("Legend"));

                radiosContext.SetHint(attributes: null, content: new HtmlString("The hint"));

                radiosContext.AddItem(new RadiosItem()
                {
                    Checked      = false,
                    LabelContent = new HtmlString("First"),
                    Disabled     = true,
                    Id           = "first",
                    Value        = "first"
                });

                radiosContext.AddItem(new RadiosItem()
                {
                    Checked      = true,
                    LabelContent = new HtmlString("Second"),
                    Disabled     = false,
                    Id           = "second",
                    Value        = "second"
                });

                radiosContext.CloseFieldset(radiosFieldsetContext);

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new RadiosTagHelper(new ComponentGenerator(), new DefaultModelHelper())
            {
                DescribedBy = "describedby",
                IdPrefix    = "my-id",
                Name        = "testradios"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var expectedHtml = @"
<div class=""govuk-form-group"">
    <fieldset aria-describedby=""describedby my-id-hint"" class=""govuk-fieldset"">
        <legend class=""govuk-fieldset__legend"">Legend</legend>
        <div class=""govuk-hint"" id=""my-id-hint"">The hint</div>
        <div class=""govuk-radios"" data-module=""govuk-radios"">
            <div class=""govuk-radios__item"">
                <input class=""govuk-radios__input"" id=""first"" name=""testradios"" type=""radio"" value=""first"" disabled=""disabled"" />
                <label class=""govuk-radios__label govuk-label"" for=""first"">First</label>
            </div>
            <div class=""govuk-radios__item"">
                <input class=""govuk-radios__input"" id=""second"" name=""testradios"" type=""radio"" value=""second"" checked=""checked"" />
                <label class=""govuk-radios__label govuk-label"" for=""second"">Second</label>
            </div>
        </div>
    </fieldset>
</div>";

            AssertEx.HtmlEqual(expectedHtml, output.RenderToString());
        }