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

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

            var tagHelper = new CheckboxesTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper())
            {
                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);
        }
Exemple #2
0
        public async Task ProcessAsync_WithCheckedItemConditionalGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-checkboxes",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

                checkboxesContext.AddItem(new CheckboxesItem()
                {
                    IsChecked          = true,
                    ConditionalContent = new HtmlString("Conditional"),
                    ConditionalId      = "conditional-first",
                    Content            = new HtmlString("First"),
                    IsDisabled         = false,
                    Id    = "first",
                    Value = "first"
                });

                checkboxesContext.SetIsConditional();

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

            var tagHelper = new CheckboxesTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper())
            {
                IdPrefix = "my-id",
                Name     = "testcheckboxes"
            };

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

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

            Assert.Equal(
                "<div class=\"govuk-form-group\">" +
                "<div class=\"govuk-checkboxes--conditional govuk-checkboxes\" data-module=\"govuk-checkboxes\">" +
                "<div class=\"govuk-checkboxes__item\">" +
                "<input checked=\"checked\" class=\"govuk-checkboxes__input\" data-aria-controls=\"conditional-first\" id=\"first\" name=\"testcheckboxes\" type=\"checkbox\" value=\"first\" />" +
                "<label class=\"govuk-checkboxes__label govuk-label\" for=\"first\">First</label>" +
                "</div>" +
                "<div class=\"govuk-checkboxes__conditional\" id=\"conditional-first\">Conditional</div>" +
                "</div>" +
                "</div>",
                html);
        }
Exemple #3
0
        public async Task ProcessAsync_WithCheckedItemConditional_GeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-checkboxes",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

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

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

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

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

            // Assert
            var expectedHtml = @"
<div class=""govuk-form-group"">
    <div class=""govuk-checkboxes"" data-module=""govuk-checkboxes"">
        <div class=""govuk-checkboxes__item"">
            <input class=""govuk-checkboxes__input"" id=""first"" name=""testcheckboxes"" type=""checkbox"" value=""first"" checked=""checked"" data-aria-controls=""conditional-first"" />
            <label class=""govuk-checkboxes__label govuk-label"" for=""first"">First</label>
        </div>
        <div class=""govuk-checkboxes__conditional"" id=""conditional-first"">Item 1 conditional</div>
    </div>
</div>";

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

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

                checkboxesContext.SetFieldset(new CheckboxesFieldset()
                {
                    LegendIsPageHeading = false,
                    LegendContent       = new HtmlString("Legend")
                });

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

            var tagHelper = new CheckboxesTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper())
            {
                DescribedBy = "describedby",
                IdPrefix    = "my-id",
                Name        = "testcheckboxes"
            };

            // 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-checkboxes\">" +
                "</div>" +
                "</fieldset>" +
                "</div>",
                html);
        }
Exemple #5
0
        public async Task ProcessAsync_CheckedItemGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-checkboxes",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

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

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

            var tagHelper = new CheckboxesTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper())
            {
                IdPrefix    = "my-id",
                DescribedBy = "describedby",
                Name        = "testcheckboxes"
            };

            // 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);
        }
Exemple #6
0
        public async Task ProcessAsync_WithErrorMessageGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-checkboxes",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

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

            var tagHelper = new CheckboxesTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper())
            {
                IdPrefix = "my-id",
                Name     = "testcheckboxes"
            };

            // 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-checkboxes\">" +
                "</div>" +
                "</div>",
                html);
        }
Exemple #7
0
        public async Task ProcessAsync_WithError_GeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-checkboxes",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

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

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

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

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

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

            // 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-checkboxes"" data-module=""govuk-checkboxes"">
        <div class=""govuk-checkboxes__item"">
            <input class=""govuk-checkboxes__input"" id=""first"" name=""testcheckboxes"" type=""checkbox"" value=""first"" aria-describedby=""my-id-error"" disabled=""disabled"" />
            <label class=""govuk-checkboxes__label govuk-label"" for=""first"">First</label>
        </div>
        <div class=""govuk-checkboxes__item"">
            <input class=""govuk-checkboxes__input"" id=""second"" name=""testcheckboxes"" type=""checkbox"" value=""second"" aria-describedby=""my-id-error"" checked=""checked"" />
            <label class=""govuk-checkboxes__label govuk-label"" for=""second"">Second</label>
        </div>
    </div>
</div>";

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

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

                checkboxesContext.OpenFieldset();
                var checkboxesFieldsetContext = new CheckboxesFieldsetContext(attributes: null);
                checkboxesFieldsetContext.SetLegend(isPageHeading: false, attributes: null, content: new HtmlString("Legend"));

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

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

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

                checkboxesContext.CloseFieldset(checkboxesFieldsetContext);

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

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

            // 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-checkboxes"" data-module=""govuk-checkboxes"">
            <div class=""govuk-checkboxes__item"">
                <input class=""govuk-checkboxes__input"" id=""first"" name=""testcheckboxes"" type=""checkbox"" value=""first"" disabled=""disabled"" />
                <label class=""govuk-checkboxes__label govuk-label"" for=""first"">First</label>
            </div>
            <div class=""govuk-checkboxes__item"">
                <input class=""govuk-checkboxes__input"" id=""second"" name=""testcheckboxes"" type=""checkbox"" value=""second"" checked=""checked"" />
                <label class=""govuk-checkboxes__label govuk-label"" for=""second"">Second</label>
            </div>
        </div>
    </fieldset>
</div>";

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