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 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);
        }
        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);
        }
        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);
        }
        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);
        }
        public async Task Checkout_WhenAnonymous_ShowsSignInFlyout()
        {
            var modalCalled = false;
            var accountService = new MockAccountService
                {
                    VerifyUserAuthenticationAsyncDelegate = () => Task.FromResult<UserInfo>(null)
                };
            var signInUserControlViewModel = new MockSignInUserControlViewModel
                {
                    OpenDelegate = (s) =>
                        {
                            modalCalled = true;
                            Assert.IsNotNull(s);
                        }
                };
            var checkoutDataRepository = new MockCheckoutDataRepository()
                {
                    GetDefaultShippingAddressAsyncDelegate = () => null,
                    GetDefaultBillingAddressAsyncDelegate =  () => null,
                    GetDefaultPaymentMethodAsyncDelegate = () => Task.FromResult<PaymentMethod>(null)
                };
            var eventAggregator = new MockEventAggregator
                {
                    GetEventDelegate = type => new MockShoppingCartUpdatedEvent()
                };
            var target = new ShoppingCartPageViewModel(null, new MockNavigationService(), accountService, signInUserControlViewModel, null, null, checkoutDataRepository, null, eventAggregator);

            await target.CheckoutCommand.Execute();

            Assert.IsTrue(modalCalled);
        }
        public void NavigatingToWhenNoShippingMethodSelected_RecalculatesOrder()
        {
            var shippingMethods = new List<ShippingMethod> { new ShippingMethod { Id = 1, Cost = 0 } };
            var shoppingCartItems = new List<ShoppingCartItem> { new ShoppingCartItem { Quantity = 1, Currency = "USD", Product = new Product() } };
            var order = new Order
                {
                ShoppingCart = new ShoppingCart(shoppingCartItems) { Currency = "USD", TotalPrice = 100 },
                ShippingAddress = new Address { Id = "1"},
                BillingAddress = new Address { Id = "1" },
                PaymentMethod = new PaymentMethod() { CardNumber = "1234" },
                ShippingMethod = null
            };
            var shippingMethodService = new MockShippingMethodService
            {
                GetShippingMethodsAsyncDelegate = () => Task.FromResult<IEnumerable<ShippingMethod>>(shippingMethods)
            };
            var orderRepository = new MockOrderRepository { CurrentOrder = order };
            var shoppingCartRepository = new MockShoppingCartRepository
                {
                    GetShoppingCartAsyncDelegate = () => Task.FromResult(order.ShoppingCart)
                };
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetShippingAddressAsyncDelegate = s => Task.FromResult(new Address());
            checkoutDataRepository.GetBillingAddressAsyncDelegate = s => Task.FromResult(new Address());
            checkoutDataRepository.GetPaymentMethodDelegate = s => Task.FromResult(new PaymentMethod { CardNumber = "1234" });
            var target = new CheckoutSummaryPageViewModel(new MockNavigationService(), new MockOrderService(), orderRepository, shippingMethodService,
                                                          checkoutDataRepository, shoppingCartRepository,
                                                          new MockAccountService(), new MockResourceLoader(), null, null);

            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, null);

            Assert.AreEqual("$0.00", target.ShippingCost);
            Assert.AreEqual("$100.00", target.OrderSubtotal);
            Assert.AreEqual("$100.00", target.GrandTotal);
        }
        public void SelectCheckoutData_Opens_AppBar()
        {
            var shippingMethods = new List<ShippingMethod>() { new ShippingMethod() { Id = 1, Cost = 0 } };
            var shoppingCartItems = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1, Currency = "USD", Product = new Product() } };
            var order = new Order()
            {
                ShoppingCart = new ShoppingCart(shoppingCartItems) { Currency = "USD", FullPrice = 100 },
                ShippingAddress = new Address(),
                BillingAddress = new Address(),
                PaymentMethod = new PaymentMethod() { CardNumber = "1234" },
                ShippingMethod = shippingMethods.First()
            };
            var shippingMethodService = new MockShippingMethodService()
            {
                GetShippingMethodsAsyncDelegate = () => Task.FromResult<IEnumerable<ShippingMethod>>(shippingMethods)
            };
            var orderRepository = new MockOrderRepository() { CurrentOrder = order};
            var shoppingCartRepository = new MockShoppingCartRepository();
            shoppingCartRepository.GetShoppingCartAsyncDelegate = () => Task.FromResult(order.ShoppingCart);
            var checkoutDataRepository = new MockCheckoutDataRepository();
            checkoutDataRepository.GetShippingAddressAsyncDelegate = s => Task.FromResult(new Address());
            checkoutDataRepository.GetBillingAddressAsyncDelegate = s => Task.FromResult(new Address());
            checkoutDataRepository.GetPaymentMethodDelegate = s => Task.FromResult(new PaymentMethod { CardNumber = "1234" });
            var target = new CheckoutSummaryPageViewModel(new MockNavigationService(), new MockOrderService(), orderRepository, shippingMethodService,
                                                          checkoutDataRepository, shoppingCartRepository,
                                                          new MockAccountService(), new MockResourceLoader(), null, null);

            target.OnNavigatedTo(new NavigatedToEventArgs { Parameter = null, NavigationMode = NavigationMode.New }, null);
            Assert.IsFalse(target.IsBottomAppBarOpened);

            target.SelectedCheckoutData = target.CheckoutDataViewModels.First();
            Assert.IsTrue(target.IsBottomAppBarOpened);
        }