Example #1
0
        public async Task ProcessAsync_SetsLegendOnContext()
        {
            // Arrange
            var fieldsetContext = new CheckboxesFieldsetContext();

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-fieldset-legend",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesFieldsetContext), fieldsetContext }
            },
                uniqueId: "test");

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

            var tagHelper = new CheckboxesFieldsetLegendTagHelper();

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

            // Assert
            Assert.Equal("Legend", fieldsetContext.Legend?.content?.AsString());
        }
Example #2
0
        public async Task ProcessAsync_ParentAlreadyHasLegend_ThrowsInvalidOperationException()
        {
            // Arrange
            var fieldsetContext = new CheckboxesFieldsetContext(attributes: null);

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

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-fieldset-legend",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesFieldsetContext), fieldsetContext }
            },
                uniqueId: "test");

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

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

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Only one <govuk-checkboxes-fieldset-legend> element is permitted within each <govuk-checkboxes-fieldset>.", ex.Message);
        }
Example #3
0
        public async Task ProcessAsync_AddsLegendToContext()
        {
            // Arrange
            var fieldsetContext = new CheckboxesFieldsetContext(attributes: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-fieldset-legend",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesFieldsetContext), fieldsetContext }
            },
                uniqueId: "test");

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

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

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

            // Assert
            Assert.Equal("Legend content", fieldsetContext.Legend?.Content.RenderToString());
            Assert.True(fieldsetContext.Legend?.IsPageHeading);
        }