public async Task ProcessFormAsync_UsesExistingIfMatchingFound()
        {
            var newPaymentMethod = new PaymentMethod
            {
                CardNumber = "1234",
                CardholderName = "testcardholdername"
            };

            var existingPaymentMethods = new List<PaymentMethod>
                                        {
                                            new PaymentMethod
                                                {
                                                    Id = "testId",
                                                    CardNumber = "1234",
                                                    CardholderName = "testcardholdername"
                                                }
                                        };

            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetAllPaymentMethodsAsyncDelegate =
                () => Task.FromResult<ICollection<PaymentMethod>>(new ReadOnlyCollection<PaymentMethod>(existingPaymentMethods));

            var target = new PaymentMethodUserControlViewModel(checkoutDataRepository);
            target.PaymentMethod = newPaymentMethod;

            await target.ProcessFormAsync();

            Assert.AreEqual("testId", target.PaymentMethod.Id);
        }
        public void OnNavigateTo_LoadsDefault_IfTryLoadDefaultTrue()
        {
            var defaultPaymentMethod = new PaymentMethod
            {
                CardholderName = "CardHolderName",
                CardNumber = "32323232",
                CardVerificationCode = "222",
                Phone = "22224232",
                ExpirationMonth = "12",
                ExpirationYear = "2016"
            };
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetDefaultPaymentMethodAsyncDelegate = () => Task.FromResult(defaultPaymentMethod);
            var target = new PaymentMethodUserControlViewModel(checkoutDataRepository);

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

            target.SetLoadDefault(true);
            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, new Dictionary<string, object>());
            Assert.AreEqual("CardHolderName", target.PaymentMethod.CardholderName);
        }
        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_SavesPaymentMethodIfNoMatchingFound()
        {
            var savePaymentMethodCalled = false;
            var newPaymentMethod = new PaymentMethod
            {
                CardNumber = "1234",
                CardholderName = "testcardholdername"
            };

            var existingPaymentMethods = new List<PaymentMethod>();
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetAllPaymentMethodsAsyncDelegate =
                () => Task.FromResult<ICollection<PaymentMethod>>(new Collection<PaymentMethod>(existingPaymentMethods));

            checkoutDataRepository.SavePaymentMethodAsyncDelegate = paymentMethod =>
            {
                savePaymentMethodCalled = true;
                Assert.AreEqual("testcardholdername",
                                paymentMethod.CardholderName);
                return Task.Delay(0);
            };
            var target = new PaymentMethodUserControlViewModel(checkoutDataRepository);
            target.PaymentMethod = newPaymentMethod;

            await target.ProcessFormAsync();

            Assert.IsTrue(savePaymentMethodCalled);
        }