Ejemplo n.º 1
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);
    }
Ejemplo n.º 2
0
    public async Task ParsesCurrentValueAsStringWhenChanged_Invalid()
    {
        // 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/Assert 1: Transition to invalid
        await inputComponent.SetCurrentValueAsStringAsync("1991/11/40");

        Assert.Empty(valueChangedArgs);
        Assert.True(rootComponent.EditContext.IsModified(fieldIdentifier));
        Assert.Equal(new[] { "Bad date value" }, rootComponent.EditContext.GetValidationMessages(fieldIdentifier));
        Assert.Equal(1, numValidationStateChanges);

        // Act/Assert 2: Transition to valid
        await inputComponent.SetCurrentValueAsStringAsync("1991/11/20");

        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(2, numValidationStateChanges);
    }
Ejemplo n.º 3
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);
    }