public async Task ProcessAsync_AddsFieldsetToContext()
        {
            // Arrange
            var radiosContext = new RadiosContext(name: null, aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-radios-fieldset",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(RadiosContext), radiosContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios-fieldset",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var fieldsetContext = context.GetContextItem <RadiosFieldsetContext>();
                fieldsetContext.SetLegend(isPageHeading: true, attributes: null, content: new HtmlString("Legend"));

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

            var tagHelper = new RadiosFieldsetTagHelper();

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

            // Assert
            Assert.True(radiosContext.Fieldset?.Legend?.IsPageHeading);
            Assert.Equal("Legend", radiosContext.Fieldset?.Legend?.Content?.RenderToString());
        }
예제 #2
0
        public async Task ProcessAsync_SetsFieldsetOnContext()
        {
            // Arrange
            var radiosContext = new RadiosContext(
                idPrefix: "prefix",
                resolvedName: "r",
                viewContext: null,
                aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-radios-fieldset",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(RadiosContext), radiosContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios-fieldset",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var fieldsetContext = (RadiosFieldsetContext)context.Items[typeof(RadiosFieldsetContext)];
                fieldsetContext.TrySetLegend(attributes: null, content: new HtmlString("Legend"));

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

            var tagHelper = new RadiosFieldsetTagHelper()
            {
                IsPageHeading = true
            };

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

            // Assert
            Assert.True(radiosContext.Fieldset.IsPageHeading);
            Assert.Equal("Legend", radiosContext.Fieldset.LegendContent.AsString());
        }
        public async Task ProcessAsync_ParentAlreadyHasFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var radiosContext = new RadiosContext(name: null, aspFor: null);

            radiosContext.OpenFieldset();
            var radiosFieldsetContext = new RadiosFieldsetContext(attributes: null);

            radiosFieldsetContext.SetLegend(isPageHeading: false, attributes: null, content: new HtmlString("Existing legend"));
            radiosContext.CloseFieldset(radiosFieldsetContext);

            var context = new TagHelperContext(
                tagName: "govuk-radios-fieldset",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(RadiosContext), radiosContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-radios-fieldset",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var fieldsetContext = context.GetContextItem <RadiosFieldsetContext>();
                fieldsetContext.SetLegend(isPageHeading: true, attributes: null, content: new HtmlString("Legend"));

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

            var tagHelper = new RadiosFieldsetTagHelper();

            // Act
            var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Only one <govuk-radios-fieldset> element is permitted within each <govuk-radios>.", ex.Message);
        }