Пример #1
0
        public async Task ValidationErrorUsesDisplayAttributeName()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <int, TestInputNumberComponent>
            {
                EditContext          = new EditContext(model),
                ValueExpression      = () => model.SomeNumber,
                AdditionalAttributes = new Dictionary <string, object>
                {
                    { "DisplayName", "Some number" }
                }
            };
            var fieldIdentifier = FieldIdentifier.Create(() => model.SomeNumber);
            var inputComponent  = await InputRenderer.RenderAndGetComponent(rootComponent);

            // Act
            await inputComponent.SetCurrentValueAsStringAsync("notANumber");

            // Assert
            var validationMessages = rootComponent.EditContext.GetValidationMessages(fieldIdentifier);

            Assert.NotEmpty(validationMessages);
            Assert.Contains("The Some number field must be a number.", validationMessages);
        }
Пример #2
0
        public async Task ParsesCurrentValueAsStringWhenChanged_Valid()
        {
            // Arrange
            var model            = new TestModel();
            var valueChangedArgs = new List <DateTime>();
            var rootComponent    = new TestInputHostComponent <DateTime, TestDateInputComponent>
            {
                EditContext     = new EditContext(model),
                ValueChanged    = valueChangedArgs.Add,
                ValueExpression = () => model.DateProperty
            };
            var fieldIdentifier = FieldIdentifier.Create(() => model.DateProperty);
            var inputComponent  = await InputRenderer.RenderAndGetComponent(rootComponent);

            var numValidationStateChanges = 0;

            rootComponent.EditContext.OnValidationStateChanged += (sender, eventArgs) => { numValidationStateChanges++; };

            // Act
            await inputComponent.SetCurrentValueAsStringAsync("1991/11/20");

            // Assert
            var receivedParsedValue = valueChangedArgs.Single();

            Assert.Equal(1991, receivedParsedValue.Year);
            Assert.Equal(11, receivedParsedValue.Month);
            Assert.Equal(20, receivedParsedValue.Day);
            Assert.True(rootComponent.EditContext.IsModified(fieldIdentifier));
            Assert.Empty(rootComponent.EditContext.GetValidationMessages(fieldIdentifier));
            Assert.Equal(0, numValidationStateChanges);
        }
Пример #3
0
        public async Task SuppliesFieldClassCorrespondingToFieldState()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                EditContext     = new EditContext(model),
                ValueExpression = () => model.StringProperty
            };
            var fieldIdentifier = FieldIdentifier.Create(() => model.StringProperty);

            // Act/Assert: Initially, it's valid and unmodified
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            Assert.Equal("valid", inputComponent.CssClass); //  no Class was specified

            // Act/Assert: Modify the field
            rootComponent.EditContext.NotifyFieldChanged(fieldIdentifier);
            Assert.Equal("modified valid", inputComponent.CssClass);

            // Act/Assert: Make it invalid
            var messages = new ValidationMessageStore(rootComponent.EditContext);

            messages.Add(fieldIdentifier, "I do not like this value");
            Assert.Equal("modified invalid", inputComponent.CssClass);

            // Act/Assert: Clear the modification flag
            rootComponent.EditContext.MarkAsUnmodified(fieldIdentifier);
            Assert.Equal("invalid", inputComponent.CssClass);

            // Act/Assert: Make it valid
            messages.Clear();
            Assert.Equal("valid", inputComponent.CssClass);
        }
Пример #4
0
        public async Task ValidationErrorUsesDisplayAttributeName()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <DateTime, TestInputDateComponent>
            {
                EditContext          = new EditContext(model),
                ValueExpression      = () => model.DateProperty,
                AdditionalAttributes = new Dictionary <string, object>
                {
                    { "DisplayName", "Date property" }
                }
            };
            var fieldIdentifier = FieldIdentifier.Create(() => model.DateProperty);
            var inputComponent  = await InputRenderer.RenderAndGetComponent(rootComponent);

            // Act
            await inputComponent.SetCurrentValueAsStringAsync("invalidDate");

            // Assert
            var validationMessages = rootComponent.EditContext.GetValidationMessages(fieldIdentifier);

            Assert.NotEmpty(validationMessages);
            Assert.Contains("The Date property field must be a date.", validationMessages);
        }
Пример #5
0
        public async Task ThrowsIfNoValueExpressionIsSupplied()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> > {
                EditContext = new EditContext(model)
            };

            // Act/Assert
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => InputRenderer.RenderAndGetComponent(rootComponent));

            Assert.Contains($"{typeof(TestInputComponent<string>)} requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'.", ex.Message);
        }
Пример #6
0
        public async Task SuppliesCurrentValueAsStringWithFormatting()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <DateTime, TestDateInputComponent>
            {
                EditContext     = new EditContext(model),
                Value           = new DateTime(1915, 3, 2),
                ValueExpression = () => model.DateProperty
            };
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            // Act/Assert
            Assert.Equal("1915/03/02", inputComponent.CurrentValueAsString);
        }
Пример #7
0
        public async Task ThrowsIfEditContextChanges()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> > {
                EditContext = new EditContext(model), ValueExpression = () => model.StringProperty
            };
            await InputRenderer.RenderAndGetComponent(rootComponent);

            // Act/Assert
            rootComponent.EditContext = new EditContext(model);
            var ex = Assert.Throws <InvalidOperationException>(() => rootComponent.TriggerRender());

            Assert.StartsWith($"{typeof(TestInputComponent<string>)} does not support changing the EditContext dynamically", ex.Message);
        }
Пример #8
0
        public async Task InputElementIsAssignedSuccessfully()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, InputTextArea>
            {
                EditContext     = new EditContext(model),
                ValueExpression = () => model.StringProperty,
            };

            // Act
            var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            // Assert
            Assert.NotNull(inputSelectComponent.Element);
        }
Пример #9
0
        public async Task CanRenderWithoutEditContext()
        {
            // Arrange
            var model         = new TestModel();
            var value         = "some value";
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                Value           = value,
                ValueExpression = () => value
            };

            // Act/Assert
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            Assert.Null(inputComponent.EditContext);
        }
Пример #10
0
        public async Task ParsesCurrentValueWhenUsingNotNullableEnumWithEmptyValue()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <TestEnum, TestInputSelect <TestEnum> >
            {
                EditContext     = new EditContext(model),
                ValueExpression = () => model.NotNullableEnum
            };
            var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            // Act
            inputSelectComponent.CurrentValueAsString = "";

            // Assert
            Assert.Equal(default, inputSelectComponent.CurrentValue);
Пример #11
0
        public async Task ExposesFieldIdentifierToSubclass()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                EditContext     = new EditContext(model),
                Value           = "some value",
                ValueExpression = () => model.StringProperty
            };

            // Act
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            // Assert
            Assert.Equal(FieldIdentifier.Create(() => model.StringProperty), inputComponent.FieldIdentifier);
        }
Пример #12
0
        public async Task GetsCurrentValueFromValueParameter()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                EditContext     = new EditContext(model),
                Value           = "some value",
                ValueExpression = () => model.StringProperty
            };

            // Act
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            // Assert
            Assert.Equal("some value", inputComponent.CurrentValue);
        }
Пример #13
0
        public async Task WritingToCurrentValueNotifiesEditContext()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                EditContext     = new EditContext(model),
                Value           = "initial value",
                ValueExpression = () => model.StringProperty
            };
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            Assert.False(rootComponent.EditContext.IsModified(() => model.StringProperty));

            // Act
            inputComponent.CurrentValue = "new value";

            // Assert
            Assert.True(rootComponent.EditContext.IsModified(() => model.StringProperty));
        }
Пример #14
0
        public async Task CanReadBackChangesToCurrentValue()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                EditContext     = new EditContext(model),
                Value           = "initial value",
                ValueExpression = () => model.StringProperty
            };
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            Assert.Equal("initial value", inputComponent.CurrentValue);

            // Act
            inputComponent.CurrentValue = "new value";

            // Assert
            Assert.Equal("new value", inputComponent.CurrentValue);
        }
Пример #15
0
        public async Task WritingToCurrentValueDoesNotInvokeValueChangedIfUnchanged()
        {
            // Arrange
            var model = new TestModel();
            var valueChangedCallLog = new List <string>();
            var rootComponent       = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                EditContext     = new EditContext(model),
                Value           = "initial value",
                ValueChanged    = val => valueChangedCallLog.Add(val),
                ValueExpression = () => model.StringProperty
            };
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            Assert.Empty(valueChangedCallLog);

            // Act
            inputComponent.CurrentValue = "initial value";

            // Assert
            Assert.Empty(valueChangedCallLog);
        }
Пример #16
0
        public async Task CssClassCombinesClassWithFieldClass()
        {
            // Arrange
            var model         = new TestModel();
            var rootComponent = new TestInputHostComponent <string, TestInputComponent <string> >
            {
                AdditionalAttributes = new Dictionary <string, object>()
                {
                    { "class", "my-class other-class" },
                },
                EditContext     = new EditContext(model),
                ValueExpression = () => model.StringProperty
            };
            var fieldIdentifier = FieldIdentifier.Create(() => model.StringProperty);

            // Act/Assert
            var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

            Assert.Equal("my-class other-class valid", inputComponent.CssClass);

            // Act/Assert: Retains custom class when changing field class
            rootComponent.EditContext.NotifyFieldChanged(fieldIdentifier);
            Assert.Equal("my-class other-class modified valid", inputComponent.CssClass);
        }