public async Task ProcessAsync_WithIdSpecifiedGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-error-message",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

            var htmlHelper = new Mock <IHtmlHelper>();

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo");

            var tagHelper = new ErrorMessageTagHelper(new DefaultGovUkHtmlGenerator())
            {
                Id = "some-id"
            };

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

            // Assert
            Assert.Equal("some-id", output.Attributes["id"].Value);
        }
Ejemplo n.º 2
0
        public async Task ProcessAsync_NoAspForOrContent_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-error-message",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

            output.TagMode = TagMode.SelfClosing;

            var htmlHelper = new Mock <IHtmlHelper>();

            var tagHelper = new ErrorMessageTagHelper();

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Cannot determine content. Element must contain content if the 'asp-for' attribute is not specified.", ex.Message);
        }
Ejemplo n.º 3
0
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-error-message",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

            var tagHelper = new ErrorMessageTagHelper();

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

            // Assert
            var expectedHtml = @"
<p class=""govuk-error-message"">
    <span class=""govuk-visually-hidden"">Error:</span>
    An error!
</p>";

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

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

            output.TagMode = TagMode.SelfClosing;

            var htmlGenerator = new Mock <DefaultGovUkHtmlGenerator>()
            {
                CallBase = true
            };

            htmlGenerator
            .Setup(mock => mock.GetValidationMessage(
                       /*viewContext: */ It.IsAny <ViewContext>(),
                       /*modelExplorer: */ It.IsAny <ModelExplorer>(),
                       /*expression: */ It.IsAny <string>()))
            .Returns("An error!");

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo");

            var tagHelper = new ErrorMessageTagHelper(htmlGenerator.Object)
            {
                AspFor      = new ModelExpression("Foo", modelExplorer),
                ViewContext = new ViewContext()
            };

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

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

            Assert.Equal(
                "<span class=\"govuk-error-message\">" +
                "<span class=\"govuk-visually-hidden\">Error</span>" +
                "An error!" +
                "</span>",
                html);
        }
Ejemplo n.º 5
0
        public async Task ProcessAsync_AspForSpecified_GeneratesExpectedContent()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-error-message",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

            output.TagMode = TagMode.SelfClosing;

            var modelHelperMock = new Mock <IModelHelper>();

            modelHelperMock
            .Setup(mock => mock.GetValidationMessage(
                       /*viewContext: */ It.IsAny <ViewContext>(),
                       /*modelExplorer: */ It.IsAny <ModelExplorer>(),
                       /*expression: */ It.IsAny <string>()))
            .Returns("An error!");

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), new Model())
                                .GetExplorerForProperty(nameof(Model.SimpleProperty));

            var tagHelper = new ErrorMessageTagHelper(htmlGenerator: null, modelHelperMock.Object)
            {
                AspFor      = new ModelExpression(nameof(Model.SimpleProperty), modelExplorer),
                ViewContext = new ViewContext()
            };

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

            // Assert
            var expectedHtml = @"
<p class=""govuk-error-message"">
    <span class=""govuk-visually-hidden"">Error:</span>
    An error!
</p>";

            AssertEx.HtmlEqual(expectedHtml, output.RenderToString());
        }
Ejemplo n.º 6
0
        public async Task ProcessAsync_AspForSpecifiedButNoError_GeneratesNoOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-error-message",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

            output.TagMode = TagMode.SelfClosing;

            var modelHelperMock = new Mock <IModelHelper>();

            modelHelperMock
            .Setup(mock => mock.GetValidationMessage(
                       /*viewContext: */ It.IsAny <ViewContext>(),
                       /*modelExplorer: */ It.IsAny <ModelExplorer>(),
                       /*expression: */ It.IsAny <string>()))
            .Returns((string)null);

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), new Model())
                                .GetExplorerForProperty(nameof(Model.SimpleProperty));

            var tagHelper = new ErrorMessageTagHelper(new ComponentGenerator(), modelHelperMock.Object)
            {
                AspFor      = new ModelExpression(nameof(Model.SimpleProperty), modelExplorer),
                ViewContext = new ViewContext()
            };

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

            // Assert
            Assert.Null(output.TagName);
        }
        public async Task ProcessAsync_WithVisuallyHiddenTextSpecifiedGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-error-message",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

            var htmlHelper = new Mock <IHtmlHelper>();

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo");

            var tagHelper = new ErrorMessageTagHelper(new DefaultGovUkHtmlGenerator())
            {
                VisuallyHiddenText = "Overriden"
            };

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

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

            Assert.Equal(
                "<span class=\"govuk-error-message\">" +
                "<span class=\"govuk-visually-hidden\">Overriden</span>" +
                "An error!" +
                "</span>",
                html);
        }