public void Refresh_OfDelegateProperty_CallsNotifyChangeWhenViewModelInstanceHasChanged()
        {
            var oldChild = new ChildVM();
            var newChild = new ChildVM();

            VM.DelegatePropertyResult = oldChild;
            VM.Load(x => x.DelegateProperty);

            VM.DelegatePropertyResult = newChild;
            VM.OnChangeInvocations.Clear();

            VM.Refresh(x => x.DelegateProperty);

            var expectedArgs = ChangeArgs
                               .ViewModelPropertyChanged(
                RootVM.ClassDescriptor.DelegateProperty,
                ValueStage.ValidatedValue,
                oldChild,
                newChild)
                               .PrependViewModel(VM);

            DomainAssert.AreEqual(
                new[] { expectedArgs },
                VM.OnChangeInvocations
                );
        }
예제 #2
0
        public void Refresh_WorksWithSelfRecursiveDependency()
        {
            var root = CreateRootVM(b => {
                b.OnChangeOf
                .Self.OrAnyDescendant
                .Refresh
                .Properties(x => x.Child1, x => x.Child2);
            });

            var gc1 = new ChildVM();
            var gc2 = new ChildVM();

            root.Child1.Grandchildren.Add(gc1);
            root.Child2.Grandchildren.Add(gc2);

            gc1.StringPropertyRefreshCount = 0;
            gc2.StringPropertyRefreshCount = 0;

            root.Child1.Refresh(x => x.Grandchildren, executeRefreshDependencies: false);

            Assert.AreEqual(1, gc1.StringPropertyRefreshCount);
            Assert.AreEqual(0, gc2.StringPropertyRefreshCount);

            gc1.StringPropertyRefreshCount = 0;
            gc2.StringPropertyRefreshCount = 0;

            root.Refresh(executeRefreshDependencies: false);

            Assert.AreEqual(1, gc1.StringPropertyRefreshCount);
            Assert.AreEqual(1, gc2.StringPropertyRefreshCount);
        }
예제 #3
0
        public void SetValue_WhenValidationResultChanges_WorksWithSelfRecursiveDependency()
        {
            var root = CreateRootVM(b => {
                b.OnChangeOf
                .Self.OrAnyDescendant
                .Refresh
                .Self();
            });

            var gc1 = new ChildVM();
            var gc2 = new ChildVM();

            root.Child1.Grandchildren.Add(gc1);
            root.Child2.Grandchildren.Add(gc2);

            gc1.StringPropertyValidationError = "Test error";

            gc1.StringPropertyRefreshCount = 0;
            gc2.StringPropertyRefreshCount = 0;

            gc1.SetValue(x => x.StringProperty, "Trigger");

            // TODO: This should ideally be 1 with an advanced implementation, but it
            // should be true if the current implementation would be correct (see
            // 'RefreshAction').
            Assert.AreEqual(1, gc1.StringPropertyRefreshCount);
            Assert.AreEqual(1, gc2.StringPropertyRefreshCount);
        }
예제 #4
0
        public void Refresh_IgnoresCollectionDependencies()
        {
            var root = CreateRootVM(b => {
                b.OnChangeOf
                .Descendant(x => x.Child1)
                .Collection(x => x.Grandchildren)
                .Refresh
                .Descendant(x => x.Child2)
                .Properties(x => x.Grandchildren);

                b.OnChangeOf
                .Descendant(x => x.Child2)
                .Descendant(x => x.Grandchildren)
                .Properties(x => x.StringProperty)
                .Refresh
                .Properties(x => x.Property1);
            });

            var trigger = new ChildVM();
            var monitor = new ChildVM();

            root.Child2.Grandchildren.Add(monitor);

            root.Property1RefreshCount = 0;

            root.Child1.Grandchildren.Add(trigger);

            Assert.AreEqual(0, root.Property1RefreshCount);
        }
        public void IsUnique_WithDuplicateItemViewModels_InvalidatesDuplicateItems()
        {
            var error = "Duplicate item";
            var vm    = CreateParent(b => b
                                     .CheckCollection(x => x.Children)
                                     .IsUnique(error)
                                     );

            var item1 = new ChildVM()
            {
                IntegerProperty = 1, StringProperty = "Item1"
            };

            vm.Children.Add(item1);

            Assert.IsTrue(vm.IsValid);

            vm.Children.Add(item1);

            var expectedResult = CreateValidationResult(
                Error(error).For(item1),
                Error(error).For(item1)
                );

            ValidationAssert.AreEqual(expectedResult, vm.ValidationResult);
        }
        public void IsUnique_WithDuplicateStringProperties_InvalidatesViewModel()
        {
            var error = "Duplicate item";
            var vm    = CreateParent(b => b
                                     .CheckCollection(x => x.Children, x => x.StringProperty)
                                     .IsUnique(StringComparison.CurrentCultureIgnoreCase, error)
                                     );

            var item1 = new ChildVM("Item 1")
            {
                StringProperty = "VAL1"
            };
            var item2 = new ChildVM("Item 2")
            {
                StringProperty = "VAL2"
            };
            var item3 = new ChildVM("Item 3")
            {
                StringProperty = "VAL3"
            };

            vm.Children.Add(item1);
            vm.Children.Add(item2);
            vm.Children.Add(item3);

            ValidationAssert.IsValid(vm);
            item3.StringProperty = "VAL2";

            var expectedResult = CreateValidationResult(
                Error(error).For(item3, x => x.StringProperty),
                Error(error).For(item2, x => x.StringProperty)
                );

            ValidationAssert.AreEqual(expectedResult, vm.ValidationResult);
        }
예제 #7
0
        public void Refresh_OfCollectionProperty_RevalidatesItems()
        {
            ParameterizedTest
            .TestCase("InstanceProperty", new Func <RootVMDescriptor, IVMPropertyDescriptor <IVMCollection <ChildVM> > >(x => x.InstanceProperty))
            .TestCase("WrapperProperty", x => x.WrapperProperty)
            .TestCase("PopulatedProperty", x => x.PopulatedProperty)
            .Run(propertySelector => {
                var collection = VM.GetValue(propertySelector);
                var item       = new ChildVM(new ChildSource());
                collection.Add(item);
                VM.PopulatedPropertyResult = new List <ChildVM> {
                    item
                };

                VM.ValidatorResults.Reset();
                VM.Refresh(propertySelector);
                VM.ValidatorResults.VerifySetupValidationResults();

                VM.ValidatorResults.SetupFailing().CollectionPropertyValidation
                .Targeting(item, x => x.ChildProperty)
                .On(VM);
                VM.Refresh(propertySelector);
                VM.ValidatorResults.VerifySetupValidationResults();
            });
        }
예제 #8
0
        public void ChildVM_To_BaseEntity_WithRedirectViaToDialogItem()
        {
            var childvm = new ChildVM(new Child()
            {
                Name  = "__child",
                Hobby = "__hobby",
                Age   = 8,
            });

            var withMappingVM = new BackendClassContainingMappingVM(new WithMapping(new List <Mapping>()
            {
                Mapping.Create("key123", "value123")
            })
            {
                Name  = "__backend",
                Hobby = "__mapping",
                Age   = 8,
            });

            BaseDialogItem child = childvm.ToDialogItem();

            Assert.IsType <Child>(child);

            Parent parent = childvm.Map();

            WithMapping backend      = withMappingVM.Map();
            Child       backendChild = withMappingVM.Map();

            Assert.True(backend.Mappings.Count == 1);
            Assert.True(backend.Mappings[0].Key == "key123");
        }
예제 #9
0
        public static bool AreEqual(Child expected, ChildVM actual)
        {
            AreEqual(expected.Id, actual.Id, "Child id didn't match");
            AreEqual(expected.FirstName, actual.FirstName, "Child first name didn't match");
            AreEqual(expected.LastName, actual.LastName, "Child last name didn't match");

            return(true);
        }
예제 #10
0
        public void InsertItm()
        {
            ChildVM thirdChild = new ChildVM();

            thirdChild.InitializeFrom(_thirdChildSource);
            _parent.MappedCollectionAccessor.Insert(1, thirdChild);
            CompareCollections();
        }
예제 #11
0
        public void AddItem()
        {
            ChildVM thirdChild = new ChildVM();

            thirdChild.InitializeFrom(_thirdChildSource);
            _parent.MappedCollectionAccessor.Add(thirdChild);
            CompareCollections();
        }
        public void Refresh_OfInstanceProperty_CallsRefreshOnChildValue()
        {
            var child = new ChildVM();

            VM.SetValue(x => x.InstanceProperty, child);

            VM.Refresh(x => x.InstanceProperty);
            Assert.IsTrue(child.WasRefreshed);
        }
        public void Refresh_OfDelegateProperty_DoesNotRefreshReturnedChildVM()
        {
            var childVM = new ChildVM();

            VM.SetValue(x => x.DelegateProperty, childVM);

            VM.Refresh(x => x.DelegateProperty);
            Assert.IsFalse(childVM.WasRefreshed);
        }
예제 #14
0
        public void Refresh_OfInstanceCollectionProperty_CallsRefreshOnAllItems()
        {
            ChildVM item = new ChildVM();

            VM.InstanceProperty.Add(item);

            VM.Refresh(x => x.InstanceProperty);
            Assert.IsTrue(item.WasRefreshed);
        }
예제 #15
0
        public void ForwardChangeNotificationToParents_WhenParentsChangingDuringNotification_DoesntThrowException()
        {
            var parent = new ParentVM();
            var child  = new ChildVM();

            parent.SetValue(x => x.Child, child);

            child.TriggerChange();
        }
        public void Refresh_OfDelegateProperty_ResetsPropertyValueByCallingGetterDelegate()
        {
            var previousChildVM = VM.GetValue(x => x.DelegateProperty);
            var newChildVM      = new ChildVM();

            VM.DelegatePropertyResult = newChildVM;

            VM.Refresh(x => x.DelegateProperty);
            Assert.AreEqual(newChildVM, VM.GetValue(x => x.DelegateProperty));
        }
예제 #17
0
        public void Refresh_OfPopulatedCollectionProperty_DoesNotRefreshReturnedItems()
        {
            var itemVM = new ChildVM();

            VM.PopulatedPropertyResult = new[] { itemVM };
            VM.Load(x => x.PopulatedProperty);

            VM.Refresh(x => x.PopulatedProperty);
            Assert.IsFalse(itemVM.WasRefreshed);
        }
        private ValidationResult SetupChildVMValidationError()
        {
            var result = CreateValidationResult("Child error");

            var child = new ChildVM();

            child.ReturnedValidationResults[ValidationResultScope.All] = result;
            ViewModelPropertyAccessor.Value = child;
            return(result);
        }
        public void Refresh_RefreshesChildVM()
        {
            var refreshDetector = new RefreshDetectorBehavior();
            var child           = new ChildVM(refreshDetector);

            Behavior.SetValue(Context, child);

            InvokeRefresh();
            Assert.IsTrue(refreshDetector.WasCalled);
        }
        public void RefreshContainer_OfWrapperPropertyIfSourceHasNotChanged_DoesNotRefreshChild()
        {
            VM.Revalidate(ValidationScope.SelfAndAllDescendants);

            var child = new ChildVM(new ChildSource());

            VM.SetValue(x => x.WrapperProperty, child);

            VM.RefreshContainer(x => x.WrapperProperty);
            Assert.IsFalse(child.WasRefreshed);
        }
        public void SetValueNext_ToNull_RevalidatesOldChildVM()
        {
            ChildVM oldChild = new ChildVM();
            ChildVM newChild = null;

            ViewModelPropertyAccessor.Value = oldChild;

            Behavior.SetValue(Context, newChild);

            Assert.IsTrue(oldChild.RevalidationBehavior.WasCalled);
        }
예제 #22
0
        public void RefreshContainer_OfWrapperCollection_DoesNotRefreshItems()
        {
            VM.Revalidate(ValidationScope.SelfAndAllDescendants);

            var child = new ChildVM(new ChildSource());

            VM.WrapperProperty.Add(child);

            VM.RefreshContainer(x => x.WrapperProperty);

            Assert.IsFalse(child.WasRefreshed);
        }
예제 #23
0
        public void RevalidateSelf_ForChildPropertyWithPropertyValidator_LoadsChildOnlyIfAccessedByValidator()
        {
            VM.PropertyValidationAction = (args) => { };
            VM.Revalidate(ValidationScope.Self);
            Assert.IsFalse(VM.IsLoaded(x => x.ChildWithPropertyValidator));

            ChildVM propertyWhileValidatingValue = null;

            VM.PropertyValidationAction = (args) => { propertyWhileValidatingValue = args.Value; };
            VM.Revalidate(ValidationScope.Self);

            Assert.IsTrue(VM.IsLoaded(x => x.ChildWithPropertyValidator));
            Assert.AreEqual(VM.GetValue(x => x.ChildWithPropertyValidator), propertyWhileValidatingValue);
        }
예제 #24
0
        public void Refresh_ViewModelInstanceHasChanged_RaisesNotifyChange()
        {
            var oldChild = new ChildVM();
            var newChild = new ChildVM();

            ValueAccessor.Value = oldChild;
            Behavior.GetValue(Context);
            ValueAccessor.Value = newChild;
            InvokeRefresh();

            var expectedChangeArgs = ChangeArgs.ViewModelPropertyChanged(Property, ValueStage.ValidatedValue, oldChild, newChild, null);

            DomainAssert.AreEqual(new[] { expectedChangeArgs }, Context.NotifyChangeInvocations);
        }
        [TestMethod] // regression test
        public void SetValue_WhenOldChildGetsInvalidAndNewChildIsValid_ParentValidationResultIsValid()
        {
            var parent = new ParentVM();

            var oldChild = new ChildVM();
            var newChild = new ChildVM();

            parent.SetValue(x => x.Child, oldChild);

            oldChild.ChildPropertyErrorMessage = "Old validation error";
            parent.SetValue(x => x.Child, newChild);

            ValidationAssert.IsValid(parent.ValidationResult);
        }
예제 #26
0
        public ActionResult CreateAChild()
        {
            ChildVM vm = new ChildVM();

            vm.ParentDropDown.Add(new SelectListItem()
            {
                Text = "Choose a parent", Value = "0"
            });
            foreach (ParentDO parent in parentDataAccess.ViewParents())
            {
                vm.ParentDropDown.Add(new SelectListItem {
                    Text = parent.FirstName + " " + parent.LastName, Value = parent.ParentID.ToString()
                });
            }
            return(View(vm));
        }
        public void SetValue_ToNewChild_RevalidatesOldAndNewChild()
        {
            var vm       = new ParentVM();
            var oldChild = new ChildVM();
            var newChild = new ChildVM();

            vm.SetValue(x => x.Child, oldChild);

            oldChild.ChildPropertyErrorMessage = "Old validation error";
            newChild.ChildPropertyErrorMessage = "New validation error";

            vm.SetValue(x => x.Child, newChild);

            ValidationAssert.ErrorMessages(oldChild.ValidationResult, oldChild.ChildPropertyErrorMessage);
            ValidationAssert.ErrorMessages(newChild.ValidationResult, newChild.ChildPropertyErrorMessage);
        }
        public void SetValue_CallsNotifyChange()
        {
            var oldChild = new ChildVM();
            var newChild = new ChildVM();

            VM.SetValue(x => x.InstanceProperty, oldChild);

            VM.OnChangeInvocations.Clear();
            VM.SetValue(x => x.InstanceProperty, newChild);

            var args = VM.OnChangeInvocations.FirstOrDefault();

            Assert.IsNotNull(args);
            CollectionAssert.AreEqual(new[] { oldChild }, args.OldItems.ToArray());
            CollectionAssert.AreEqual(new[] { newChild }, args.NewItems.ToArray());
        }
예제 #29
0
        public void Refresh_RefresesSelectedDescendants()
        {
            var root  = new RootVM();
            var child = new ChildVM();
            var item1 = new ChildVM();

            root.Child = child;
            root.Children.Add(item1);

            root.RefreshDescendants(b => {
                b.Descendant(x => x.Children)
                .Properties(x => x.RefreshDetectionProperty);
            });

            Assert.IsTrue(item1.WasRefreshed);
            Assert.IsFalse(child.WasRefreshed);
        }
예제 #30
0
        public ActionResult CreateAChild(ChildVM form)
        {
            ActionResult result = null;

            if (ModelState.IsValid)
            {
                ChildDO mappedData = Mapper.MapChildPOToDO(form.ChildForm);
                dataAccess.CreateAChild(mappedData);
                result = RedirectToAction("ViewAllChildren");
            }
            else
            {
                //Re-Populate your drop down list.
                result = View(form);
            }
            return(result);
        }