Ejemplo n.º 1
0
        public async Task ProcessAsync_AddsFieldsetToContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: null, aspFor: null);

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

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

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

            var tagHelper = new CheckboxesFieldsetTagHelper();

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

            // Assert
            Assert.True(checkboxesContext.Fieldset?.Legend?.IsPageHeading);
            Assert.Equal("Legend", checkboxesContext.Fieldset?.Legend?.Content?.RenderToString());
        }
Ejemplo n.º 2
0
        public async Task ProcessAsync_SetsFieldsetOnContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(
                idPrefix: "prefix",
                resolvedName: "r",
                aspFor: null,
                viewContext: null);

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

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

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

            var tagHelper = new CheckboxesFieldsetTagHelper();

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

            // Assert
            Assert.True(checkboxesContext.Fieldset.LegendIsPageHeading);
            Assert.Equal("Legend", checkboxesContext.Fieldset.LegendContent.AsString());
        }
Ejemplo n.º 3
0
        public async Task ProcessAsync_ParentAlreadyHasFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: null, aspFor: null);

            checkboxesContext.OpenFieldset();
            var checkboxesFieldsetContext = new CheckboxesFieldsetContext(attributes: null);

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

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

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

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

            var tagHelper = new CheckboxesFieldsetTagHelper();

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

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