public async Task ProcessFormAsync_UsesExistingAddressIfMatchingFound()
        {
            var newAddress = new Address
            {
                FirstName = "testfirst",
                StreetAddress = "teststreetaddress"
            };

            var existingAddresses = new List<Address>
                                        {
                                            new Address
                                                {
                                                    Id = "testId",
                                                    FirstName = "testfirst",
                                                    StreetAddress = "teststreetaddress"
                                                }
                                        };

            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetAllBillingAddressesAsyncDelegate =
                () => Task.FromResult<ICollection<Address>>(new ReadOnlyCollection<Address>(existingAddresses));

            var target = new BillingAddressUserControlViewModel(checkoutDataRepository, null, null, null);
            target.Address = newAddress;

            await target.ProcessFormAsync();

            Assert.AreEqual("testId", target.Address.Id);
        }
        public void OnNavigateTo_LoadsDefault_IfTryLoadDefaultTrue()
        {
            var defaultAddress = new Address
            {
                FirstName = "FirstName",
                State = "WA"
            };
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetDefaultBillingAddressAsyncDelegate = () => Task.FromResult(defaultAddress);
            var locationService = new MockLocationService();
            var resourceLoader = new MockResourceLoader();
            var target = new BillingAddressUserControlViewModel(checkoutDataRepository, locationService, resourceLoader, null);

            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, new Dictionary<string, object>());
            Assert.IsNull(target.Address.FirstName);

            target.SetLoadDefault(true);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, new Dictionary<string, object>());
            Assert.AreEqual("FirstName", target.Address.FirstName);
        }
        private void FillWithDummyData()
        {
            ShippingAddressViewModel = new ShippingAddressUserControlViewModel(null, null, null, null)
            {
                Address = new Address()
                {
                    FirstName = "Name",
                    MiddleInitial = "M",
                    LastName = "Lastname",
                    StreetAddress = "12345 Main St NE",
                    City = "Seattle",
                    State = "Washington",
                    ZipCode = "54321",
                    Phone = "1234 5678 9876" 
                }
            };

            BillingAddressViewModel = new BillingAddressUserControlViewModel(null, null, null, null);
            UseSameAddressAsShipping = true;
            BillingAddressViewModel.IsEnabled = !UseSameAddressAsShipping;

            PaymentMethodViewModel = new PaymentMethodUserControlViewModel(null)
            {
                PaymentMethod = new PaymentMethod()
                {
                    CardholderName = "Name Lastname",
                    CardNumber = "1234",
                    CardVerificationCode = "54321",
                    ExpirationMonth = "10",
                    ExpirationYear = "2014",
                    Phone = "1234 5678 9876" 
                },
            };

            // Validation
            IsShippingAddressInvalid = ShippingAddressViewModel.Address.Errors.GetAllErrors().Count > 0;
            IsBillingAddressInvalid = BillingAddressViewModel.Address.Errors.GetAllErrors().Count > 0;
            IsPaymentMethodInvalid = PaymentMethodViewModel.PaymentMethod.Errors.GetAllErrors().Count > 0;
        }
        public async Task ProcessFormAsync_SavesAddressIfNoMatchingFound()
        {
            var saveAddressCalled = false;
            var newAddress = new Address
            {
                FirstName = "testfirst",
                StreetAddress = "teststreetaddress"
            };

            var existingAddresses = new List<Address>();
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetAllBillingAddressesAsyncDelegate =
                () => Task.FromResult<ICollection<Address>>(new Collection<Address>(existingAddresses));

            checkoutDataRepository.SaveBillingAddressAsyncDelegate = address =>
            {
                saveAddressCalled = true;
                Assert.AreEqual("teststreetaddress",
                                address.StreetAddress);
                return Task.Delay(0);
            };
            var target = new BillingAddressUserControlViewModel(checkoutDataRepository, null, null, null);
            target.Address = newAddress;

            await target.ProcessFormAsync();

            Assert.IsTrue(saveAddressCalled);
        }