public void GenerateLabel_LabelContentOnContextAndAspFor_ReturnsContentFromContext()
        {
            // Arrange
            var formGroupContext = new TestFormGroupContext();

            formGroupContext.SetLabel(isPageHeading: false, attributes: null, content: new HtmlString("Context label"));

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

            var modelHelper = new Mock <IModelHelper>();

            modelHelper.Setup(mock => mock.GetDisplayName(viewContext, modelExplorer, modelExpression)).Returns("ModelMetadata label");

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                AspFor      = new ModelExpression(modelExpression, modelExplorer),
                Id          = "test",
                ViewContext = viewContext
            };

            // Act
            var label = tagHelper.GenerateLabel(formGroupContext);

            // Assert
            Assert.NotNull(label);
            Assert.Contains("Context label", label.RenderToString());
        }
        public void GenerateHint_NonEmptyHint_AddsHintIdToDescribedBy()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            formGroupContext.SetHint(attributes: null, content: new HtmlString("Hint"));

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            var hint = tagHelper.GenerateHint(tagHelperContext, formGroupContext);

            // Assert
            var element = hint.RenderToElement();

            Assert.Equal("test-hint", element.GetAttribute("id"));
            Assert.Contains("test-hint", tagHelper.DescribedBy?.Split(' ') ?? Array.Empty <string>());
        }
        public void GenerateHint_NonEmptyModelMetadataDescription_ReturnsContent()
        {
            // Arrange
            var formGroupContext = new TestFormGroupContext();

            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

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

            var modelHelper = new Mock <IModelHelper>();

            modelHelper.Setup(mock => mock.GetDescription(modelExplorer)).Returns("ModelState hint");

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                AspFor      = new ModelExpression(modelExpression, modelExplorer),
                Id          = "test",
                ViewContext = viewContext
            };

            // Act
            var hint = tagHelper.GenerateHint(tagHelperContext, formGroupContext);

            // Assert
            Assert.NotNull(hint);
        }
        public void GenerateErrorMessage_ErrorMessageSetOnContext_ReturnsContent()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            formGroupContext.SetErrorMessage(visuallyHiddenText: "vht", attributes: null, content: new HtmlString("Error message"));

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            var errorMessage = tagHelper.GenerateErrorMessage(tagHelperContext, formGroupContext);

            // Assert
            Assert.NotNull(errorMessage);
        }
        public void GenerateHint_HintOnContext_ReturnsContent()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            formGroupContext.SetHint(attributes: null, content: new HtmlString("Hint"));

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            var hint = tagHelper.GenerateHint(tagHelperContext, formGroupContext);

            // Assert
            Assert.NotNull(hint);
        }
        public void GenerateErrorMessage_NoErrorMessageOnContextOrModelStateErrors_ReturnsNull()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), new Model())
                                .GetExplorerForProperty(nameof(Model.SimpleProperty));
            var viewContext     = new ViewContext();
            var modelExpression = "Foo";

            var modelHelper = new Mock <IModelHelper>();

            modelHelper.Setup(mock => mock.GetValidationMessage(viewContext, modelExplorer, modelExpression)).Returns((string)null);

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                AspFor      = new ModelExpression(modelExpression, modelExplorer),
                Id          = "test",
                ViewContext = viewContext
            };

            // Act
            var errorMessage = tagHelper.GenerateErrorMessage(tagHelperContext, formGroupContext);

            // Assert
            Assert.Null(errorMessage);
        }
        public void GenerateLabel_NoLabelContentOnContextOrAspFor_ThrowsInvalidOperationException()
        {
            // Arrange
            var formGroupContext = new TestFormGroupContext();

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            var ex = Record.Exception(() => tagHelper.GenerateLabel(formGroupContext));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Label content must be specified when the 'asp-for' attribute is not specified.", ex.Message);
        }
        public void GenerateLabel_LabelContentOnContext_ReturnsContentFromContext()
        {
            // Arrange
            var formGroupContext = new TestFormGroupContext();

            formGroupContext.SetLabel(isPageHeading: false, attributes: null, content: new HtmlString("Context label"));

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            var label = tagHelper.GenerateLabel(formGroupContext);

            // Assert
            Assert.NotNull(label);
            Assert.Contains("Context label", label.RenderToString());
        }
        public void GenerateErrorMessage_AddsErrorToFormErrorContext()
        {
            // Arrange
            var formErrorContext = new FormErrorContext();

            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(FormErrorContext), formErrorContext }
            },
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            formGroupContext.SetErrorMessage(visuallyHiddenText: "vht", attributes: null, content: new HtmlString("Error message"));

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            tagHelper.GenerateErrorMessage(tagHelperContext, formGroupContext);

            // Assert
            Assert.Collection(
                formErrorContext.Errors,
                error =>
            {
                Assert.Equal("Error message", error.Content.RenderToString());
            });
        }
        public void GenerateHint_EmptyHint_DoesNotAddHintIdToDescribedBy()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            tagHelper.GenerateHint(tagHelperContext, formGroupContext);

            // Assert
            Assert.DoesNotContain("test-hint", tagHelper.DescribedBy?.Split(' ') ?? Array.Empty <string>());
        }
        public void GenerateHint_NoHintOnContextOrModelMetadataDescription_ReturnsNull()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            var hint = tagHelper.GenerateHint(tagHelperContext, formGroupContext);

            // Assert
            Assert.Null(hint);
        }
        public void GenerateErrorMessage_NoErrorMessageOnContextOrAspFor_ReturnsNull()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            var modelHelper = new Mock <IModelHelper>();

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                Id = "test"
            };

            // Act
            var errorMessage = tagHelper.GenerateErrorMessage(tagHelperContext, formGroupContext);

            // Assert
            Assert.Null(errorMessage);
        }
        public void GenerateErrorMessage_ErrorMessageOnContextAndModelStateUsesContextError_ReturnsContent()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                tagName: "test",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var formGroupContext = new TestFormGroupContext();

            formGroupContext.SetErrorMessage(visuallyHiddenText: "vht", attributes: null, content: new HtmlString("Context error"));

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

            var modelHelper = new Mock <IModelHelper>();

            modelHelper.Setup(mock => mock.GetValidationMessage(viewContext, modelExplorer, modelExpression)).Returns("ModelState error");

            var tagHelper = new TestFormGroupTagHelper(new ComponentGenerator(), modelHelper.Object)
            {
                AspFor      = new ModelExpression(modelExpression, modelExplorer),
                Id          = "test",
                ViewContext = viewContext
            };

            // Act
            var errorMessage = tagHelper.GenerateErrorMessage(tagHelperContext, formGroupContext);

            // Assert
            Assert.NotNull(errorMessage);
            Assert.Contains("Context error", errorMessage.RenderToString());
        }